ElasticSearchのAPIからJSONデータのインポートとfuzzy_like_thisなどで検索してみた
ElasticSearchのセットアップ 覚えるのめんどくさいし、Amazonからマネージドサービスが提供されているのでそれを使ってパッと立ち上げちゃいます。 ちゃんと設定したい方はクラスメソッドのブログなどを参考に頑張 […]
目次
ElasticSearchのセットアップ
覚えるのめんどくさいし、Amazonからマネージドサービスが提供されているのでそれを使ってパッと立ち上げちゃいます。
ちゃんと設定したい方はクラスメソッドのブログなどを参考に頑張ってください。
今回はこの記事書いてる間だけAPIが使えればいいので、全力で適当に設定してます。
ドメイン名の設定

ドメイン名を指定します。
インスタンスサイズなどの設定

インスタンスサイズなどの設定をします。
t2.micro1つが1年間無料枠対象なので、それを使いましょう。
必要に応じてEBSの設定などをする

t2.microはEBSを指定する必要があるので選択します。
他は特に触りません。
アクセス許可の設定を適当にする

アクセス許可の設定をします。
どうせすぐにインスタンスごと削除するので、全許可しときます。実運用では絶対にやらないでください。
立ち上げる

確認画面が出るので、立ち上げます。

リストに出たら完了です。「Active」になるまで待ちます。
テストデータを投入する
はい。立ち上がりました。
https://search-DOMAIN-NAME-XXXX.ap-northeast-1.es.amazonaws.com
のようなエンドポイントができますので、ここにcURLからデータを叩き込んだり検索したりしてみます。
まずはElasticSearchのサイトに載っているテストデータを入れます。
「You can download the sample dataset (accounts.json) from here.」という部分からサンプルのJSONをDLできるので、落とします。
そして黒い画面(ターミナル)からElasticSearchにデータを投入します。
$ curl -XPUT https://search-DOMAIN-NAME-XXXX.ap-northeast-1.es.amazonaws.com/bank/account/_bulk?pretty' --data-binary "@accounts.json"

こんな表示が出ればOK
検索する
ElasticSearchは検索サービスですので、早速検索してみましょう。
$ curl -XPUT https://search-DOMAIN-NAME-XXXX.ap-northeast-1.es.amazonaws.com/bank/_search?query=* | jq .
先ほど投入したデータがJSON形式で返ってきました。
中身を見るとどうやら住所や氏名などの情報が入ったデータのようです。
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 42,
"max_score": 2.041331,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "670",
"_score": 2.041331,
"_source": {
"account_number": 670,
"balance": 10178,
"firstname": "Ollie",
"lastname": "Riley",
"age": 22,
"gender": "M",
"address": "252 Jackson Place",
"employer": "Adornica",
"email": "[email protected]",
"city": "Brethren",
"state": "WI"
}
},
ということでElasticSearchらしい検索をやってみます。
stateがWIもしくはNDのデータを3件取得する
sizeで件数を、query[match]でkeyのデータからスペース区切りで検索キーワードを指定できます。
% curl -XPOST https://search-DOMAIN-NAME-XXXX.ap-northeast-1.es.amazonaws.com/bank/_search -d '{
"size":3,
"query":{
"match":{
"state":"ND WI"
}
}
}'
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 42,
"max_score": 2.041331,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "670",
"_score": 2.041331,
"_source": {
"account_number": 670,
"balance": 10178,
"firstname": "Ollie",
"lastname": "Riley",
"age": 22,
"gender": "M",
"address": "252 Jackson Place",
"employer": "Adornica",
"email": "[email protected]",
"city": "Brethren",
"state": "WI"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "719",
"_score": 2.041331,
"_source": {
"account_number": 719,
"balance": 33107,
"firstname": "Leanna",
"lastname": "Reed",
"age": 25,
"gender": "F",
"address": "528 Krier Place",
"employer": "Rodeology",
"email": "[email protected]",
"city": "Carrizo",
"state": "WI"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "99",
"_score": 1.889911,
"_source": {
"account_number": 99,
"balance": 47159,
"firstname": "Ratliff",
"lastname": "Heath",
"age": 39,
"gender": "F",
"address": "806 Rockwell Place",
"employer": "Zappix",
"email": "[email protected]",
"city": "Shaft",
"state": "ND"
}
}
]
}
}
<
h3>pace(placeのtypo)で検索する
fuzzy_like_thisクエリを使うことであいまい検索ができます。
例えば「addressにPlaceが含まれるデータ」でしようとして、「pace」と入力してしまったケースを考えます。
% curl -XPOST https://search-DOMAIN-NAME-XXXX.ap-northeast-1.es.amazonaws.com/bank/_search -d '{
"query":{
"fuzzy_like_this":{
"fields":["address"],
"like_text":"pace"
}
}
}'
WordPressの検索とかだと0件とか言われるんですが、ElasticSearchならこのように似ているワードがある場合も値を返してくれます。
{
"took": 27,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 207,
"max_score": 3.0256464,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "590",
"_score": 3.0256464,
"_source": {
"account_number": 590,
"balance": 4652,
"firstname": "Ladonna",
"lastname": "Tucker",
"age": 31,
"gender": "F",
"address": "162 Kane Place",
"employer": "Infotrips",
"email": "[email protected]",
"city": "Utting",
"state": "IA"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "606",
"_score": 3.0256462,
"_source": {
"account_number": 606,
"balance": 28770,
"firstname": "Michael",
"lastname": "Bray",
"age": 31,
"gender": "M",
"address": "935 Lake Place",
"employer": "Telepark",
"email": "[email protected]",
"city": "Lemoyne",
"state": "CT"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "11",
"_score": 2.5610347,
"_source": {
"account_number": 11,
"balance": 20203,
"firstname": "Jenkins",
"lastname": "Haney",
"age": 20,
"gender": "M",
"address": "740 Ferry Place",
"employer": "Qimonk",
"email": "[email protected]",
"city": "Steinhatchee",
"state": "GA"
}
}
]
}
}
“_score”の値が高ければ関連性が高いらしいです。
そんなわけで
とりあえずここまでElasticSearchでいろいろ検索してみました。
JSONでデータ投入できればOKなので、いろいろと投入してみたいと思います。