端くれプログラマの備忘録 Elasticsearch [Elasticsearch] チュートリアル wantedly-demo

[Elasticsearch] チュートリアル wantedly-demo

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

{
  "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"
        }
      }
    }
  }
}
$ curl -XPOST localhost:9200/wantedly-demo -d @mapping.json

データ入力

同じく、データをJSONに保存してコマンドで食わせてみる。

requests.json

 { "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 エンジニアウォンテッド!" }
$ curl -s -XPOST localhost:9200/_bulk --data-binary @requests.json > /dev/null

クエリを投げる

Headからクエリを投げてみる。

elasticsearch-head3
elasticsearch-head2