【问题标题】:ElasticSearch Term Query for multiple fields多个字段的 ElasticSearch 术语查询
【发布时间】:2022-01-03 16:13:12
【问题描述】:

我下面的 elasticsearch 查询以 0 条记录回复我。但是,如果我分别查询 Approved 或 Declined,它会给出我想要的确切结果。

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "responseType": "Approved"
          }
        },
        {
          "term": {
            "responseType": "Declined"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

【问题讨论】:

  • 因为我不认为responseType 可以有多个值,所以你想要Approved OR Declined 而不是Approved AND Declined,对吗?
  • 我将批准、拒绝、错误、无。
  • 你没有回答我的问题。你想要AND 还是OR 语义?
  • 我想要多个值,比如我需要 Approved 和 Declined 的值。

标签: elasticsearch elasticsearch-5 elasticsearch-dsl


【解决方案1】:

如果你想要OR 语义,那么你有两个选择:

选项1:bool/should查询:

{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "term": {
            "responseType.keyword": "Approved"
          }
        },
        {
          "term": {
            "responseType.keyword": "Declined"
          }
        }
      ],
      "must_not": [],
      "must": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

选项 2:terms 查询:

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "responseType.keyword": ["Approved", "Declined"]
          }
        }
      ],
      "must": [],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [],
  "aggs": {}
}

【讨论】:

  • 在给定 ``` { "took": 0, "timed_out": false, "_shards": { "total": 1, "successful": 1, "skipped “:0,“失败”:0 },“命中”:{“总”:{“价值”:0,“关系”:“eq”},“max_score”:空,“命中”:[] } } ```
  • 奇怪,如果您的初始查询有效,那么这两个也应该有效。你能分享你的索引映射吗?
  • ``` "mappings": { "properties": { "responseType": { "type": "text", "fields": { "keyword": { "type": "keyword ", "ignore_above": 256 } } }, "transactionType": { "type": "text", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } }, ```
  • 谢谢,我修复了查询以使用正确的字段,即.keyword 子字段。请重试。
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多