2019-10-12更
- URI Search
GET /movies/_search?q=2012&df=title&sort=year:desc&from=0&size=10&timeout=1s { "profile": "true" }
上面是一个请求uri search请求例子
-
q : 指的是查询语句,使用query string syntax
-
df : 默认字段,针对某一个字段查询,如果不指会对所有字段进行查询。
-
sort : 排序规则,sort=year:desc, 对年份进行倒叙排序
-
form : 查询文档起始偏移量,即offset
-
size: 查询条数,及limit
-
timout: 超时时间
-
profile: 查询分析,在请求体里加上这个profile,在结果中即能返回查询过程
{ "took" : 3, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 6, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "105254", "_score" : null, "_source" : { "year" : 2013, "genre" : [ "Adventure", "Comedy" ], "@version" : "1", "id" : "105254", "title" : "Crystal Fairy & the Magical Cactus and 2012" }, "sort" : [ 2013 ] } ] }, "profile" : { "shards" : [ { "id" : "[JZoUKVAzQkuhCZV5j8r4Qg][movies][0]", "searches" : [ { "query" : [ { "type" : "TermQuery", "description" : "title:2012", "time_in_nanos" : 237890, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 2266, "match" : 0, "next_doc_count" : 6, "score_count" : 0, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 3776, "advance_count" : 1, "score" : 0, "build_scorer_count" : 5, "create_weight" : 23410, "shallow_advance" : 0, "create_weight_count" : 1, "build_scorer" : 208425 } } ], "rewrite_time" : 6419, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 300932, "children" : [ { "name" : "SimpleFieldCollector", "reason" : "search_top_hits", "time_in_nanos" : 287335 } ] } ] } ], "aggregations" : [ ] } ] } }
上面是请求的结果
-
-
通过使用脚本创建新字段
GET movies/_search { "profile": "true", "size": 1, "script_fields": { "myfield": { "script": { "lang": "painless", "source": "doc['year'].value + '哈哈哈'" } } } }
执行结果
{ "took" : 108, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 10000, "relation" : "gte" }, "max_score" : 1.0, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "126", "_score" : 1.0, "fields" : { "myfield" : [ "1994哈哈哈" ] } } ] }, "profile" : { "shards" : [ { "id" : "[JZoUKVAzQkuhCZV5j8r4Qg][movies][0]", "searches" : [ { "query" : [ { "type" : "MatchAllDocsQuery", "description" : "*:*", "time_in_nanos" : 640401, "breakdown" : { "set_min_competitive_score_count" : 4, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 173688, "next_doc" : 107233, "match" : 0, "next_doc_count" : 2, "score_count" : 2, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 10572, "advance_count" : 4, "score" : 6042, "build_scorer_count" : 8, "create_weight" : 8685, "shallow_advance" : 0, "create_weight_count" : 1, "build_scorer" : 334160 } } ], "rewrite_time" : 3776, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 468957, "children" : [ { "name" : "SimpleTopScoreDocCollector", "reason" : "search_top_hits", "time_in_nanos" : 445169 } ] } ] } ], "aggregations" : [ ] } ] } }
通过在请求体内使用script_fields关键词执行新字段名称,并指出新字段应用的脚本及如何取值
2019-10-14更search 查询相关
- match query
GET movies/_search { "profile": "true", "query": { "match": { "title": "Jupiter Ascending" } } }
match 中Jupiter Ascending,使用match 查询,es会默认将搜索项认定为两个单词,即 Jupiter 和Ascending,这样其实搜的就是 title为 Jupiter 或title为 Ascending 的文档,下面是执行结果及解析
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 22.639622, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "113345", "_score" : 22.639622, "_source" : { "year" : 2015, "genre" : [ "Action", "Adventure", "Sci-Fi" ], "@version" : "1", "id" : "113345", "title" : "Jupiter Ascending" } } ] }, "profile" : { "shards" : [ { "id" : "[JZoUKVAzQkuhCZV5j8r4Qg][movies][0]", "searches" : [ { "query" : [ { "type" : "BooleanQuery", "description" : "title:jupiter title:ascending", "time_in_nanos" : 793687, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 1, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 6419, "match" : 755, "next_doc_count" : 1, "score_count" : 1, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 95906, "advance_count" : 1, "score" : 9818, "build_scorer_count" : 5, "create_weight" : 255623, "shallow_advance" : 0, "create_weight_count" : 1, "build_scorer" : 425156 }, "children" : [ { "type" : "TermQuery", "description" : "title:jupiter", "time_in_nanos" : 298684, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 3, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 1, "compute_max_score_count" : 3, "compute_max_score" : 58525, "advance" : 1511, "advance_count" : 2, "score" : 6797, "build_scorer_count" : 6, "create_weight" : 135174, "shallow_advance" : 18124, "create_weight_count" : 1, "build_scorer" : 78537 } }, { "type" : "TermQuery", "description" : "title:ascending", "time_in_nanos" : 138968, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 3, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 1, "compute_max_score_count" : 3, "compute_max_score" : 12460, "advance" : 9440, "advance_count" : 2, "score" : 755, "build_scorer_count" : 6, "create_weight" : 81935, "shallow_advance" : 6042, "create_weight_count" : 1, "build_scorer" : 28320 } } ] } ], "rewrite_time" : 23787, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 39645, "children" : [ { "name" : "SimpleTopScoreDocCollector", "reason" : "search_top_hits", "time_in_nanos" : 21900 } ] } ] } ], "aggregations" : [ ] } ] } }
通过 profile我们能看出 ,match 查询实际上将短语解析成了两个单独的单词,并将之间作为或关系关联,然后进行查询。如果我们想使用match进行匹配短语,就查title 为 Jupiter Ascending 的短语,位置不能变,那我们可以使用match的operator关键词指定操作形式,如下
GET movies/_search { "profile": "true", "query": { "match": { "title":{ "query": "Jupiter Ascending", "operator": "and" } } } }
注意,我们给match要查询的短语指定一个operator为and,这样es解析的时候依旧会将短语解析成两个单词,但单词之间的关联关系不再是or而是and,下面是执行结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 22.639622, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "113345", "_score" : 22.639622, "_source" : { "year" : 2015, "genre" : [ "Action", "Adventure", "Sci-Fi" ], "@version" : "1", "id" : "113345", "title" : "Jupiter Ascending" } } ] }, "profile" : { "shards" : [ { "id" : "[JZoUKVAzQkuhCZV5j8r4Qg][movies][0]", "searches" : [ { "query" : [ { "type" : "BooleanQuery", "description" : "+title:jupiter +title:ascending", "time_in_nanos" : 604517, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 45687, "match" : 0, "next_doc_count" : 1, "score_count" : 1, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 10194, "advance_count" : 1, "score" : 5286, "build_scorer_count" : 5, "create_weight" : 410809, "shallow_advance" : 0, "create_weight_count" : 1, "build_scorer" : 132532 }, "children" : [ { "type" : "TermQuery", "description" : "title:jupiter", "time_in_nanos" : 340220, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 3, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 1, "compute_max_score_count" : 2, "compute_max_score" : 12837, "advance" : 1889, "advance_count" : 2, "score" : 3776, "build_scorer_count" : 6, "create_weight" : 292626, "shallow_advance" : 3399, "create_weight_count" : 1, "build_scorer" : 25678 } }, { "type" : "TermQuery", "description" : "title:ascending", "time_in_nanos" : 94029, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 0, "shallow_advance_count" : 3, "set_min_competitive_score" : 0, "next_doc" : 0, "match" : 0, "next_doc_count" : 0, "score_count" : 1, "compute_max_score_count" : 2, "compute_max_score" : 3397, "advance" : 378, "advance_count" : 1, "score" : 755, "build_scorer_count" : 3, "create_weight" : 72118, "shallow_advance" : 1510, "create_weight_count" : 1, "build_scorer" : 15860 } } ] } ], "rewrite_time" : 9062, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 16990, "children" : [ { "name" : "SimpleTopScoreDocCollector", "reason" : "search_top_hits", "time_in_nanos" : 9819 } ] } ] } ], "aggregations" : [ ] } ] } }
通过profile我们能看出搜索描述中已经将两个关键词以+的形式即and的方式连接起来并再查询了。这里有一点要注意的是,比如使用operator指定关联关系为and的词,词之间的位置也会影响查询结果,Jupiter Ascending 会被解析为Jupiter + Ascending,Jupiter Ascending会被查询到,而Ascending Jupiter则不会。
-
matc_phrase短语查询
GET movies/_search { "profile": "true", "query": { "match_phrase": { "title": "Jupiter Ascending" } } }
match_phrase短语查询,将查询项看成一个整体进行查询,位置,单词间顺序保持不变,一下是查询结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 21.460526, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "113345", "_score" : 21.460526, "_source" : { "year" : 2015, "genre" : [ "Action", "Adventure", "Sci-Fi" ], "@version" : "1", "id" : "113345", "title" : "Jupiter Ascending" } } ] }, "profile" : { "shards" : [ { "id" : "[JZoUKVAzQkuhCZV5j8r4Qg][movies][0]", "searches" : [ { "query" : [ { "type" : "PhraseQuery", "description" : """title:"jupiter ascending"""", "time_in_nanos" : 575828, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 2, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 1889, "match" : 18502, "next_doc_count" : 2, "score_count" : 1, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 5286, "advance_count" : 2, "score" : 2643, "build_scorer_count" : 7, "create_weight" : 391552, "shallow_advance" : 0, "create_weight_count" : 1, "build_scorer" : 155941 } } ], "rewrite_time" : 2265, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 17368, "children" : [ { "name" : "SimpleTopScoreDocCollector", "reason" : "search_top_hits", "time_in_nanos" : 7178 } ] } ] } ], "aggregations" : [ ] } ] } }
看profile的description,将搜索短语看成了一个整体进行搜索。如果说我们的短语可能会有一个不确定的字符或者短语,我们可以使用slop关键词来进行查询
GET movies/_search { "profile": "true", "query": { "match_phrase": { "title": { "query": "i too", "slop": 1 } } } }
关键词中指定短语采用slop关键词,会扩大搜索范围,短语之间允许存在一个字符或者短语,以下是搜索结果
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 6.0625525, "hits" : [ { "_index" : "movies", "_type" : "_doc", "_id" : "90114", "_score" : 6.0625525, "_source" : { "year" : 1935, "genre" : [ "Comedy", "Musical", "Romance" ], "@version" : "1", "id" : "90114", "title" : "I Dream Too Much" } } ] }, "profile" : { "shards" : [ { "id" : "[JZoUKVAzQkuhCZV5j8r4Qg][movies][0]", "searches" : [ { "query" : [ { "type" : "PhraseQuery", "description" : """title:"i too"~1""", "time_in_nanos" : 333042, "breakdown" : { "set_min_competitive_score_count" : 0, "match_count" : 4, "shallow_advance_count" : 0, "set_min_competitive_score" : 0, "next_doc" : 21899, "match" : 49840, "next_doc_count" : 4, "score_count" : 1, "compute_max_score_count" : 0, "compute_max_score" : 0, "advance" : 23033, "advance_count" : 1, "score" : 2265, "build_scorer_count" : 6, "create_weight" : 120448, "shallow_advance" : 0, "create_weight_count" : 1, "build_scorer" : 115540 } } ], "rewrite_time" : 1888, "collector" : [ { "name" : "CancellableCollector", "reason" : "search_cancelled", "time_in_nanos" : 21522, "children" : [ { "name" : "SimpleTopScoreDocCollector", "reason" : "search_top_hits", "time_in_nanos" : 6422 } ] } ] } ], "aggregations" : [ ] } ] } }
根据profile分析来看是将关键词转换为了个匹配项 i too"~1 。
2019-10-16更
-
mapping的dynamic属性:
-
true : es的dynamic默认为true,新增字段数据存入时,mapping会被更新,文档可索引、字段可索引。
-
false: 新增字段数据存入时,mapping不会被更新,文档可索引,字段不可索引
-
strict,:新增字段数据存入时,mapping会更新、文档不可索引、字段不可索引
{ "error": { "root_cause": [ { "type": "strict_dynamic_mapping_exception", "reason": "mapping set to strict, dynamic introduction of [newtitle] within [_doc] is not allowed" } ], "type": "strict_dynamic_mapping_exception", "reason": "mapping set to strict, dynamic introduction of [newtitle] within [_doc] is not allowed" }, "status": 400 }
上面返回的是dynamic设置为strict时插入新字段返回的错误提示
-