Elasticsearch复合查询

1、精确匹配:

查询auditType为test的文档

{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "auditType": "test"
                    }
                }
            ]
        }
    }
}

 

2、时间范围+精确匹配字段

查询auditType为test的并在时间范围(包括起始时间)内的文档

{
  "query" : {
    "bool" : {
      "must" : [ {
        "term" : {
          "auditType" : "test"
        }
      }, {
        "range" : {
          "reportTime" : {
            "from" : 1551369600000,
            "to" : 1551455999000,
            "include_lower" : true,
            "include_upper" : true
          }
        }
      } ]
    }
  }
}

 

3、模糊匹配:

 查询deviceTypeType为空的文档

{
    "query": {
        "bool": {
            "must_not": [
                {
                    "wildcard": {
                        "deviceTypeType": "*"
                    }
                }
            ]
        }
    }
}

4、多条件精确匹配

terms相当于sql的in条件

{
  "query": {
    "bool": {
      "must_not": [
        {
          "term": {
            "deleteState": "1"
          }
        },
        {
          "term": {
            "assetStatus": "5"
          }
        }
      ],
      "must": [
        {
          "term": {
            "isRegister": "0"
          }
        },
        {
          "terms": {
            "regState": [
              "0.0",
              "0"
            ]
          }
        }
      ]
    }
  }
}

 

5、短语匹配

type有phrase_prefix和phrase,phrase_prefix为前缀短语匹配

查询公司名称包含“百度”的文档

 

{
  "query" : {
    "bool" : {
      "must" : {
        "match" : {
          "company" : {
            "query" : "百度",
            "type" : "phrase"
          }
        }
      }
    }
  },
  "sort" : [ {
    "name" : {
      "order" : "asc"
    }
  } ]
}

 

 

你可能感兴趣的:(随笔,Elasticsearch,复合查询)