世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问 :index_options有什么特点?
答 :
问 :index_options如何使用?
答 :
# index_options
PUT /index_options_test
{
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"index_options" : "positions",
"fields" : {
"doc" : {
"type" : "text",
"index_options" : "docs"
},
"freq" : {
"type" : "text",
"index_options" : "freqs"
},
"offset" : {
"type" : "text",
"index_options" : "offsets"
}
}
}
}
}
}
# 索引
POST /index_options_test/_doc/1
{
"name" : "hello good me"
}
# 索引
POST /index_options_test/_doc/2
{
"name" : "hello hello good me"
}
# 搜索, positions - 默认
GET /index_options_test/_search
{
"query" : {
"match" : {
"name" : "hello"
}
}
}
# 结果
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.24100877,
"hits" : [
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.24100877,
"_source" : {
"name" : "hello hello good me"
}
},
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.19363807,
"_source" : {
"name" : "hello good me"
}
}
]
}
}
# 搜索,docs - 不通过词频评分
GET /index_options_test/_search
{
"query" : {
"match" : {
"name.doc" : "hello"
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.18232156,
"hits" : [
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.18232156,
"_source" : {
"name" : "hello good me"
}
},
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.18232156,
"_source" : {
"name" : "hello hello good me"
}
}
]
}
}
# 搜索,freqs - 通过词频评分
GET /index_options_test/_search
{
"query" : {
"match" : {
"name.freq" : "hello"
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.24100877,
"hits" : [
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.24100877,
"_source" : {
"name" : "hello hello good me"
}
},
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.19363807,
"_source" : {
"name" : "hello good me"
}
}
]
}
}
# 搜索,positions - 支持短语查询
GET /index_options_test/_search
{
"query" : {
"match_phrase" : {
"name" : "hello good"
}
}
}
# 结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.38727614,
"hits" : [
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.38727614,
"_source" : {
"name" : "hello good me"
}
},
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.34450948,
"_source" : {
"name" : "hello hello good me"
}
}
]
}
}
# 搜索,docs - 不支持短语查询
GET /index_options_test/_search
{
"query" : {
"match_phrase" : {
"name.doc" : "hello good"
}
}
}
# 结果
{
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "failed to create query: field:[name.doc] was indexed without position data; cannot run PhraseQuery",
"index_uuid" : "bCDW6OdlQHuW3_EMyP9SwA",
"index" : "index_options_test"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "index_options_test",
"node" : "VK0GF1KyQGKmM_0KvgHHnw",
"reason" : {
"type" : "query_shard_exception",
"reason" : "failed to create query: field:[name.doc] was indexed without position data; cannot run PhraseQuery",
"index_uuid" : "bCDW6OdlQHuW3_EMyP9SwA",
"index" : "index_options_test",
"caused_by" : {
"type" : "illegal_state_exception",
"reason" : "field:[name.doc] was indexed without position data; cannot run PhraseQuery"
}
}
}
]
},
"status" : 400
}
# 搜索,freqs - 不支持短语查询
GET /index_options_test/_search
{
"query" : {
"match_phrase" : {
"name.freq" : "hello good"
}
}
}
# 结果
{
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "failed to create query: field:[name.freq] was indexed without position data; cannot run PhraseQuery",
"index_uuid" : "bCDW6OdlQHuW3_EMyP9SwA",
"index" : "index_options_test"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "index_options_test",
"node" : "VK0GF1KyQGKmM_0KvgHHnw",
"reason" : {
"type" : "query_shard_exception",
"reason" : "failed to create query: field:[name.freq] was indexed without position data; cannot run PhraseQuery",
"index_uuid" : "bCDW6OdlQHuW3_EMyP9SwA",
"index" : "index_options_test",
"caused_by" : {
"type" : "illegal_state_exception",
"reason" : "field:[name.freq] was indexed without position data; cannot run PhraseQuery"
}
}
}
]
},
"status" : 400
}
# 搜索,offsets - 加速高亮
GET /index_options_test/_search
{
"query" : {
"match" : {
"name.offset" : "hello good"
}
},
"highlight" : {
"fields" : {
"name.offset" : {}
}
}
}
# 结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.4132635,
"hits" : [
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.4132635,
"_source" : {
"name" : "hello hello good me"
},
"highlight" : {
"name.offset" : [
"<em>hello</em> <em>hello</em> <em>good</em> me"
]
}
},
{
"_index" : "index_options_test",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.38727614,
"_source" : {
"name" : "hello good me"
},
"highlight" : {
"name.offset" : [
"<em>hello</em> <em>good</em> me"
]
}
}
]
}
}