elasticsearch之多索引查詢

無風聽海發表於2021-12-31

一、問題源起

在elasticsearch的查詢中,我們一般直接通過URL來設定要search的index; 如果我們需要查詢的索引比較多並且沒有什麼規律的話,就會面臨一個尷尬的局面,超過URL的長度限制;

二、測試環境

elasticsearch 6.8.12

測試資料

新增三個測試的index,每個index裡邊一個document;

PUT test1/_doc/1
{
  "id":1,
  "name":"test1-1"
}


# {
#   "_index" : "test1",
#   "_type" : "_doc",
#   "_id" : "1",
#   "_version" : 1,
#   "result" : "created",
#   "_shards" : {
#     "total" : 2,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "_seq_no" : 0,
#   "_primary_term" : 1
# }

PUT test2/_doc/1
{
  "id":1,
  "name":"test2-1"
}


# {
#   "_index" : "test2",
#   "_type" : "_doc",
#   "_id" : "1",
#   "_version" : 1,
#   "result" : "created",
#   "_shards" : {
#     "total" : 2,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "_seq_no" : 0,
#   "_primary_term" : 1
# }

PUT test3/_doc/1
{
  "id":1,
  "name":"test3-1"
}

# {
#   "_index" : "test3",
#   "_type" : "_doc",
#   "_id" : "1",
#   "_version" : 1,
#   "result" : "created",
#   "_shards" : {
#     "total" : 2,
#     "successful" : 1,
#     "failed" : 0
#   },
#   "_seq_no" : 0,
#   "_primary_term" : 1
# }

三、URL中指定multi index

直接在URL中指定搜尋特定的index

POST test1/_search 
{
    "query": {
        "match_all": {}
    }
}


# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 5,
#     "successful" : 5,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 1,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       }
#     ]
#   }
# }

可以通過都好分割同時搜尋多個index;

POST test1,test2/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 1,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }

我們可以使用關鍵字_all指定搜尋所有的index;

POST _all/_search 
{
    "query": {
        "match_all": {}
    }
}

{
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 15,
#     "successful" : 15,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 3,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       },
#       {
#         "_index" : "test3",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test3-1"
#         }
#       }
#     ]
#   }
# }

也可以使用萬用字元*來匹配一些名字有共同特徵的index;

POST test*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 1,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 15,
#     "successful" : 15,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 3,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       },
#       {
#         "_index" : "test3",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test3-1"
#         }
#       }
#     ]
#   }
# }

還可以使用-來排除某個index;

POST test*,-test2/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test3",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test3-1"
#         }
#       }
#     ]
#   }
# }

四、URL中multi index的一些控制選項

如果我們顯示search一個不存在的或者關閉的index就會報錯;

POST test4/_search
{
    "query": {
        "match_all": {}
    }
}


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

POST test3/_close
# 
# {
#   "acknowledged" : true
# }

POST test3/_search
{
    "query": {
        "match_all": {}
    }
}


# {
#   "error": {
#     "root_cause": [
#       {
#         "type": "index_closed_exception",
#         "reason": "closed",
#         "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#         "index": "test3"
#       }
#     ],
#     "type": "index_closed_exception",
#     "reason": "closed",
#     "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#     "index": "test3"
#   },
#   "status": 400
# }

我們可以使用ignore_unavailable來忽略不存在或者關閉的index;


POST test4/_search?ignore_unavailable=true
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }


POST test3/_search?ignore_unavailable=true
{
    "query": {
        "match_all": {}
    }
}


# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }

如果通過萬用字元、_all隱式的指定search的index,如果不存在則預設不會報錯,不過可以通過allow_no_indices=false來讓elasticsearch報錯;

POST noexist*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }


POST noexist*/_search?allow_no_indices=false
{
    "query": {
        "match_all": {}
    }
}

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



POST test3*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 0,
#     "successful" : 0,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 0,
#     "max_score" : 0.0,
#     "hits" : [ ]
#   }
# }

POST test3*/_search?allow_no_indices=false
{
    "query": {
        "match_all": {}
    }
}

# {
#   "error" : {
#     "root_cause" : [
#       {
#         "type" : "index_not_found_exception",
#         "reason" : "no such index",
#         "resource.type" : "index_or_alias",
#         "resource.id" : "test3*"
#       }
#     ],
#     "type" : "index_not_found_exception",
#     "reason" : "no such index",
#     "resource.type" : "index_or_alias",
#     "resource.id" : "test3*"
#   },
#   "status" : 404
# }


我們也可以使用expand_wildcards來控制展開哪些index,可選值open、closed、none、all;

預設只擴充套件open;

POST test*/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }


POST test*/_search?expand_wildcards=all
{
    "query": {
        "match_all": {}
    }
}

# {
#   "error": {
#     "root_cause": [
#       {
#         "type": "index_closed_exception",
#         "reason": "closed",
#         "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#         "index": "test3"
#       }
#     ],
#     "type": "index_closed_exception",
#     "reason": "closed",
#     "index_uuid": "KI7Iv4eGRIOk6MsycXokNQ",
#     "index": "test3"
#   },
#   "status": 400
# }

POST test*/_search?expand_wildcards=all&ignore_unavailable=true
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }

五、使用index aliases封裝物理index

aliases是物理索引的別名,請求api的時候,elasticsearch會自動將aliases轉化為對應的物理index name;

別名既可以對映到某個特定的index,也可以對映到多個index;

別名也可以同時應用過濾條件,實現只對index的區域性資料進行搜尋;

POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}

# {
#   "acknowledged" : true
# }

POST all_test_indices/_search
{
    "query": {
        "match_all": {}
    }
}

# {
#   "took" : 0,
#   "timed_out" : false,
#   "_shards" : {
#     "total" : 10,
#     "successful" : 10,
#     "skipped" : 0,
#     "failed" : 0
#   },
#   "hits" : {
#     "total" : 2,
#     "max_score" : 1.0,
#     "hits" : [
#       {
#         "_index" : "test1",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test1-1"
#         }
#       },
#       {
#         "_index" : "test2",
#         "_type" : "_doc",
#         "_id" : "1",
#         "_score" : 1.0,
#         "_source" : {
#           "id" : 1,
#           "name" : "test2-1"
#         }
#       }
#     ]
#   }
# }

六、multi search--通過body指定index

Multi Search API的主要目的是實現在一個API裡邊實現多個search請求,其通過如下格式分別通過header指定index,body指定查詢語句;

header\n
body\n
header\n
body\n

Multi Search API除了與前兩者具有相同的指定index name的能力,最大的優勢就是通過body傳遞index name,輕鬆突破URL的長度限制的侷限性;

還有一點就是Multi Search API支援大量的沒有特定規律的index name,例如跟時間序列有關的index name等;

GET _msearch
{"index":"test*"}
{"query" : {"match_all" : {}}}

# {
#   "responses" : [
#     {
#       "took" : 0,
#       "timed_out" : false,
#       "_shards" : {
#         "total" : 10,
#         "successful" : 10,
#         "skipped" : 0,
#         "failed" : 0
#       },
#       "hits" : {
#         "total" : 2,
#         "max_score" : 1.0,
#         "hits" : [
#           {
#             "_index" : "test1",
#             "_type" : "_doc",
#             "_id" : "1",
#             "_score" : 1.0,
#             "_source" : {
#               "id" : 1,
#               "name" : "test1-1"
#             }
#           },
#           {
#             "_index" : "test2",
#             "_type" : "_doc",
#             "_id" : "1",
#             "_score" : 1.0,
#             "_source" : {
#               "id" : 1,
#               "name" : "test2-1"
#             }
#           }
#         ]
#       },
#       "status" : 200
#     }
#   ]
# }

相關文章