es - elasticsearch mapping - parameters - index_phrases

581 阅读1分钟

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :index_phrases有什么特点?
答 :
在这里插入图片描述
问 :index_phrases如何使用?
答 :

# index_phrases
PUT /index_phrases_test
{
  "mappings" : {
    "properties" : {
      "name" : {
        "type"          : "text",
        "index_phrases" : true,
        "fields" : {
          "normal" : {
            "type" : "text"
          }
        }
      }
    }
  }
}

# 索引
POST /index_phrases_test/_doc/1
{
  "name" : "this is a dog is dog is dog"
}

# 索引
POST /index_phrases_test/_doc/2
{
  "name" : "this is dog"
}

# 搜索, index_phrases 为 true
GET /index_phrases_test/_search
{
  "query" : {
    "match_phrase" : {
      "name" : "is dog"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.23594555,
    "hits" : [
      {
        "_index" : "index_phrases_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.23594555,
        "_source" : {
          "name" : "this is dog"
        }
      },
      {
        "_index" : "index_phrases_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.21681483,
        "_source" : {
          "name" : "this is a dog is dog is dog"
        }
      }
    ]
  }
}

# 搜索, index_phrases 为 false
GET /index_phrases_test/_search
{
  "query" : {
    "match_phrase" : {
      "name.normal" : "is dog"
    }
  }
}

# 结果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.44793728,
    "hits" : [
      {
        "_index" : "index_phrases_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.44793728,
        "_source" : {
          "name" : "this is dog"
        }
      },
      {
        "_index" : "index_phrases_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.44455233,
        "_source" : {
          "name" : "this is a dog is dog is dog"
        }
      }
    ]
  }
}