组合elasticsearch查询

问题描述 投票:1回答:1

我最近开始学习Elasticsearch,并希望合并两个查询。

{
    'query': {
        'match': {
            'title': 'phrase'
        }
    }
}

{
    'query': {
        'range': {
           '@timestamp': {
              'gte': 'now-30d',
              'lte': 'now'
            }
        }
    }
}

阅读文档,我发现可以与bool进行这种组合,但是我不知道该如何构造。

elasticsearch
1个回答
2
投票

bool是一个提供四个选项的容器

  1. 必须-它的作用类似于AND

  2. 应该-它的作用类似于OR

  3. Must_not-类似于NOT

  4. 过滤器-用作AND,但不计算分数。

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": "phrase"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-30d",
              "lte": "now"
            }
          }
        }
      ]
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.