过滤器查询在弹性搜索中不起作用

Filter query is not working in elastic search

我有如下文档和搜索查询 elastic 在创建索引时最初无法获取匹配的异常 ID 的文档,我已经完成映射,之后它无法获取记录

我的映射如下所示

{
  "mappings": {
    "properties": {
      "events": {
        "type": "nested",
        "properties": {
          "data": {
            "type": "nested",
            "properties": {
              "comments": {
                "type": "nested",
                "properties": {
                  "type": {
                    "type": "keyword"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

这是我正在使用搜索查询进行测试的索引文档。

{
  "id": "1",
  "score": 1,
  "comments": [{
    "id": "1",
    "type": "Delayed",

您不能直接在嵌套字段上使用查询字符串,您需要使用嵌套查询

GET <index-name>/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "nested": { --> note
            "path": "events.recommendationData",
            "query": {
              "query_string": {
                "query": "\"1\" OR \"2\"",
                "fields": [
                  "events.recommendationData.exceptionId"
                ],
                "type": "best_fields",
                "default_operator": "or",
                "max_determinized_states": 10000,
                "enable_position_increments": true,
                "fuzziness": "AUTO",
                "fuzzy_prefix_length": 0,
                "fuzzy_max_expansions": 50,
                "phrase_slop": 0,
                "escape": false,
                "auto_generate_synonyms_phrase_query": true,
                "fuzzy_transpositions": true,
                "boost": 1
              }
            }
          }
        }
      ]
    }
  },
  "size": 1, --> note, to return documents ,keep 0 for only aggs
  "aggs": {
            "genres": {
                "nested": {
                    "path": "events.recommendationData.recommendations"
                },
                "aggs": {
                    "nested_comments_recomms": {
                        "terms": {
                            "field": "events.recommendationData.recommendations.recommendationType"
                        }
                    }
                }
            }
        }
}