elasticsearch常用命令

1、term:包含对应值

       匹配对应字段包含指定的值,如果该字段设置了可以分词查询,则匹配的是分词后的查询比配。注意:该用法不是等于对应值,而是包含对应的值。

 1 GET /my_store/products/_search
 2 {
 3     "query" : {
 4         "constant_score" : {
 5             "filter" : {
 6                 "term" : {
 7                     "productID" : "XHDK-A-1293-#fJ3"
 8                 }
 9             }
10         }
11     }
12 }
13 查询my_sore索引products类型中字段productID中包含"XHDK-A-1293-#fJ3"的文档,如果productID字段设置了分词,name就是查询productID字段中包含"xhdk","a","1296","fj3"的文档
View Code

2、terms:包含数组内的某个值

   匹配对应字段包含指定的集合,如果查询字段包含对应term中的任何一个值都将返回。

GET /my_store/products/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "terms" : { 
                    "price" : [20, 30]
                }
            }
        }
    }
}

查询:my_store索引products类型中price等于20或等于30的数据
View Code

 

3、exists:存在

   查询对应字段不为空的数据,如MySQL的 is not null

GET /my_index/posts/_search
{
    "query" : {
        "constant_score" : {
            "filter" : {
                "exists" : { "field" : "tags" }
            }
        }
    }
}

查询my_index索引,posts类型中tags字段中不为空的数据
View Code

4、missing: 不存在

  查询对应字段为空的数据,如MySQL的 is null

 1 GET /my_index/posts/_search
 2 {
 3     "query" : {
 4         "constant_score" : {
 5             "filter": {
 6                 "missing" : { "field" : "tags" }
 7             }
 8         }
 9     }
10 }
11 查询my_index索引posts类型中 tags字段为空的数据
View Code

   exists和missing也可以作用于一个对象

 1 如对象name:
 2 {
 3    "name" : {
 4       "first" : "John",
 5       "last" :  "Smith"
 6    }
 7 }
 8 查询:
 9 {
10     "exists" : { "field" : "name" }
11 }
12 实际执行的是:
13 {
14     "bool": {
15         "should": [
16             { "exists": { "field": "name.first" }},
17             { "exists": { "field": "name.last" }}
18         ]
19     }
20 }
View Code

5、should:满足其中一个条件即可:如MySQL中的or

样例:

SELECT document
FROM   products
WHERE  productID      = "KDKE-B-9947-#kL5"
  OR (     productID = "JODL-X-1937-#pV7"
       AND price     = 30 )

ES查询实现语句:
GET /my_store/products/_search
{
   "query" : {
      "filtered" : {
         "filter" : {
            "bool" : {
              "should" : [
                { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, 
                { "bool" : { 
                  "must" : [
                    { "term" : {"productID" : "JODL-X-1937-#pV7"}}, 
                    { "term" : {"price" : 30}} 
                  ]
                }}
              ]
           }
         }
      }
   }
}
View Code

6、minimum_should_match:设置控制需要匹配的 should 语句的数量,它既可以是一个绝对的数字,又可以是个百分比

 1 GET /my_index/my_type/_search
 2 {
 3   "query": {
 4     "bool": {
 5       "should": [
 6         { "match": { "title": "brown" }},
 7         { "match": { "title": "fox"   }},
 8         { "match": { "title": "dog"   }}
 9       ],
10       "minimum_should_match": 2 
11     }
12   }
13 }
14 
15 这个查询结果会将所有满足以下条件的文档返回: title 字段包含 "brown"
16 AND "fox" 、 "brown" AND "dog" 或 "fox" AND "dog" 。如果有文档包含所有三个条件,它会比只包含两个的文档更相关。
View Code

7、match:查询包含对应的数据

底层就是生成 term 查询包裹在一个 bool 查询中,默认使用的or

8、operator:与match配合使用,match默认使用or,可以通过配置operator设置使用and

1 {
2     "match": {
3         "title": {
4             "query":    "brown fox",
5             "operator": "and"
6         }
7     }
8 }
View Code

9、dis_max:最大化查询-- 将任何与任一查询匹配的文档作为结果返回,但只将最佳匹配的评分作为查询的评分结果返回

PUT /my_index/my_type/1
{
    "title": "Quick brown rabbits",
    "body":  "Brown rabbits are commonly seen."
}

PUT /my_index/my_type/2
{
    "title": "Keeping pets healthy",
    "body":  "My quick brown fox eats rabbits on a regular basis."
}

查询语句:
{
    "query": {
        "dis_max": {
            "queries": [
                { "match": { "title": "Brown fox" }},
                { "match": { "body":  "Brown fox" }}
            ]
        }
    }
}
返回结果:
{
  "hits": [
     {
        "_id":      "2",
        "_score":   0.21509302,
        "_source": {
           "title": "Keeping pets healthy",
           "body":  "My quick brown fox eats rabbits on a regular basis."
        }
     },
     {
        "_id":      "1",
        "_score":   0.12713557,
        "_source": {
           "title": "Quick brown rabbits",
           "body":  "Brown rabbits are commonly seen."
        }
     }
  ]
}
View Code

10、multi_match:

 

posted @ 2022-06-02 15:51  陈远波  阅读(539)  评论(0编辑  收藏  举报