ES Query的一些小笔记

  • Post author:
  • Post category:其他




match_all 全查询

GET index/_search
{
  "query": {
    "match_all": {}
  }
}



match

匹配非字符串,精确查询

匹配字符串,模糊查询

GET index/_search
{
  "query": {
    "match": {
      "account_number": 995
    }
  }
}
---------------------------
GET index/_search
{
  "query": {
    "match": {
      "desc": "like me"
    }
  }
}



match_phrase

短句匹配,不分词匹配

GET index/_search
{
  "query": {
    "match_phrase": {
      "desc": "aaa bbb"
    }
  }
}
// 说明:只匹配含有aaa bbb的desc 



multi_match

多字段查询

GET index/_search
{
  "query": {
    "multi_match": {
      "query": "aaa bbb",
      "fields": [
        "desc",
        "name"
      ]
    }
  }
}
// 说明:在字段desc和name中查询含有"aaa bbb"的字符,只含有aaa的也能查询到
// 命中越多,评分越高



wildcard通配符匹配

对于keyword格式字段,是不会进行分词存储的,一般用来精准匹配,当需要模糊匹配时,可以使用通配符模糊匹配值

GET index/_search
{
   "query":{
        "wildcard":{
              	"desc":{
              		"value":"*test123*",
              		"boost":1 //权重设置,越大命中后分数越高
              	}
         }
    }
}



bool复合查询

bool是一个或多个查询字句的组合,可以理解为sql中 where a= ‘’ and b= ‘’,多个条件查询

  • must:必须匹配每个子查询,类似“与”
  • should:选择性匹配子查询,类似“或”
  • must_not:必须不匹配,不参与算分,类似“非”
  • filter:必须匹配,不参与算分

注: 查询时,参与打分字段越多,性能越低



1、must:必须符合

GET index/_search
{
   "query":{
        "bool":{
             "must":[
              {
              		"match_phrase":{
              			"desc":"aaa bbb"
              		}
              }
             ]
         }
    }
}
//说明:必须满足desc包含“aaa”和“age”=18



2、must_not:必须不符合

GET index/_search
{
   "query":{
        "bool":{
             "must_not":[
             	 {
             		 "match": {
             		 	"desc":"aaa"
             		 }
             	 }
             ]
         }
    }
}
//说明:必须t同时不满足desc包含“aaa”和“age”=18



3、should:应该满足,满足加分,不满足不加分

GET index/_search
{
   "query":{
        "bool":{
             "must":[
              {
              	"match":{
              		"desc":"aaa"
              		}
              	}
             ],
             "must_not": [
              {
                "match": {
                  "age": "18"
                }
              }
            ],
             "should": [
              {
                "match": {
                  "lastname": "Wallace"
                }
              }
            ]
         }
    }
}



4、filter结果过滤

GET index/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "balance": {
            "gte": "10000",
            "lte": "20000"
          }
        }
      }
    }
  }
}



term 非文本匹配


term

查询被用于精确值匹配,这些精确值可能是数字、时间、布尔或者那些

not_analyzed

的字符串:

GET index/_search
{
  "query": {
    "term": {
      "age": 28
    }
  }
}

term 查询对于输入的文本不

分析

,所以它将给定的值进行精确查询。

和match一样。匹配某个属性的值。全文检索字段用match,其他非text字段匹配用term。



terms

terms 查询和 term 查询一样,但它允许你指定多值进行匹配。如果这个字段包含了指定值中的任何一个值,那么这个文档满足条件

{ "terms": { "tag": [ "search", "full_text", "nosql" ] }}



exists和missing查询


exists

查询和

missing

查询被用于查找那些指定字段中有值 (exists) 或无值 (missing) 的文档。这与SQL中的

IS_NULL

(missing) 和

NOT IS_NULL

(exists) 在本质上具有共性:

{
    "exists":   {
        "field":    "title"
    }
}

这些查询经常用于某个字段有值的情况和某个字段缺值的情况。



版权声明:本文为weixin_49374214原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。