[ElasticSearch] 데이터 조회(Search)
by youngjun._.
실습 환경
- 💡 Elasticsearch 7.9.0
- 💡 Windows 10
- 💡 Git Bash
ElasticSearch에 메인 기능이라고 볼 수 있는 Search
를 실습해보자.
먼저 sample 데이터를 bulk
해보자.
{ "index" : { "_index" : "basketball", "_type" : "record", "_id" : "1" } } {"team" : "Chicago Bulls","name" : "Michael Jordan", "points" : 30,"rebounds" : 3,"assists" : 4, "submit_date" : "1996-10-11"} { "index" : { "_index" : "basketball", "_type" : "record", "_id" : "2" } } {"team" : "Chicago Bulls","name" : "Michael Jordan","points" : 20,"rebounds" : 5,"assists" : 8, "submit_date" : "1996-10-11"}
1. Index에 데이터 Bulk하기
위에서 확인한 sample 데이터를 삽입하면
curl -XPOST "/ES주소/"_bulk --data-binary @"file명".json
$ curl -XPOST 'http://localhost:9200/_bulk?pretty' --data-binary @simple_basketball.json -H 'Content-Type: application/json'
결과
{ "took" : 423, "errors" : false, "items" : [ { "index" : { "_index" : "basketball", "_type" : "record", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1, "status" : 201 } }, { "index" : { "_index" : "basketball", "_type" : "record", "_id" : "2", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1, "status" : 201 } } ] }
잘 bulk 됐다!
2. ✔ _search : 옵션 없이 사용
$ curl -XGET 'http://localhost:9200/basketball/record/_search?pretty'
모든 document
가 다 출력된다!
결과
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "basketball", "_type" : "record", "_id" : "1", "_score" : 1.0, "_source" : { "team" : "Chicago Bulls", "name" : "Michael Jordan", "points" : 30, "rebounds" : 3, "assists" : 4, "submit_date" : "1996-10-11" } }, { "_index" : "basketball", "_type" : "record", "_id" : "2", "_score" : 1.0, "_source" : { "team" : "Chicago Bulls", "name" : "Michael Jordan", "points" : 20, "rebounds" : 5, "assists" : 8, "submit_date" : "1996-10-11" } } ] } }
3. ✔ _search : URI 옵션
serach 옵션 중 URI 옵션을 사용해보자.
basketball
Index 중 points = 30
인 데이터만 가져오기 위해 Query를 날리면 된다.
$ curl -XGET 'http://localhost:9200/basketball/record/_search?q=points:30&pretty'
points
가 30
인 하나의 데이터가 반환되는 것을 확인할 수 있다.
결과
{ "took" : 25, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "basketball", "_type" : "record", "_id" : "1", "_score" : 1.0, "_source" : { "team" : "Chicago Bulls", "name" : "Michael Jordan", "points" : 30, "rebounds" : 3, "assists" : 4, "submit_date" : "1996-10-11" } } ] } }
4. ✔ _search : Request body
Request Body를 사용할 수 있다!
계속 사용되는
-d
는 directe의 줄인말
$ curl -XGET http://localhost:9200/basketball/record/_search?pretty -d ' { "query" : { "term" : {"points" : 30} } }' -H 'Content-Type: application/json'
term query
로 points가 30인 것만 출력했다.
points
가 30
인 하나의 데이터가 반환되는 것을 확인할 수 있다.
결과
{ "took" : 1, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "basketball", "_type" : "record", "_id" : "1", "_score" : 1.0, "_source" : { "team" : "Chicago Bulls", "name" : "Michael Jordan", "points" : 30, "rebounds" : 3, "assists" : 4, "submit_date" : "1996-10-11" } } ] } }
Request Body 활용할 수 있는 방법이 많으니 공식 문서찾아보고 공부해보자.
본 포스팅은
Inflearn
의 ELK 스택 (ElasticSearch, Logstash, Kibana) 으로 데이터 분석 강의를 참고하여 작성되었습니다.
블로그의 정보
개발하는만두
youngjun._.활동하기
개발하는만두공부한 내용을 정리합니다 :)