「ElasticSearch」 ElasticSearch 简单查询
查询语句
query | bool | must | exist | “exists”: { “field”: “name” } | 判断字段是否存在 |
must_not | match | “match”: { “tweet”: “elasticsearch” } | 匹配字符串中是否包含 | ||
should | |||||
Filter | |||||
简单查询
查询某个字段是否存在或者是否为null
1curl -H 'Content-type: application/json' -XPOST 'http://ip:9200/alert_group/_search' -d
1{
2 "query": {
3 "bool": {
4 "must": { // must_not
5 "exists": {
6 "field": "name" // 必须存在该字段,且该字段不为null
7 }
8 }
9 }
10 }
11}
空查询(empty search)
{}
在功能上等价于使用 match_all
查询, 正如其名字一样,匹配所有文档:
1curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d'
2{
3 "query": {
4 "match_all": {}
5 }
6}
7'
Match
1GET /_search
2{
3 "query": {
4 "match": {
5 "tweet": "elasticsearch"
6 }
7 }
8}
复合查询
组合多条件查询。elasticsearch提供bool
来实现这种需求;
主要参数:
must
:文档 必须
匹配这些条件才能被包含进来。
must_not
:文档 必须不
匹配这些条件才能被包含进来。
should
:如果满足这些语句中的任意语句,或的关系,将增加 _score
,否则,无任何影响。它们主要用于修正每个文档的相关性得分。
filter
:必须 匹配
,但它以不评分、过滤模式来进行。这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。
一个 bool
语句 允许在你需要的时候组合其它语句,无论是 must
匹配、 must_not
匹配还是 should
匹配,同时它可以包含不评分的过滤器(filters):
1{
2 "bool": {
3 "must": { "match": { "tweet": "elasticsearch" }},
4 "must_not": { "match": { "name": "mary" }},
5 "should": { "match": { "tweet": "full text" }},
6 "filter": { "range": { "age" : { "gt" : 30 }} }
7 }
8}
了找出信件正文包含 business opportunity
的星标邮件,或者在收件箱正文包含 business opportunity
的非垃圾邮件:
shoule在与must或者filter同级时,默认是不需要满足should中的任何条件的,此时我们可以加上minimum_should_match
参数,来达到我们的目的,即上述代码改为:
1{
2 "bool": {
3 "must": { "match": { "email": "business opportunity" }},
4 "should": [
5 { "match": { "starred": true }},
6 { "bool": {
7 "must": { "match": { "folder": "inbox" }},
8 "must_not": { "match": { "spam": true }}
9 }}
10 ],
11 "minimum_should_match": 1
12 }
13}
Term
精确查询
1GET /forum/article/_search
2{
3 "query": {
4 "constant_score": {
5 "filter": {
6 "bool": {
7 "must" : [
8 {"term" : {"tag_cnt" : 1}},
9 {"terms" : {"tag" : ["java"]}}
10 ]
11 }
12 }
13 }
14 }
15}
嵌套查询
如果doc中有个字段是Object Array, 通过其中的一个Object 查询
1{
2 "query": {
3 "nested": {
4 "path": "tags", // 字段名
5 "query": {
6 "bool": {
7 "must": [
8 {
9 "term": {
10 "tags.key": "k1" // 查找tags 中包含object{key:k1,val:v1}的所有doc
11 }
12 },
13 {
14 "term": {
15 "tags.val": "v1"
16 }
17 }
18 ]
19 }
20 }
21 }
22 }
23}
1{
2 "query": {
3 "nested": {
4 "path": "tags",
5 "query": {
6 "bool": {
7 "must": [
8 {
9 "term": {
10 "tags.key": "$psm"
11 }
12 },
13 {
14 "terms": {
15 "tags.val":[ "v1","v2"] // 查询多个值
16 }
17 }
18 ]
19 }
20 }
21 }
22 },
23 "sort": [
24 {
25 "created_at": {
26 "order": "desc"
27 }
28 }
29 ]
30}
在排序的过程中,只能使用可排序的属性进行排序。那么可以排序的属性有哪些呢?
- 数字
- 日期
嵌套查询的效率相对是比较低的,如果数据量非常大的情况下就会出现慢查询的问题
1//按照条件新建一个index 作为测试数据使用
2POST _reindex
3{
4 "source": {
5 "index": "usernested",
6 "query": {
7 "nested": {
8 "path": "tags",
9 "query": {
10 "bool": {
11 "must": [
12 {
13 "term": {
14 "tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"
15 }
16 },
17 {
18 "term": {
19 "tags.site": "163"
20 }
21 }
22 ]
23 }
24 }
25 }
26 }
27 },
28 "dest": {
29 "index": "new_usernested"
30 }
31}
1//根据条件更新一个 nested的文档
2GET usernested/_update_by_query
3{
4 "query": {
5 "nested": {
6 "path": "tags",
7 "query": {
8 "bool": {
9 "must": [
10 {
11 "term": {
12 "tags.brand": "c55fd643-1333-4647-b898-fb3e5e4e6d67"
13 }
14 },
15 {
16 "term": {
17 "tags.site": "163"
18 }
19 }
20 ]
21 }
22 }
23 }
24 },
25 "script": {
26 "inline": "for(e in ctx._source.tags){e.brand = 'test2';}" //更新nested字段
27 //"inline":"ctx._source.userid = 'testid'" //更新普通字段
28 }
29}
通过_source
字段中的include和exclude来指定返回结果包含哪些字段,排除哪些字段
1{
2 "_source":{
3 "include":[
4 "policyNo",
5 "policyRelationNo",
6 "policyStatus"
7 ],
8 "exclude":[
9 "salesType"
10 ]
11 },
12 "query": {
13 "bool": {
14 "must": [
15 {
16 "term": {
17 "policyRelationNo": "KR01435021"
18 }
19 }
20 ],
21 "should": [],
22 "must_not": []
23 }
24 },
25 "from": 0,
26 "size": 10
27}
1
2{
3 "_shards": {
4 "total": 5,
5 "successful": 5,
6 "failed": 0
7 },
8 "hits": {
9 "total": 19,
10 "max_score": 11.391884,
11 "hits": [
12 {
13 "_index": "search4policy-msad-dev3_20200520000000",
14 "_type": "policy-msad-dev3",
15 "_id": "4407038",
16 "_score": 11.391884,
17 "_source": {
18 "policyRelationNo": "KR01435021",
19 "policyNo": "B609120319",
20 "policyStatus": 11
21 }
22 },
23 {
24 "_index": "search4policy-msad-dev3_20200520000000",
25 "_type": "policy-msad-dev3",
26 "_id": "4407046",
27 "_score": 10.713255,
28 "_source": {
29 "policyRelationNo": "KR01435021",
30 "policyNo": "B609120323",
31 "policyStatus": 11
32 }
33 }
34 ]
35 },
36 "took": 5,
37 "timed_out": false
38}
参考资料:
https://blog.csdn.net/u012934325/article/details/106971482/
https://blog.csdn.net/qq_35393693/article/details/80143287
https://segmentfault.com/a/1190000020245240
*https://www.jianshu.com/p/c377477df7fc