Kibana+ElasticSearch实现索引数据的几种查询方式
1.match_all搜索,直接返回所有文档
1 2 3 4 5 6 | GET /school/_search { "query" : { "match_all" : {} } } |
返回结果大致如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | { "took" : 13, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 23, "max_score" : 1, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "b3ffcWIB-npqvsX5SmVm" , "_score" : 1, "_source" : { "aggs" : { "group_by_word_count" : { "terms" : { "field" : "word_count" } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "c3fjcWIB-npqvsX5G2Wh" , "_score" : 1, "_source" : { "aggs" : { "grades_word_count" : { "stats" : { "field" : "word_count" } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "dHfjcWIB-npqvsX5uWWr" , "_score" : 1, "_source" : { "aggs" : { "grades_word_count" : { "min" : { "field" : "word_count" } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "dXfkcWIB-npqvsX5hmWx" , "_score" : 1, "_source" : { "query" : { "match" : { "name" : "海哥" } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "dnflcWIB-npqvsX5S2V0" , "_score" : 1, "_source" : { "query" : { "multi_match" : { "query" : "海哥" , "fields" : [ "name" , "address" ] } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "fXfqcWIB-npqvsX5yGXf" , "_score" : 1, "_source" : { "query" : { "bool" : { "filter" : { "term" : { "word_count" : 2000 } } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "fnfrcWIB-npqvsX5mGXq" , "_score" : 1, "_source" : { "query" : { "constant_score" : { "filter" : { "match" : { "title" : "ElasticSearch" } }, "boost" : 2 } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "gHfucWIB-npqvsX5HWUB" , "_score" : 1, "_source" : { "query" : { "bool" : { "must_not" : [ { "term" : { "word_count" : "2000" } } ] } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "HYVJOGIBUtf8tEPshwDC" , "_score" : 1, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } }, { "_index" : "school" , "_type" : "student" , "_id" : "cHffcWIB-npqvsX59mXN" , "_score" : 1, "_source" : { "aggs" : { "group_by_word_count" : { "terms" : { "field" : "word_count" } } } } } ] } } |
参数大致解释:
took: 执行搜索耗时,毫秒为单位
time_out: 搜索是否超时
_shards: 多少分片被搜索,成功多少,失败多少
hits: 搜索结果展示
hits.total: 匹配条件的文档总数
hits.hits: 返回结果展示,默认返回十个
hits.max_score:最大匹配得分
hits._score: 返回文档的匹配得分(得分越高,匹配程度越高,越靠前)
_index _type _id 作为剥层定位到特定的文档
_source 文档源
2.执行查询
2.1 只显示name和address
1 2 3 4 5 6 7 8 9 10 | POST /school/_search { "query" : { "match_all" : {} }, "_source" : [ "name" , "address" ] } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | { "took" : 313, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 23, "max_score" : 1, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "b3ffcWIB-npqvsX5SmVm" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "c3fjcWIB-npqvsX5G2Wh" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "dHfjcWIB-npqvsX5uWWr" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "dXfkcWIB-npqvsX5hmWx" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "dnflcWIB-npqvsX5S2V0" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "fXfqcWIB-npqvsX5yGXf" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "fnfrcWIB-npqvsX5mGXq" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "gHfucWIB-npqvsX5HWUB" , "_score" : 1, "_source" : {} }, { "_index" : "school" , "_type" : "student" , "_id" : "HYVJOGIBUtf8tEPshwDC" , "_score" : 1, "_source" : { "address" : "山东烟台" , "name" : "张小花" } }, { "_index" : "school" , "_type" : "student" , "_id" : "cHffcWIB-npqvsX59mXN" , "_score" : 1, "_source" : {} } ] } } |
2.2 返回name为haige的document
1 2 3 4 5 6 7 8 | POST /school/_search { "query" : { "match" : { "name" : "张小花" } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | { "took" : 439, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 2.634553, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "bXfXcWIB-npqvsX5w2Vc" , "_score" : 2.634553, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } }, { "_index" : "school" , "_type" : "student" , "_id" : "HYVJOGIBUtf8tEPshwDC" , "_score" : 0.8630463, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } } ] } } |
2.3 返回name包含"海"的所有document
1 2 3 4 5 6 7 8 | POST /school/_search { "query" : { "match" : { "name" : "海" } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | { "took" : 68, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0417081, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "3" , "_score" : 1.0417081, "_source" : { "name" : "海哥" , "address" : "山东济宁" , "age" : 27, "date" : "1998-03-16" } } ] } } |
2.4 返回name中包含term "海" 或 "花" 的所有document
1 2 3 4 5 6 7 8 | POST /school/_search { "query" : { "match" : { "name" : "海 花" } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | { "took" : 26, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0417081, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "3" , "_score" : 1.0417081, "_source" : { "name" : "海哥" , "address" : "山东济宁" , "age" : 27, "date" : "1998-03-16" } }, { "_index" : "school" , "_type" : "student" , "_id" : "bXfXcWIB-npqvsX5w2Vc" , "_score" : 0.8781843, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } }, { "_index" : "school" , "_type" : "student" , "_id" : "HYVJOGIBUtf8tEPshwDC" , "_score" : 0.2876821, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } } ] } } |
2.5 匹配phrase "海 花"
1 2 3 4 5 6 7 8 | POST /school/_search { "query" : { "match_phrase" : { "name" : "海 花" } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | { "took" : 5, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 0, "max_score" : null , "hits" : [] } } |
2.6 返回name中包含"海"和"哥"的所有账户(AND)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | POST /school/_search { "query" : { "bool" : { "must" : [ { "match" : { "name" : "海" } }, { "match" : { "name" : "哥" } } ] } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | { "took" : 208, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 2.0834162, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "3" , "_score" : 2.0834162, "_source" : { "name" : "海哥" , "address" : "山东济宁" , "age" : 27, "date" : "1998-03-16" } } ] } } |
2.7 返回name中包含"海"或"花"的所有document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | POST /school/_search { "query" : { "bool" : { "should" : [ { "match" : { "name" : "海" } }, { "match" : { "name" : "花" } } ] } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | { "took" : 19, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 3, "max_score" : 1.0417081, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "3" , "_score" : 1.0417081, "_source" : { "name" : "海哥" , "address" : "山东济宁" , "age" : 27, "date" : "1998-03-16" } }, { "_index" : "school" , "_type" : "student" , "_id" : "bXfXcWIB-npqvsX5w2Vc" , "_score" : 0.8781843, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } }, { "_index" : "school" , "_type" : "student" , "_id" : "HYVJOGIBUtf8tEPshwDC" , "_score" : 0.2876821, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } } ] } } |
2.8 查询name中既不包含"海",也不包含"哥"的所有document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | POST /school/_search { "query" : { "bool" : { "must_not" : [ { "match" : { "name" : "海" } }, { "match" : { "name" : "哥" } } ] } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | { "took" : 264, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 22, "max_score" : 1, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "b3ffcWIB-npqvsX5SmVm" , "_score" : 1, "_source" : { "aggs" : { "group_by_word_count" : { "terms" : { "field" : "word_count" } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "c3fjcWIB-npqvsX5G2Wh" , "_score" : 1, "_source" : { "aggs" : { "grades_word_count" : { "stats" : { "field" : "word_count" } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "dHfjcWIB-npqvsX5uWWr" , "_score" : 1, "_source" : { "aggs" : { "grades_word_count" : { "min" : { "field" : "word_count" } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "dXfkcWIB-npqvsX5hmWx" , "_score" : 1, "_source" : { "query" : { "match" : { "name" : "海哥" } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "dnflcWIB-npqvsX5S2V0" , "_score" : 1, "_source" : { "query" : { "multi_match" : { "query" : "海哥" , "fields" : [ "name" , "address" ] } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "fXfqcWIB-npqvsX5yGXf" , "_score" : 1, "_source" : { "query" : { "bool" : { "filter" : { "term" : { "word_count" : 2000 } } } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "fnfrcWIB-npqvsX5mGXq" , "_score" : 1, "_source" : { "query" : { "constant_score" : { "filter" : { "match" : { "title" : "ElasticSearch" } }, "boost" : 2 } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "gHfucWIB-npqvsX5HWUB" , "_score" : 1, "_source" : { "query" : { "bool" : { "must_not" : [ { "term" : { "word_count" : "2000" } } ] } } } }, { "_index" : "school" , "_type" : "student" , "_id" : "HYVJOGIBUtf8tEPshwDC" , "_score" : 1, "_source" : { "name" : "张小花" , "address" : "山东烟台" , "age" : 24, "date" : "1996-07-24" } }, { "_index" : "school" , "_type" : "student" , "_id" : "cHffcWIB-npqvsX59mXN" , "_score" : 1, "_source" : { "aggs" : { "group_by_word_count" : { "terms" : { "field" : "word_count" } } } } } ] } } |
2.9 返回name中包含"海",且地址不是"山东烟台"的所有document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | POST /school/_search { "query" : { "bool" : { "must" : [ { "match" : { "name" : "海" } } ], "must_not" : [ { "match" : { "address" : "山东烟台" } } ] } } } |
查询结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | { "took" : 73, "timed_out" : false , "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0417081, "hits" : [ { "_index" : "school" , "_type" : "student" , "_id" : "3" , "_score" : 1.0417081, "_source" : { "name" : "海哥" , "address" : "山东济宁" , "age" : 27, "date" : "1998-03-16" } } ] } } |
3. 过滤查询
3.1 在所有document中寻找age在0-25岁之间(闭区间)的学生
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | POST /school/_search { "query" : { "filtered" : { "query" : { "match_all" : {} }, "filter" : { "range" : { "age" : { "gte" : 0, "lte" : 25 } } } } } } |
4.谈论query和filter的效率
一般认为filter的速度快于query的速度
- filter不会计算相关度得分,效率高
- filter的结果可以缓存到内存中,方便再用
原文链接:https://blog.csdn.net/linhaiyun_ytdx/article/details/79762926
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具