4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Elasticsearch へのデータ投入

Last updated at Posted at 2018-10-03

次のプログラムを参考にしました。
Elasticsearchへのデータ投入と検索の方法(初級)

日本語も入れてみました。

データを投入するスクリプト

go_insert.sh
#
curl -H "Content-Type: application/json" \
	-X PUT http://localhost:9200/blog/_doc/t0001 -d '
{
	"name": "山本太郎",
	"title": "My Name Is Yamamoto",
	"content": "I love dog",
	"tags": ["red", "green", "blue"]
}
'

curl -H "Content-Type: application/json" \
	-X PUT http://localhost:9200/blog/_doc/t0002 -d '
{
	"name": "田中康夫",
	"title": "My Name Is Tanaka",
	"content": "I love cat",
	"tags": ["Earth", "Moon", "Mars"]
}
'

curl -H "Content-Type: application/json" \
	-X PUT http://localhost:9200/blog/_doc/t0003 -d '
{
	"name": "渡辺五郎",
	"title": "My Name Is Watanabe",
	"content": "I love fish",
	"tags": ["apple", "orange", "banana"]
}
'

データ数のカウント

$ curl 'localhost:9200/blog/_count?q=*'
{"count":3,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0}}

データ投入後、検索します。

$ curl -X Gcurl -X GET http://localhost:9200/blog/_search?q=title:yamamoto
{"took":9,"timed_out":false,"_shards":{"total":7,"successful":7,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":0.9808291,"hits":[{"_index":"blog","_type":"article","_id":"t0001","_score":0.9808291,"_source":
{
	"name": "山本太郎",
	"title": "My Name Is Yamamoto",
	"content": "I love dog",
	"tags": ["red", "green", "blue"]
}
}]}}
$ curl -X GET http://localhost:9200/blog/_search?q=tags:apple,moon 
{"took":66,"timed_out":false,"_shards":{"total":7,"successful":7,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":0.9808291,"hits":[{"_index":"blog","_type":"article","_id":"t0002","_score":0.9808291,"_source":
{
	"name": "田中康夫",
	"title": "My Name Is Tanaka",
	"content": "I love cat",
	"tags": ["Earth", "Moon", "Mars"]
}
},{"_index":"blog","_type":"article","_id":"t0003","_score":0.9808291,"_source":
{
	"name": "渡辺五郎",
	"title": "My Name Is Watanabe",
	"content": "I love fish",
	"tags": ["apple", "orange", "banana"]
}
}]}}

データの削除

$ curl -X DELETE http://localhost:9200/blog
{"acknowledged":true}

次のバージョンで確認しました。

$ curl http://localhost:9200
{
  "name" : "iwata",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "R92Wu-uxTl2gad2cs6OkIg",
  "version" : {
    "number" : "7.17.1",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "e5acb99f822233d62d6444ce45a4543dc1c8059a",
    "build_date" : "2022-02-23T22:20:54.153567231Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Httpie の例

Elasticsearch: Httpie でアクセス

4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?