如何替换Elasticsearch Indices Query

时间:2016-12-19 10:34:51

标签: elasticsearch

在Elasticsearch 5.0中,Indices Query has been marked as deprecated

文档告诉我"在_index字段上搜索",但对我来说,如何做到这一点并不明显。如何将这样的示例查询更改为新方法?

GET /_search
{
    "query": {
        "bool": {
            "minimum_number_should_match": 1,
            "should": [
                {"indices": {
                    "indices": ["index1"],
                    "no_match_query": "none",
                    "query": {
                        "bool": {"must": [
                            {"multi_match": {"fields": ["field1", "field2"], "operator": "or", "query": "foobar", "type": "boolean", "use_dis_max": true}},
                            {"multi_match": {"fields": ["field1", "field2"], "operator": "or", "query": "xuul", "type": "boolean", "use_dis_max": true}}
                        ]}}
                }},
                {"indices": {
                    "indices": ["index2", "index3"],
                    "no_match_query": "none",
                    "query": {"bool": {"must": [
                        {"multi_match": {"fields": ["field1", "field2"], "operator": "or", "query": "foobar", "type": "boolean", "use_dis_max": true}}
                    ]}}}}
            ]}
    }
}

1 个答案:

答案 0 :(得分:4)

你可以尝试这样:

{
  "bool": {
    "minimum_number_should_match": 1,
    "should": [
      {
        "bool": {
          "filter": [
            {
              "terms": {
                "_index": ["index1"]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "multi_match": {
                      "fields": [
                        "field1",
                        "field2"
                      ],
                      "operator": "or",
                      "query": "foobar",
                      "type": "boolean",
                      "use_dis_max": true
                    }
                  },
                  {
                    "multi_match": {
                      "fields": [
                        "field1",
                        "field2"
                      ],
                      "operator": "or",
                      "query": "xuul",
                      "type": "boolean",
                      "use_dis_max": true
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      {
        "bool": {
          "filter": [
            {
              "terms": {
                "_index": [
                  "index2",
                  "index3"
                ]
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "multi_match": {
                      "fields": [
                        "field1",
                        "field2"
                      ],
                      "operator": "or",
                      "query": "foobar",
                      "type": "boolean",
                      "use_dis_max": true
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
}
相关问题