ElasticSearch導入のための学習とまとめ。
環境
- vagrant1.7.2
- centos6.5
Search with Query DSL
JSON request bodyを使って検索ができる。
curl -XGET http://192.168.33.19:9200/megacorp/employee/_search -d ' { "query" : { "match" : { "last_name" : "Smith" } } }' // 結果 {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":2,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.30685282,"_source": { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } },{"_index":"megacorp","_type":"employee","_id":"2","_score":0.30685282,"_source": { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } }]}}
More-Complicated Searches
もうちょい複雑な検索をしてみる。filterを利用する。
curl -XGET http://192.168.33.19:9200/megacorp/employee/_search -d ' { "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "smith" } } } } }' // 結果 {"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"megacorp","_type":"employee","_id":"2","_score":0.30685282,"_source": { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } }]}}
gtはgreater than。
Full-Text Search & Phrase Search
さらにもうちょい複雑な検索をしてみる。rock climbingが趣味のユーザーを取得する。
curl -XGET http://192.168.33.19:9200/megacorp/employee/_search -d ' { "query" : { "match_phrase" : { "about" : "rock climbing" } } }' // 結果 {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.23013961,"_source": { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } }]}}
"rock" と "climbing" の両方を含んでいるデータを取得する場合は、match_phraseで検索する。matchだと"rock" と "climbing"のいづれか一つのフレーズがマッチしただけで結果を取得することになる。
Highlighting Our Searches
ハイライトさせたいときはhighlight parameterを加える。
curl -XGET http://192.168.33.19:9200/megacorp/employee/_search -d ' { "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } } }' // 結果 {"took":51,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.23013961,"hits":[{"_index":"megacorp","_type":"employee","_id":"1","_score":0.23013961,"_source": { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } ,"highlight":{"about":["I love to go rock climbing"]}}]}}
Analytics
集合を操れる。ここは割愛。上記までのやり方が理解できてれば、全然難しくない。
ここより先は、必要な時、もっと理解を深めたいときに読むと良いとのこと。
というわけで導入の決定版ガイドで学習は終了になります。次は既存のmysqlと連携して利用してみようと思います。
また具体的に学習する項目があれば、こんな感じでやっていこうと思います。
では、また。
0 件のコメント:
コメントを投稿