SOFTELメモ Developer's blog

会社概要 ブログ 調査依頼 採用情報 ...
てるてる動画

Elasticsearchする

問題

Elastic Search 使ってみたいです。

どうやってインストールするの?どうやって使うの?

答え

docker で環境を作るのをよく見かけますが、rpmでもサクッとインストールできる。

java8 が必要

CentOS6でも1.8がちゃんと入ってくる。CentOS8でも同様だった。

yum install java

Elasticsearchインストール

公式rpmでインストールできる。複雑な依存関係も特にない様子。

yum install https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.0-x86_64.rpm

プラグイン追加。日本語の検索をするなら。

/usr/share/elasticsearch/bin/elasticsearch-plugin install analysis-kuromoji

Elasticsearch設定

ローカルで使用するならそのまま起動してもよい。

別サーバーからアクセスするつもりなら以下のような設定の追加をするとよい。

vi /etc/elasticsearch/elasticsearch.yml
network.host: 0.0.0.0
http.port: 9200
transport.host: localhost
transport.tcp.port: 9300

こちらの設定ファイル /etc/elasticsearch/elasticsearch.yml 初期状態ではすべてコメントなので、該当箇所を書き換えるもよいし、末尾に追記でも大丈夫。

Elasticsearch起動

あっさり起動します。

service elasticsearch start

もしくは

systemctl start elasticsearch

Elasticsearchを使う

Elasticsearchと適当に会話する。

URL に ?pretty がついているが、人間にとって見やすく整形してくれるオプションなので、普段使うときは外してよい。

・Nodeの情報ください

$ curl -X GET 'http://localhost:9200/_nodes?pretty'
{
  "_nodes" : {
    "total" : 1,
    "successful" : 1,
    "failed" : 0
  },
  "cluster_name" : "elasticsearch",
  "nodes" : {
    "E9U91tXPSwqn4UdotIMo8Q" : {
      "name" : "juchumail-db",
      "transport_address" : "127.0.0.1:9300",
      "host" : "localhost",
      "ip" : "127.0.0.1",
      "version" : "7.6.0",
      "build_flavor" : "default",
      "build_type" : "rpm",
(以下略)

・レコード(document)を作る。

indexは自動的に作成されるそうなので、いきなり投入可能。

curlコマンドで、customer index に顧客データを投入。

$ curl -X POST http://localhost:9200/customer/_doc/ -H "Content-Type: application/json" -d '
{
    "name": "Gifu Taro",
    "birthday": "2000-01-01",
    "jikoshokai": "犬が好き。猫も好き。",
    "tags": ["red", "green", "blue"]
}
'

結果

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

index作成のためか、1回目は一瞬待たされましたが、2回目以降はサクサク入ります。

$ curl -X POST http://localhost:9200/customer/_doc/ -H "Content-Type: application/json" -d '
{
    "name": "Nagoya Hanako",
    "birthday": "2000-02-02",
    "jikoshokai": "花が好き。草も好き。テストデータです。テストデータです。",
    "tags": ["red", "green", "blue"]
}
'
{"_index":"customer","_type":"_doc","_id":"J-2QanABDcj0ED5yQ_eb","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
$ curl -X POST http://localhost:9200/customer/_doc/ -H "Content-Type: application/json" -d '
{
    "name": "Tokai Jiro",
    "birthday": "2000-03-03",
    "jikoshokai": "エビが好き。カニも好き。テストデータです。",
    "tags": ["red", "green", "blue"]
}
'
{"_index":"customer","_type":"_doc","_id":"KO2QanABDcj0ED5yePfi","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}

・検索する

クエリなし

curl -X GET http://localhost:9200/customer/_search -H "Content-Type: application/json"

キーワード「猫(%E7%8C%AB)」

$ curl -X GET http://localhost:9200/customer/_search?q=%E7%8C%AB

{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.1040031,"hits":[{"_index":"customer","_type":"_doc","_id":"Ju2GanABDcj0ED5y7fdz","_score":1.1040031,"_source":
{
    "name": "Gifu Taro",
    "birthday": "2000-01-01",
    "jikoshokai": "犬が好き。猫も好き。",
    "tags": ["red", "green", "blue"]
}

ソート(誕生日の降順)

$ curl -X GET  http://localhost:9200/customer/_search?sort=birthday:desc
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":null,"hits":[{"_index":"customer","_type":"_doc","_id":"KO2QanABDcj0ED5yePfi","_score":null,"_source":
{
    "name": "Tokai Jiro",
    "birthday": "2000-03-03",
    "jikoshokai": "エビが好き。カニも好き。テストデータです。",
    "tags": ["red", "green", "blue"]
}
,"sort":[952041600000]},{"_index":"customer","_type":"_doc","_id":"J-2QanABDcj0ED5yQ_eb","_score":null,"_source":
{
    "name": "Nagoya Hanako",
    "birthday": "2000-02-02",
    "jikoshokai": "花が好き。草も好き。テストデータです。テストデータです。",
    "tags": ["red", "green", "blue"]
}
,"sort":[949449600000]},{"_index":"customer","_type":"_doc","_id":"Ju2GanABDcj0ED5y7fdz","_score":null,"_source":
{
    "name": "Gifu Taro",
    "birthday": "2000-01-01",
    "jikoshokai": "犬が好き。猫も好き。",
    "tags": ["red", "green", "blue"]
}

起動、データ投入、検索まで、とても簡単にできた。

データ全削除

テスト中にデータをリセットしたくなったら、以下できれいに消える。

$ curl -X DELETE  'http://localhost:9200/*'

特定のindexのみなど部分的に削除も可能。

$ curl -X DELETE  'http://localhost:9200/customer'

郵便番号データを投入してさらに動作を確認する

データがたくさん入ったCSVということで、日本郵便の郵便番号データを利用して。ちょっとしたスクリプトを書いて、Elasticsearchに投入しました。

match で検索をかけるとあいまい検索になり、だんだんGoogle検索っぽい動きになってきます。

キーワード「南北(\u5357\u5317)」で検索すると

レスポンスは即答で帰ってくるのですが、MySQLの LIKE ‘%南北%’ を期待していると期待を裏切られます。

北野南、上北南などがヒットします。

$ curl -X POST http://localhost:9200/postcode/_search?pretty -H "Content-Type: application/json" -d '{
  "query": {
    "match": {
      "cho": {
        "query":"\u5357\u5317"
      }
    }
  }
}'
{
  "took" : 13,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 8486,
      "relation" : "eq"
    },
    "max_score" : 7.3872533,
    "hits" : [
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "ITbhanABo_MoE6hcJ9bB",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "03201",
          "oldpostalcode" : "020  ",
          "postalcode" : "0200863",
          "ken_kana" : "イワテケン",
          "shi_kana" : "モリオカシ",
          "cho_kana" : "ミナミセンボク",
          "ken" : "岩手県",
          "shi" : "盛岡市",
          "cho" : "南仙北",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "GTfhanABo_MoE6hc_iZB",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "11208",
          "oldpostalcode" : "359  ",
          "postalcode" : "3591156",
          "ken_kana" : "サイタマケン",
          "shi_kana" : "トコロザワシ",
          "cho_kana" : "キタノミナミ",
          "ken" : "埼玉県",
          "shi" : "所沢市",
          "cho" : "北野南",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "2DbhanABo_MoE6hcIdOa",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "02408",
          "oldpostalcode" : "03924",
          "postalcode" : "0392405",
          "ken_kana" : "アオモリケン",
          "shi_kana" : "カミキタグントウホクマチ",
          "cho_kana" : "カミキタミナミ",
          "ken" : "青森県",
          "shi" : "上北郡東北町",
          "cho" : "上北南",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "_zbhanABo_MoE6hcV-fP",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "04341",
          "oldpostalcode" : "98121",
          "postalcode" : "9812187",
          "ken_kana" : "ミヤギケン",
          "shi_kana" : "イググンマルモリマチ",
          "cho_kana" : "キタハラミナミ",
          "ken" : "宮城県",
          "shi" : "伊具郡丸森町",
          "cho" : "北原南",
          "a" : "0",
          "b" : "0",
          "c" : "0",
          "d" : "1",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "9DfjanABo_MoE6hcJ5Q2",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "21201",
          "oldpostalcode" : "50125",
          "postalcode" : "5012516",
          "ken_kana" : "ギフケン",
          "shi_kana" : "ギフシ",
          "cho_kana" : "キタノミナミ",
          "ken" : "岐阜県",
          "shi" : "岐阜市",
          "cho" : "北野南",
          "a" : "0",
          "b" : "0",
          "c" : "0",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "ijfkanABo_MoE6hcLveI",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "27125",
          "oldpostalcode" : "559  ",
          "postalcode" : "5590034",
          "ken_kana" : "オオサカフ",
          "shi_kana" : "オオサカシスミノエク",
          "cho_kana" : "ナンコウキタ",
          "ken" : "大阪府",
          "shi" : "大阪市住之江区",
          "cho" : "南港北",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "BzjkanABo_MoE6hcZw3x",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "28207",
          "oldpostalcode" : "664  ",
          "postalcode" : "6640887",
          "ken_kana" : "ヒョウゴケン",
          "shi_kana" : "イタミシ",
          "cho_kana" : "ミナミノキタ",
          "ken" : "兵庫県",
          "shi" : "伊丹市",
          "cho" : "南野北",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "jTjkanABo_MoE6hc-UOC",
        "_score" : 7.3872533,
        "_source" : {
          "jichitai" : "35202",
          "oldpostalcode" : "75902",
          "postalcode" : "7590209",
          "ken_kana" : "ヤマグチケン",
          "shi_kana" : "ウベシ",
          "cho_kana" : "コウナンキタ",
          "ken" : "山口県",
          "shi" : "宇部市",
          "cho" : "厚南北",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "wTbganABo_MoE6hc-8XC",
        "_score" : 6.609371,
        "_source" : {
          "jichitai" : "01631",
          "oldpostalcode" : "08001",
          "postalcode" : "0800306",
          "ken_kana" : "ホッカイドウ",
          "shi_kana" : "カトウグンオトフケチョウ",
          "cho_kana" : "キタスズランミナミ",
          "ken" : "北海道",
          "shi" : "河東郡音更町",
          "cho" : "北鈴蘭南",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      },
      {
        "_index" : "postcode",
        "_type" : "_doc",
        "_id" : "DTbganABo_MoE6hc_MaL",
        "_score" : 6.609371,
        "_source" : {
          "jichitai" : "01631",
          "oldpostalcode" : "08001",
          "postalcode" : "0800311",
          "ken_kana" : "ホッカイドウ",
          "shi_kana" : "カトウグンオトフケチョウ",
          "cho_kana" : "ミナミスズランキタ",
          "ken" : "北海道",
          "shi" : "河東郡音更町",
          "cho" : "南鈴蘭北",
          "a" : "0",
          "b" : "0",
          "c" : "1",
          "d" : "0",
          "e" : "0",
          "f" : "0"
        }
      }
    ]
  }
}

このあたりの動作は、テーブル定義っぽい役目のマッピング(Mapping)によって変わる。アナライザーによって変わる。

マッピング

インデックスのmappingについて問い合わせると

$ curl -X GET http://localhost:9200/postcode/_mapping?pretty

以下のようになっている。こうなっていると上の「南北」検索のような結果になる。

(前略)
        "cho" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
(後略)

フィールドの型やアナライザなどを指定してデータを入れると結果が変わる。

以下だと1文字ずつバラされるのに対して

GET /customer/_analyze
{
    "field": "text",
    "text" : "昔々あるところにおじいさんとおばあさんがいました"
}

→ 昔, 々, あ, る, と, こ, ろ, に …

以下のように、アナライザーにkuromojiを指定すると、単語や文節で切ってくれる。

PUT kuromoji_sample
{
  "settings": {
    "index": {
      "analysis": {
        "analyzer": {
          "my_analyzer": {
            "tokenizer": "kuromoji_tokenizer",
            "filter": [
              "kuromoji_baseform"
            ]
          }
        }
      }
    }
  }
}
GET /kuromoji_sample/_analyze
{
"analyzer": "my_analyzer",
"text" : "昔々,あるところにおじいさんとおばあさんがいました。桃が流れてきました"
}

→ 昔, 々, ある, ところ, に, おじいさん, …

・集計する

GET /postalcode/_search
{
  "size": 0,
  "aggs": {
    "total_value_count": {
      "value_count": {
        "field": "ken.keyword"
      }
    }
  }
}
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "total_value_count" : {
      "value" : 124395
    }
  }
}

郵便番号データは 124395件と出た。

・集計する2

GET /postalcode/_search
{
  "size": 0,
  "aggs": {
    "total_value_count": {
      "value_count": {
        "field": "postalcode.keyword"
      }
    },
    "group_by_ken": {
      "terms": {
        "field": "ken.keyword",
        "size":50
      },
      "aggs": {
        "count_ken_goto": {
          "value_count": {
            "field": "postalcode.keyword"
          }
        },
        "shi_ruiji": {
          "cardinality": {
            "field": "postalcode.keyword"
          }
        }
      }
    }
  }
}

県ごとの郵便番号の件数が出た。

{
  "took" : 102,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "total_value_count" : {
      "value" : 124395
    },
    "group_by_ken" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "北海道",
          "doc_count" : 8252,
          "count_ken_goto" : {
            "value" : 8252
          },
          "shi_ruiji" : {
            "value" : 8037
          }
        },
        {
          "key" : "愛知県",
          "doc_count" : 7713,
          "count_ken_goto" : {
            "value" : 7713
          },
          "shi_ruiji" : {
            "value" : 7231
          }
        },
        {
          "key" : "京都府",
          "doc_count" : 6660,
          "count_ken_goto" : {
            "value" : 6660
          },
          "shi_ruiji" : {
            "value" : 6456
          }
        },
        {
          "key" : "新潟県",
          "doc_count" : 5448,
          "count_ken_goto" : {
            "value" : 5448
          },
          "shi_ruiji" : {
            "value" : 5347
          }
        },
        {
          "key" : "兵庫県",
          "doc_count" : 5223,
          "count_ken_goto" : {
            "value" : 5223
          },
          "shi_ruiji" : {
            "value" : 5191
          }
        },
        {
          "key" : "東京都",
          "doc_count" : 3972,
          "count_ken_goto" : {
            "value" : 3972
          },
          "shi_ruiji" : {
            "value" : 3990
          }
        },
        {
          "key" : "福島県",
          "doc_count" : 3953,
          "count_ken_goto" : {
            "value" : 3953
          },
          "shi_ruiji" : {
            "value" : 3583
          }
        },
        {
          "key" : "大阪府",
          "doc_count" : 3849,
          "count_ken_goto" : {
            "value" : 3849
          },
          "shi_ruiji" : {
            "value" : 3818
          }
        },
        {
          "key" : "千葉県",
          "doc_count" : 3602,
          "count_ken_goto" : {
            "value" : 3602
          },
          "shi_ruiji" : {
            "value" : 3598
          }
        },
        {
          "key" : "宮城県",
          "doc_count" : 3391,
          "count_ken_goto" : {
            "value" : 3391
          },
          "shi_ruiji" : {
            "value" : 2843
          }
        },
        {
          "key" : "岐阜県",
          "doc_count" : 3372,
          "count_ken_goto" : {
            "value" : 3372
          },
          "shi_ruiji" : {
            "value" : 3369
          }
        },
        {
          "key" : "福岡県",
          "doc_count" : 3297,
          "count_ken_goto" : {
            "value" : 3297
          },
          "shi_ruiji" : {
            "value" : 3273
          }
        },
        {
          "key" : "富山県",
          "doc_count" : 3258,
          "count_ken_goto" : {
            "value" : 3258
          },
          "shi_ruiji" : {
            "value" : 2977
          }
        },
        {
          "key" : "埼玉県",
          "doc_count" : 2959,
          "count_ken_goto" : {
            "value" : 2959
          },
          "shi_ruiji" : {
            "value" : 2950
          }
        },
        {
          "key" : "静岡県",
          "doc_count" : 2948,
          "count_ken_goto" : {
            "value" : 2948
          },
          "shi_ruiji" : {
            "value" : 2880
          }
        },
        {
          "key" : "茨城県",
          "doc_count" : 2858,
          "count_ken_goto" : {
            "value" : 2858
          },
          "shi_ruiji" : {
            "value" : 2856
          }
        },
        {
          "key" : "石川県",
          "doc_count" : 2562,
          "count_ken_goto" : {
            "value" : 2562
          },
          "shi_ruiji" : {
            "value" : 2472
          }
        },
        {
          "key" : "青森県",
          "doc_count" : 2516,
          "count_ken_goto" : {
            "value" : 2516
          },
          "shi_ruiji" : {
            "value" : 2031
          }
        },
        {
          "key" : "三重県",
          "doc_count" : 2478,
          "count_ken_goto" : {
            "value" : 2478
          },
          "shi_ruiji" : {
            "value" : 2454
          }
        },
        {
          "key" : "神奈川県",
          "doc_count" : 2296,
          "count_ken_goto" : {
            "value" : 2296
          },
          "shi_ruiji" : {
            "value" : 2293
          }
        },
        {
          "key" : "福井県",
          "doc_count" : 2263,
          "count_ken_goto" : {
            "value" : 2263
          },
          "shi_ruiji" : {
            "value" : 2130
          }
        },
        {
          "key" : "岡山県",
          "doc_count" : 2190,
          "count_ken_goto" : {
            "value" : 2190
          },
          "shi_ruiji" : {
            "value" : 2180
          }
        },
        {
          "key" : "広島県",
          "doc_count" : 2161,
          "count_ken_goto" : {
            "value" : 2161
          },
          "shi_ruiji" : {
            "value" : 2123
          }
        },
        {
          "key" : "秋田県",
          "doc_count" : 2158,
          "count_ken_goto" : {
            "value" : 2158
          },
          "shi_ruiji" : {
            "value" : 2153
          }
        },
        {
          "key" : "岩手県",
          "doc_count" : 2074,
          "count_ken_goto" : {
            "value" : 2074
          },
          "shi_ruiji" : {
            "value" : 1915
          }
        },
        {
          "key" : "山形県",
          "doc_count" : 1958,
          "count_ken_goto" : {
            "value" : 1958
          },
          "shi_ruiji" : {
            "value" : 1947
          }
        },
        {
          "key" : "奈良県",
          "doc_count" : 1937,
          "count_ken_goto" : {
            "value" : 1937
          },
          "shi_ruiji" : {
            "value" : 1907
          }
        },
        {
          "key" : "熊本県",
          "doc_count" : 1897,
          "count_ken_goto" : {
            "value" : 1897
          },
          "shi_ruiji" : {
            "value" : 1861
          }
        },
        {
          "key" : "長崎県",
          "doc_count" : 1894,
          "count_ken_goto" : {
            "value" : 1894
          },
          "shi_ruiji" : {
            "value" : 1892
          }
        },
        {
          "key" : "大分県",
          "doc_count" : 1851,
          "count_ken_goto" : {
            "value" : 1851
          },
          "shi_ruiji" : {
            "value" : 1840
          }
        },
        {
          "key" : "滋賀県",
          "doc_count" : 1850,
          "count_ken_goto" : {
            "value" : 1850
          },
          "shi_ruiji" : {
            "value" : 1838
          }
        },
        {
          "key" : "栃木県",
          "doc_count" : 1834,
          "count_ken_goto" : {
            "value" : 1834
          },
          "shi_ruiji" : {
            "value" : 1831
          }
        },
        {
          "key" : "山口県",
          "doc_count" : 1813,
          "count_ken_goto" : {
            "value" : 1813
          },
          "shi_ruiji" : {
            "value" : 1656
          }
        },
        {
          "key" : "愛媛県",
          "doc_count" : 1750,
          "count_ken_goto" : {
            "value" : 1750
          },
          "shi_ruiji" : {
            "value" : 1742
          }
        },
        {
          "key" : "高知県",
          "doc_count" : 1695,
          "count_ken_goto" : {
            "value" : 1695
          },
          "shi_ruiji" : {
            "value" : 1690
          }
        },
        {
          "key" : "長野県",
          "doc_count" : 1684,
          "count_ken_goto" : {
            "value" : 1684
          },
          "shi_ruiji" : {
            "value" : 1666
          }
        },
        {
          "key" : "和歌山県",
          "doc_count" : 1601,
          "count_ken_goto" : {
            "value" : 1601
          },
          "shi_ruiji" : {
            "value" : 1597
          }
        },
        {
          "key" : "群馬県",
          "doc_count" : 1501,
          "count_ken_goto" : {
            "value" : 1501
          },
          "shi_ruiji" : {
            "value" : 1499
          }
        },
        {
          "key" : "鹿児島県",
          "doc_count" : 1459,
          "count_ken_goto" : {
            "value" : 1459
          },
          "shi_ruiji" : {
            "value" : 1449
          }
        },
        {
          "key" : "徳島県",
          "doc_count" : 1426,
          "count_ken_goto" : {
            "value" : 1426
          },
          "shi_ruiji" : {
            "value" : 961
          }
        },
        {
          "key" : "鳥取県",
          "doc_count" : 1397,
          "count_ken_goto" : {
            "value" : 1397
          },
          "shi_ruiji" : {
            "value" : 1391
          }
        },
        {
          "key" : "島根県",
          "doc_count" : 1183,
          "count_ken_goto" : {
            "value" : 1183
          },
          "shi_ruiji" : {
            "value" : 1179
          }
        },
        {
          "key" : "山梨県",
          "doc_count" : 950,
          "count_ken_goto" : {
            "value" : 950
          },
          "shi_ruiji" : {
            "value" : 945
          }
        },
        {
          "key" : "宮崎県",
          "doc_count" : 877,
          "count_ken_goto" : {
            "value" : 877
          },
          "shi_ruiji" : {
            "value" : 876
          }
        },
        {
          "key" : "佐賀県",
          "doc_count" : 872,
          "count_ken_goto" : {
            "value" : 872
          },
          "shi_ruiji" : {
            "value" : 870
          }
        },
        {
          "key" : "沖縄県",
          "doc_count" : 801,
          "count_ken_goto" : {
            "value" : 801
          },
          "shi_ruiji" : {
            "value" : 799
          }
        },
        {
          "key" : "香川県",
          "doc_count" : 712,
          "count_ken_goto" : {
            "value" : 712
          },
          "shi_ruiji" : {
            "value" : 709
          }
        }
      ]
    }
  }
}

・集計する3

queryも指定できるので、一部の集計も可能。

GET /postalcode/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "ken.keyword": ["岐阜県", "愛知県", "三重県"]
        }
      }
    }
  },
  "aggs": {
    "total_value_count": {
      "value_count": {
        "field": "postalcode.keyword"
      }
    },
    "group_by_ken": {
      "terms": {
        "field": "ken.keyword",
        "size":50
      },
      "aggs": {
        "count_ken_goto": {
          "value_count": {
            "field": "postalcode.keyword"
          }
        },
        "shi_ruiji": {
          "cardinality": {
            "field": "postalcode.keyword"
          }
        }
      }
    }
  }
}
{
  "took" : 19,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "total_value_count" : {
      "value" : 13563
    },
    "group_by_ken" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "愛知県",
          "doc_count" : 7713,
          "count_ken_goto" : {
            "value" : 7713
          },
          "shi_ruiji" : {
            "value" : 7231
          }
        },
        {
          "key" : "岐阜県",
          "doc_count" : 3372,
          "count_ken_goto" : {
            "value" : 3372
          },
          "shi_ruiji" : {
            "value" : 3369
          }
        },
        {
          "key" : "三重県",
          "doc_count" : 2478,
          "count_ken_goto" : {
            "value" : 2478
          },
          "shi_ruiji" : {
            "value" : 2454
          }
        }
      ]
    }
  }
}

メモ

・以下のエラーが出たら

Can't update non dynamic settings

openな状態ではインデックスの設定を変更できない。closeして、設定変更して、openする。

例えば、インデックスにアナライザーの追加。

POST /customer/_close
{
}
PUT /customer/_settings
{
  "analysis": {
    "analyzer": {
      "my_analyzer": {
        "tokenizer": "kuromoji_tokenizer",
        "filter": [
          "kuromoji_baseform"
        ]
      }
    }
  }
}
POST /customer/_open
{
}

・マッピングの追加

PUT /customer/_mapping
{
  "properties": {
    "jikoshokai": {
      "type": "text",
      "fields": {
        "keyword": { 
          "type":     "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}

・マッピングを変更できないときは、別indexを作って、コピー(_reindex)

POST /_reindex
{
  "source": {
    "index": "customer"
  },
  "dest": {
    "index": " customer_new"
  }
}

関連するメモ

コメント