Elasticsearch复合查询

本文介绍了Elasticsearch的复合查询,包括精确匹配,如查询auditType为test的文档;时间范围与精确匹配结合,查询特定时间内auditType为test的文档;模糊匹配,如查询deviceTypeType为空的文档;多条件精确匹配及短语匹配,如查询公司名称含“百度”的文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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"
    }
  } ]
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值