Elasticsearchに慣れるために以下のチュートリアルをなぞってみる。解りやすい記事に感謝。
実践!Elasticsearch – Wantedly Engineer Blog
http://engineer.wantedly.com/2014/02/25/elasticsearch-at-wantedly-1.html
動作環境
Elasticsearch 2.2.1
プラグイン: head, kuromoji
analyzer と mapping の設定設定
設定をJSONに保存してコマンドで食わせてみる。
mapping.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
{ "settings": { "analysis": { "filter": { "pos_filter": { "type": "kuromoji_part_of_speech", "stoptags": [ "助詞-格助詞-一般", "助詞-終助詞" ] }, "greek_lowercase_filter": { "type": "lowercase", "language": "greek" } }, "tokenizer": { "kuromoji": { "type": "kuromoji_tokenizer" }, "ngram_tokenizer": { "type": "nGram", "min_gram": "2", "max_gram": "3", "token_chars": [ "letter", "digit" ] } }, "analyzer": { "kuromoji_analyzer": { "type": "custom", "tokenizer": "kuromoji_tokenizer", "filter": [ "kuromoji_baseform", "pos_filter", "greek_lowercase_filter", "cjk_width" ] }, "ngram_analyzer": { "tokenizer": "ngram_tokenizer" } } } }, "mappings": { "company": { "_source": { "enabled": true }, "_all": { "enabled": true, "analyzer": "kuromoji_analyzer" }, "properties": { "id": { "type": "integer", "index": "not_analyzed" }, "name": { "type": "string", "index": "analyzed", "analyzer": "ngram_analyzer" }, "location": { "type": "string", "index": "analyzed", "analyzer": "kuromoji_analyzer" } } }, "project": { "_source": { "enabled": true }, "_all": { "enabled": true, "analyzer": "kuromoji_analyzer" }, "_parent": { "type": "company" }, "properties": { "id": { "type": "integer", "index": "not_analyzed" }, "title": { "type": "string", "index": "analyzed", "analyzer": "kuromoji_analyzer" } } } } } |
1 |
$ curl -XPOST localhost:9200/wantedly-demo -d @mapping.json |
データ入力
同じく、データをJSONに保存してコマンドで食わせてみる。
requests.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
{ "index": { "_index": "wantedly-demo", "_type": "company", "_id": "1" } } { "id": "1", "name": "wantedly", "location": "東京都港区白金台 3-19-6 白金台ビル3F" } { "index": { "_index": "wantedly-demo", "_type": "company", "_id": "2" } } { "id": "2", "name": "heroku", "location": "東京都千代田区丸の内2-7-2 JPタワー 12階" } { "index": { "_index": "wantedly-demo", "_type": "company", "_id": "3" } } { "id": "3", "name": "higanworks", "location": "大阪府大阪市中央区道修町2-2-5 " } { "index": { "_index": "wantedly-demo", "_type": "company", "_id": "4" } } { "id": "4", "name": "hatena", "location": "京都府京都市中京区御池通間之町東入高宮町206" } { "index": { "_index": "wantedly-demo", "_type": "company", "_id": "5" } } { "id": "5", "name": "nulab", "location": "福岡市博多区中洲5丁目5-13 KDC福岡ビル7F " } { "index": { "_index": "wantedly-demo", "_type": "project", "_parent": "1" } } { "id": "1", "title": "iOS エンジニアウォンテッド!" } { "index": { "_index": "wantedly-demo", "_type": "project", "_parent": "1" } } { "id": "2", "title": "Ruby on Rails 得意なエンジニアウォンテッド!" } { "index": { "_index": "wantedly-demo", "_type": "project", "_parent": "2" } } { "id": "3", "title": "Ruby 好きウォンテッド!" } { "index": { "_index": "wantedly-demo", "_type": "project", "_parent": "3" } } { "id": "4", "title": "Chef と Ruby 書ける人ウォンテッド!" } { "index": { "_index": "wantedly-demo", "_type": "project", "_parent": "4" } } { "id": "5", "title": "Perl エンジニアウォンテッド!" } { "index": { "_index": "wantedly-demo", "_type": "project", "_parent": "5" } } { "id": "6", "title": "Java エンジニアウォンテッド!" } |
1 |
$ curl -s -XPOST localhost:9200/_bulk --data-binary @requests.json > /dev/null |
クエリを投げる
Headからクエリを投げてみる。