Elasticsearch中查询的多种方式
发表于:2025-01-31 作者:千家信息网编辑
千家信息网最后更新 2025年01月31日,本篇内容介绍了"Elasticsearch中查询的多种方式"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所
千家信息网最后更新 2025年01月31日Elasticsearch中查询的多种方式
本篇内容介绍了"Elasticsearch中查询的多种方式"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
一、DSL查询
query DSL:Domain Specified Language,特定领域的语言
1.查询某个索引下所有的数据
语法
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" ] } } ] }}
2.查询名字中含有xiao 按照年龄倒叙排序的
语法
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 ] } ] }}
3.分页查询数据
语法
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" ] } } ] }}
4.搜索名称含有xian sheng 且年龄大于25的
语法
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" ] } } ] }}
5.full-text search(全文检索)
示例
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" ] } } ] }}
6.phrase search(短语搜索)
和全文检索相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面意义匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回
短语搜索,要求输入的搜索串,必须在制定的字段文本中,完全包含一模一样的,擦可以算匹配,才能作为结果返回
语法
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" ] } } ] }}
7.highlight search(高亮搜索结果)
语法
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 kaifa" ] } }, { "_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 kaifa" ] } } ] }}
倒排索引
其实保存数据的时候,其查询索引就已经创建了,使用的倒排索引
例如: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.sizegreen open .kibana_task_manager_1 Qyl1MousQLq5FyMOCBO4nw 1 0 2 0 30.5kb 30.5kbgreen open .apm-agent-configuration qPsz40bsQxW_Zcd_4pkJJg 1 0 0 0 283b 283bgreen open .kibana_1 yYdsQgxWQ0utXN-Ulhm5ew 1 0 9 0 35.4kb 35.4kbyellow open staffs uoo38LYwRB2PzxupYjJ66Q 1 1 4 0 17.9kb 17.9kb
"Elasticsearch中查询的多种方式"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
查询
索引
示例
数据
语法
搜索
拆解
名称
字段
结果
全文
全文检索
年龄
检索
输入
多种
方式
代表
内容
分数
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全川大和电子科大哪个好
普联社会招聘嵌入式软件开发
安稳控制 网络安全
艾雷网络技术
日照大学 软件开发
数据库软件下载MySQL打开
江苏金税盘安全接入服务器
rtc服务器不可用
人寿保险公司长沙软件开发
51ting数据库图书表
tt服务器数据线接线图
网站服务器怎样查看管理员密码
文山市网络安全吗
网络安全宣传新闻稿 银行
服务器管理网站软件
江苏服务器虚拟化优化
人民广场租房软件开发
软件开发公司岗位有
数据库对大学生的作用
网络安全之数字签名与数字证书
数据库学入门
青少年如何培养网络安全意识
软件开发详细过程
数据库主外键关系表
杭州电脑软件开发大概要多少钱
rtc服务器不可用
国家网络安全宣传周矢量图
苹果手机怎么切服务器
深圳光启软件开发待遇如何
标准参考数据库