4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Elasticsearch] 複数インデックスへの検索

Posted at

Elasticsearch導入時にインデックス設計段階にて下記を確認したかったので、確認した内容。

  • Elasticsearchにて複数インデックスへの検索ができるか?
  • できるなら、そのやり方は。
  • レスポンスのデータ構造がどうなるか
  • 設計段階での注意点は何か

version

  • elasticsearch:7.6.2
  • kibana:7.6.2

実行の流れ

Kibana の Consoleで実行していく。

テストデータ投入

# テストデータをmessagesインデックスに入れる
POST /messages/_doc/
{
  "properties":{
    "channel_id": 4,
    "content": "トルトル"
  }
}

# テストデータをtitlesインデックスに入れる
POST /titles/_doc/
{
  "properties":{
    "channel_id": 1,
    "title": "タイトル1"
  }
}

検索

# 検索
GET /messages,titles/_search
{
  "query":{
    "bool":{
      "should": [
        { "wildcard": { "properties.title": "*トル" } },
        { "wildcard": { "properties.content": "*トル" } }
      ]
    }
  }
}

# => 結果のJSONデータ messagesインデックス、titleインデックスからデータを検索できている
{
  "hits" : {
    // ...
    "hits" : [
      {
        "_index" : "messages",
        "_type" : "_doc",
        "_id" : "VfjKv3EBFmjJ_Mf0kBdQ",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 4,
            "content" : "トルトル"
          }
        }
      },
      {
        "_index" : "titles",
        "_type" : "_doc",
        "_id" : "VvjKv3EBFmjJ_Mf0rBch",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 1,
            "title" : "タイトル1"
          }
        }
      }
    ]
  }
}

その他:エイリアスを設定する

今回のケースのように複数のインデックスからの検索を行うケースの場合は事前にエイリアスを設定しておくこと。

なぜなら、検索対象インデックスの増えたとき

  • エイリアスを設定していないと、変更範囲がElasticsearchのレイヤーだけではなくてアプリケーションレイヤーにまで影響が出てしまうため。
  • エイリアスを設定していれば、Elasticsearch側にてインデックスをエイリアスに新しく追加するだけの閉じた影響範囲になる。

実際にエイリアスの設定と検索をしてみる。

# エイリアス設定: searchableエイリアスを作成する
POST /_aliases/
{
  "actions": [
    { "add": { "indices": [ "messages", "titles"],  "alias": "searchable" } }
  ]
}

# 検索: インデックスと同じようにsearchableエイリアスを対象に検索する
GET /searchable/_search
{
  "query":{
    "bool":{
      "should": [
        { "wildcard": { "properties.title": "*トル" } },
        { "wildcard": { "properties.content": "*トル" } }
      ]
    }
  }
}

=> 結果はインデックスもエイリアスも変わらない
{
  "hits" : {
    // ...
    "hits" : [
      {
        "_index" : "messages",
        "_type" : "_doc",
        "_id" : "VfjKv3EBFmjJ_Mf0kBdQ",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 4,
            "content" : "トルトル"
          }
        }
      },
      {
        "_index" : "titles",
        "_type" : "_doc",
        "_id" : "VvjKv3EBFmjJ_Mf0rBch",
        "_score" : 1.0,
        "_source" : {
          "properties" : {
            "channel_id" : 1,
            "title" : "タイトル1"
          }
        }
      }
    ]
  }
}

REF

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?