您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇内容介绍了“Elasticsearch中查询的多种方式”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
query DSL:Domain Specified Language,特定领域的语言
语法
GET /{index}/_search { "query": {"match_all": {}} }
示例
输入: GET /staffs/_search { "query": {"match_all": {}} }
输出 { "took" : 1, 消耗的时间 "timed_out" : false, 是否超时 "_shards" : { "total" : 1, 一共请求了几个shared "successful" : 1, 成功了几个 "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, 查询的结果的总数量 "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "staffs", 索引的名称 "_type" : "_doc", "_id" : "1a", 数据对应的主键 "_score" : 1.0,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高 "_source" : { 数据的详情 "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 1.0, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "2", "_score" : 1.0, "_source" : { "name" : "lixiansheng", "age" : 25, "phone" : "15733333333", "posittion" : "android", "hobby" : [ "paobu", "zuqiu", "tubu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "3", "_score" : 1.0, "_source" : { "name" : "maxiaoshuan", "age" : 23, "phone" : "15744444444", "posittion" : "ios", "hobby" : [ "paobu", "yumaoqiu", "lanqiu" ] } } ] } }
语法
GET {index}/_search { "query": { "match": { "FIELD": "TEXT" FIELD:字段的名称 TEXT:条件 } }, "sort": [ { "FIELD": { FIELD: 字段的名称 "order": "desc" } } ] }
示例
GET staffs/_search { "query": { "match": { "name": "xiao" } }, "sort": [ { "age": { "order": "desc" } } ] }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : null, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "3", "_score" : null, "_source" : { "name" : "ma xiao shuai", "age" : 23, "phone" : "15744444444", "posittion" : "ios", "hobby" : [ "paobu", "yumaoqiu", "lanqiu" ] }, "sort" : [ 23 ] }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : null, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] }, "sort" : [ 21 ] } ] } }
语法
GET {index}/_search { "query": {"match_all": {}}, "from": 1, 从第几页开始查询,0:代表第一页,1:代表第二页 "size": 1 每页显示的条数 }
示例
GET staffs/_search { "query": {"match_all": {}}, "from": 1, "size": 1 }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 1.0, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] } } ] } }
语法
GET {index}}/_search { "query": { "bool": { "must": [ {"match": { "FIELD": "TEXT" }} ] } }, "post_filter": { "range": { "FIELD": { "gte": 10 } } } }
示例
GET staffs/_search { "query": { "bool": { 可以拼接多个条件的查询 "must": [ {"match": { "name": "xian sheng" }} ] } }, "post_filter": { "range": { "age": { "gt": 25 } } } }
其实有两条 但是有一个年龄为25 所以这里只显示一条 { "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.3862944, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 1.3862944, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } } ] } }
示例
GET staffs/_search { "query": {"match": { "posittion": "java kaifa" }} }
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.6694658, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 1.6694658, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 0.60996956, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] } } ] } }
和全文检索相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面意义匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回
短语搜索,要求输入的搜索串,必须在制定的字段文本中,完全包含一模一样的,擦可以算匹配,才能作为结果返回
语法
GET staffs/_search { "query": { "match_phrase": { "FIELD": "PHRASE" } } }
示例
GET staffs/_search { "query": { "match_phrase": { "posittion": "java kaifa" } } }
{ "took" : 30, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.6694657, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 1.6694657, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] } } ] } }
语法
GET {index}/_search { "query": { "match": { "FIELD": "TEXT" } }, "highlight": { "fields": { "FIELD": {} } } }
示例
GET staffs/_search { "query": { "match": { "posittion": "kaifa" } }, "highlight": { "fields": { "posittion": {} } } }
{ "took" : 54, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.60996956, "hits" : [ { "_index" : "staffs", "_type" : "_doc", "_id" : "1a", "_score" : 0.60996956, "_source" : { "name" : "shu xian sheng", "age" : 28, "phone" : "15711111111", "posittion" : "java kaifa", "hobby" : [ "lanqiu", "zuqiu", "tubu" ] }, "highlight" : { "posittion" : [ "java <em>kaifa</em>" ] } }, { "_index" : "staffs", "_type" : "_doc", "_id" : "Nq_96G0Bs8sg-pU7kn0S", "_score" : 0.60996956, "_source" : { "name" : "wang xiao san", "age" : 21, "phone" : "15722222222", "posittion" : "web kaifa", "hobby" : [ "yumaoqiu", "zuqiu", "taiqiu" ] }, "highlight" : { "posittion" : [ "web <em>kaifa</em>" ] } } ] } }
其实保存数据的时候,其查询索引就已经创建了,使用的倒排索引
例如:posittion字段,先被拆解,然后创建倒排索引
拆解是根据选择的分词器构成的
拆解的单词 | 对应的数据主键 |
---|---|
java | 1a |
kaifa | 1a,Nq_96G0Bs8sg-pU7kn0S |
web | 1a,Nq_96G0Bs8sg-pU7kn0S |
ios | 3 |
android | 4 |
GET _cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana_task_manager_1 Qyl1MousQLq5FyMOCBO4nw 1 0 2 0 30.5kb 30.5kb green open .apm-agent-configuration qPsz40bsQxW_Zcd_4pkJJg 1 0 0 0 283b 283b green open .kibana_1 yYdsQgxWQ0utXN-Ulhm5ew 1 0 9 0 35.4kb 35.4kb yellow open staffs uoo38LYwRB2PzxupYjJ66Q 1 1 4 0 17.9kb 17.9kb
“Elasticsearch中查询的多种方式”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。