LoginSignup
1
1

More than 5 years have passed since last update.

Elasticsearch インストールからクエリ叩くまで!

Last updated at Posted at 2017-09-29

全文検索エンジンElasticsearchをちょろ触りしたのでメモ。
https://www.elastic.co/guide/en/elasticsearch/reference/current/_basic_concepts.html

Elasticsearchでやってること

文章(検索対象のドキュメント)と検索クエリの両方共からトークン群(形態素解析もしくはNGramで分割)を作り、検索クエリのトークン群が文章のトークン群に含まれていたら検索にヒットする。もちろん検索クエリのトークン群のうち一部が含まれていたらOKとか、トークンを変形させてあいまい検索にも対応するとかそのようなことも可能。

レッツインストール!

OSにjavaが入ってるの前提で
本家サイトからElasticsearchのZipファイルをダウンロードする。

$ cd path/to/解凍したディレクトリ

Elasticsearch 起動!9200番ポートで立ち上がる。
$ bin/elaticsearch

データ挿入してみる
$ curl -X POST http://localhost:9200/jazzlegend/sample -d '{"name":"Michael Brecker","instrument":"Tenor Sax"}'
データ検索してみる
$ curl -X GET http://localhost:9200/jazzlegend/sample/_search -d '{"query":{"match":{"instrument":"Tenor Sax"}}}' | jq .

jqってライブラリ入れとくと| jq .でJSON綺麗に出力してくれる。


{
  "took": 2,              かかった時間
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {            取得したデータの件数
    "total": 1,
    "max_score": 0.51623213,
    "hits": [
      {
        "_index": "jazzlegend",
        "_type": "sample",
        "_id": "AV69eHB__b0V4BwomFK8",
        "_score": 0.51623213,
        "_source": {
          "name": "Michael Brecker",
          "instrument": "Tenor Sax"
        }
      }
    ]
  }
}

ゆるーく用語の確認

ふわっと理解したつもりでいるので、間違ってたらご指摘お願いします🙇

node: Elasticsearchサーバー
cluster: nodeの集まり。アプリごととかで分けたりする単位なのか
index: いわゆるデータベース
shard: nodeのリミットを超えるような大量のデータを扱うためにindexを小分けにするためのやつ
type: いわゆるデータテーブル
document: データオブジェクト単体

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1