使用嵌套字段进行多索引搜索


Multi indices search with nested fields

我有两个索引:

首先,questions有嵌套字段answers。第二,articles没有此字段。

我尝试通过多个索引搜索:

{
    "index": "questions, articles",
    "body":{
        "query":{
            "bool":{
                "must":{
                    "nested":{
                        "path": "answerswer",
                        ...
                    }
                }
            }
        }
    }
}

,得到错误"query_parsing_exception: [nested] failed to find nested object under path [answer]"

如何搜索没有错误,当一个索引有嵌套字段,但另一个没有?

我认为您需要使用indices查询,并为每个索引使用不同的查询。像这样:

GET /questions,articles/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "indices": {
                  "indices": [
                    "questions"
                  ],
                  "query": {
                    "nested": {
                      "path": "answerswer",
                      "query": {
                        "term": {
                          "text": "bla"
                        }
                      }
                    }
                  }
                }
              },
              {
                "match_all": {}
              }
            ]
          }
        },
        {
          "term": {
            "some_common_field": {
              "value": "whatever"
            }
          }
        }
      ]
    }
  }
}