ElasticSearch在多索引,多类型搜索时返回404

时间:2017-08-02 12:36:18

标签: elasticsearch

我们有一个要求,我们需要在多个索引中进行查询,如下所示 我们正在使用ElasticSearch 5.1.1 http://localhost:9200/index1,index2,index3/type1,type2/_search
查询:

{
 "query": {
  "multi_match": {
   "query": "data",
   "fields": ["status"]
  }
 }
}

但是我们可能事先并不知道指数是否存在,如果上述指数之一不存在,我们会得到以下错误。

{
    "error": {
        "root_cause": [
            {
                "type": "index_not_found_exception",
                "reason": "no such index",
                "resource.type": "index_or_alias",
                "resource.id": "index3",
                "index_uuid": "_na_",
                "index": "index3"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index",
        "resource.type": "index_or_alias",
        "resource.id": "index3",
        "index_uuid": "_na_",
        "index": "index3"
    },
    "status": 404
}

一个显而易见的方法是检查索引是否已经存在,但我想避免额外的呼叫 注意:始终存在至少1个索引 是否有可能避免这种例外?
提前致谢 !!

1 个答案:

答案 0 :(得分:1)

“ignore_unavailable”就是解决方案。将其作为搜索网址中的查询参数传递。 EXA。 http://localhost:9200/index1,index2/type/_search?ignore_unavailable

即使其中任何一个指数不存在,也不会给出