LoginSignup
1
1

More than 3 years have passed since last update.

【備忘メモ】elastic search 入門メモ

Last updated at Posted at 2020-03-30

サンプルコードURL

まず、こちらからソースをダウンロード。

docker-compose.ymlのあるディレクトリで以下のコマンドでelasticsearchとkibanaが立ち上がる

docker-compose up -d

立ち上がったら 、、

elastic search へのアクセス

http://localhost:9200/

kibana へのアクセス

参考URL

コマンドメモ

リソース作成・変更

curl -XPUT "http://localhost:9200/customer/external/1" \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "terms": {
      "name": "taro", "cook_time_min": [10, 15, 20]
    }
  }
}'
# リソース作成にあたってURLのID指定がない場合は自動でIDが割り当てられる
curl -XPOST "http://localhost:9200/customer/external" \
-H "Content-Type: application/json" \
-d '{
  "query": {
    "terms": {
      "name": "taro3", "cook_time_min": [10, 15, 20]
    }
  }
}'
curl -XPOST "http://localhost:9200/test/recipes/2" \
-H "Content-Type: application/json" \
-d @./data/recipes/basil-and-pesto-hummus.json

リソース削除

curl -XDELETE "http://localhost:9200/customer/external/2"

バッチプロセッシング

curl -XPOST "http://localhost:9200/customer/external/_bulk" \
-H "Content-Type: application/json" \
-d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe2" }
{"index":{"_id":"3"}}
{"name": "Jane Doe3" }
'
curl -XPOST "http://localhost:9200/customer/external/_bulk" \
-H "Content-Type: application/json" \
-d '
{"update":{"_id":"1"}}
{"doc": {"name": "John Doe becomes Jane Doe"}}
{"delete":{"_id":"2"}}
'

検索

データ挿入

curl -XPOST 'localhost:9200/classmethod/employees/_bulk?pretty' \
-H "Content-Type: application/json" \
--data-binary "@./data/employees/employees.jsonl"

curl 'localhost:9200/_cat/indices?v&index=classmethod'

全件

curl 'localhost:9200/classmethod/employees/_search' \
-H "Content-Type: application/json" \
-d '
{
    "_source": {
        "exclude": ["joined_date", "friends"]
    },
    "query": {
        "match_all": {}
    }
}
'

ページング


curl 'localhost:9200/classmethod/employees/_search' \
-H "Content-Type: application/json" \
-d '
{
    "query": {
        "match_all": {}
    },
    "size": 10,
    "from": 0
}
'

フィルタリング

curl 'localhost:9200/classmethod/employees/_search' \
-H "Content-Type: application/json" \
-d '
{
    "_source": {
        "include": ["employee_id", "firstname"]
    },
    "query": {
        "match" : {
            "firstname": "tammy"
        }
    }
}
'
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