elasticsearch - elasticsearch需要在 bool 中添加一个必须的查询

标签 elasticsearch filter

我有以下查询可以正常工作:

GET <index_name>/_search
{        
    "sort": [
      {
        "irFileCreateTime": {
        "order": "desc"
       }
     }
    ],
    "query": {
        "bool": {
          "should": [
            {
              "match": {
                "fileId": 46704
              }
            },
            {
              "match": {
                "fileId": 46706
              }
            },
            {
              "match": {
                "fileId": 46719
              }
            }
          ]
        }
     }
}

问题是我需要进一步过滤数据,但是我需要过滤的字段是文本字段。我尝试了多种不同的方式来将必须匹配项放入查询中,但是当我知道它只过滤掉一半时,所有内容要么格式错误,要么过滤掉所有匹配。如何向此查询添加必须匹配的“irStatus”:“COMPLETE”?提前致谢。

最佳答案

您所追求的是查询term,最好是查询keywordirStatus。也就是说:

GET index/_search
{
  "sort": [
    {
      "irFileCreateTime": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "irStatus.keyword": {
              "value": "COMPLETE"
            }
          }
        }
      ],
      "should": [
        {
          "match": {
            "fileId": 46704
          }
        },
        {
          "match": {
            "fileId": 46706
          }
        },
        {
          "match": {
            "fileId": 46719
          }
        }
      ]
    }
  }
}

假设您的映射如下所示:
{
  "mappings": {
    "properties": {
      "irFileCreateTime": {
        "type": "date"
      },
      "fileId": {
        "type": "integer"
      },
      "irStatus": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

它显然在您端上失败的原因是由于standard分析器,“COMPLETE”已小写。

或者,您可以执行以下操作:
{
  "must":[
    {
      "query_string":{
        "query":"irStatus:COMPLETE AND (fileId:(46704 OR 46706 OR 46719))"
      }
    }
  ]
}

关于elasticsearch - elasticsearch需要在 bool 中添加一个必须的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62312011/

相关文章:

elasticsearch - elasticsearch 7无法将映射放在索引logstash类型事件和拒绝映射更新上

Mysql 通过其他表中的选项从一个表中选择

php - WordPress:如何只显示特定类别的帖子?

for-loop - 使用 filter() 的惯用方式

Elasticsearch:禁用索引文档的版本控制

elasticsearch - Kibana 和 _source 字段,Elastic 版本 6.7

lucene - 我可以在节点/集群上创建ElasticSearch索引,然后在以后更改集群吗?

javascript - 在 Elasticsearch 中查询

plugins - Extjs 4网格过滤

javascript - Backbone.js:如何通过模型 ID 数组过滤对象集合?