[๐ŸŒŽelasticsearch] Elasticsearch search ๊ธฐ๋ณธ์‚ฌ์šฉ๋ฒ• (feat. Kibana)

Park Ji Youngยท2021๋…„ 1์›” 13์ผ
2

elasticsearch

๋ชฉ๋ก ๋ณด๊ธฐ
5/8
post-thumbnail
post-custom-banner


์ด๋ฒˆ์—๋Š” ElasticSearch ๋ฅผ ์“ฐ๋Š” ์ด์œ ์ค‘ ํ•˜๋‚˜์ธ ์ „๋ฌธ๊ฒ€์ƒ‰ (Full Text Search) ์˜ ๊ฐ„๋‹จํ•œ ๊ธฐ๋Šฅ์„ ์•Œ์•„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.
์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ๋Š” ๋‚ด์šฉ์ด ์›Œ๋‚™ ๋ฐฉ๋Œ€ํ•˜๋‹ค๋ณด๋‹ˆ ์ œ ๊ธ€์€ ๋ง›๋ณด๊ธฐ ์šฉ์œผ๋กœ ์ƒ๊ฐํ•ด์ฃผ์‹œ๊ณ  Elasticsearch ๊ฐ€์ด๋“œ๋ถ ์„ ๋” ์ฐธ๊ณ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.!

๐Ÿ‘“์ „๋ฌธ๊ฒ€์ƒ‰?

์ „๋ฌธ๊ฒ€์ƒ‰์€ ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋กœ ๋„ค์ด๋ฒ„์˜ ๊ฒ€์ƒ‰, ๊ตฌ๊ธ€์˜ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ์ƒ๊ฐํ•˜์‹œ๋ฉด ํŽธํ•ฉ๋‹ˆ๋‹ค.
๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ํŠน์ง•์ธ ํŠน์ • ๋‹จ์–ด์˜ ํฌํ•จ ์—ฌ๋ถ€, ์กฐ๊ฑด๋ฌธ์˜ ๊ธฐ๋Šฅ๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ํŠน์ • ๋‹จ์–ด์™€์˜ ์ƒ๊ด€๊ด€๊ณ„์— ๋Œ€ํ•œ ์ƒ‰์ธ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์–ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๊ธ€๋กœ๋งŒ ์ฝ์–ด๋„ ์—„์ฒญ ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋กœ ์˜ˆ์‹œ ๋“ค์–ด๊ฐ€๊ฒ ์Šต๋‹ˆ๋‹ค.
์ผ๋‹จ ElasticSearch ์™€ Kibana ๋ฅผ ์ผœ ์ฃผ์‹œ๊ณ  Kibana DevTools ๋กœ ๋“ค์–ด๊ฐ€์ฃผ์„ธ์š”! ์ด์ „์— ํ–ˆ์œผ๋‹ˆ ์—ฌ๋Ÿฌ๋ถ„ ๋‹ค ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

๐Ÿ˜‚ ์ผ๋‹จ ํ•ด๋ณด์ž!

๋จผ์ € ์ด์ „์— ๋ฐฐ์šด bulk ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๊ฒ€์ƒ‰ํ•  ๋ฐ์ดํ„ฐ๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•ฉ์‹œ๋‹ค!!
copy copy~

POST _bulk
{ "index" : { "_index*" : "test", "_id" : "1" } }
{ "field1" : "i love my cute cat" }
{ "index" : { "_index" : "test", "_id" : "2" } }
{ "field1" : "my dog is so big" }
{ "index" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "i am big" }
{ "index" : { "_index" : "test", "_id" : "4" } }
{ "field1" : "my girl friend is cute" }

์ œ์ผ ๋จผ์ € ๊ฐ„๋‹จํ•œ ๊ฒ€์ƒ‰์ธ URI ๊ฒ€์ƒ‰๋ถ€ํ„ฐ!

GET test/_search?q=dog

์ž ๊ทธ๋Ÿผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ฌ๊ฒ๋‹ˆ๋‹ค!

{
    "took" : 0,
    "timed_out" : false,
    "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
    },
    "hits" : {
        "total" : {
        "value" : 1,
        "relation" : "eq"
        },
        "max_score" : 1.1516262,
        "hits" : [
            {
                "_index" : "test",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 1.1516262,
                "_source" : {
                "field1" : "my dog is so big"
                }
            }
        ]
    }
}

"hits" ํ•„๋“œ๋ฅผ ์ฃผ๋ชฉํ•ฉ์‹œ๋‹ค. ๋งŒ์•ฝ ํ•ด๋‹น ๊ฒ€์ƒ‰์—์„œ ์•„๋ฌด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด ์ด ํ•„๋“œ์•ˆ์—๋Š” ์•„๋ฌด ๊ฐ’์ด ์—†์Šต๋‹ˆ๋‹ค.
"hits" ํ•„๋“œ ์•ˆ์— ๋‚ด์šฉ์„ ์ž˜ ๋ณด์‹œ๋ฉด _score ํ•ญ๋ชฉ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„์—์„œ ๊ฒ€์ƒ‰ํ•œ ํ‚ค์›Œ๋“œ์ธ "dog" ์™€ ์–ผ๋งˆ๋‚˜ ์ƒ๊ด€๊ด€๊ณ„๊ฐ€ ์žˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ ์ˆ˜ ์ž…๋‹ˆ๋‹ค. ์ •๋ง ์œ ์šฉํ•œ ์ง€ํ‘œ์ฃ ?

URI ๊ฒ€์ƒ‰์€

GET <index>/_search=?q=<value>

์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ ๊ฒ€์ƒ‰์„ ๋งํ•ฉ๋‹ˆ๋‹ค. Request Body ์— ์˜ต์…˜์„ ๋ถ™์—ฌ์„œ ๋” ์ž์„ธํ•œ ๊ฒ€์ƒ‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
๋‹ค์Œ ๋ช…๋ น์–ด๋Š” test ์ธ๋ฑ์Šค์˜ field1 ์˜ ํ•„๋“œ์—์„œ๋งŒ "dog" ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

GET test/_search
{
    "query": {
        "match": {
        "field1": "dog"
        }
    }
}

๋งŒ์•ฝ ํŠน์ • ์ธ๋ฑ์Šค์˜ ๋ชจ๋“  document ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•ด์ฃผ์„ธ์š”!.

GET my_index/_search
{
    "query":{
        "match_all":{ }
    }
}

์ž์ž์ž ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋Šฅ์„ ๋ฐฐ์› ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ์—๋Š” ์ข€๋” ์‹ฌํ™”๋‚ด์šฉ์„ ๋ฐฐ์›Œ๋ด…์‹œ๋‹ค! ๋‹ค์Œ ๋‚ด์šฉ์„ ๋ณด๊ธฐ ์ „์— ์ปคํ”ผ ์Šค์œฝ~ ์ŠคํŠธ๋ ˆ์นญ ์ญˆ์šฑ ํ•˜๊ณ  ์˜ค์„ธ์šฉ~๐Ÿงก๐Ÿงก

profile
I am two cat's father
post-custom-banner

3๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 1์›” 14์ผ

๊ตฌ๋…!

1๊ฐœ์˜ ๋‹ต๊ธ€
comment-user-thumbnail
2022๋…„ 8์›” 4์ผ

์ข‹์•„์š”!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ