[๐ŸŒŽelasticsearch] search query ์ง‘์ค‘ํƒ๊ตฌ - 1 ์ „์ฒ˜๋ฆฌ ๊ณผ์ •

Park Ji Youngยท2021๋…„ 1์›” 26์ผ
0

elasticsearch

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


๐Ÿงก ElasitcSearch Full-Text-Search ์˜ ์›๋ฆฌ

๐Ÿ™‚Inverted Index

Elasticsearch ๋Š” "text" ๋กœ ๋งคํ•‘๋œ ํ•„๋“œ์˜ ๊ฒฝ์šฐ ๊ทธ ์•ˆ์— ๋ฐ์ดํ„ฐ๋ฅผ Inverted Index ๋ผ๋Š” ๊ตฌ์กฐ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์ง์—ญํ•˜์ž๋ฉด ์—ญ ์ƒ‰์ธ, ๋ฐ˜๋Œ€ ์ƒ‰์ธ ์ด๋Ÿฐ ๋œป์ธ๋ฐ ๋Œ€์ถฉ ๊ฐ์ด ์˜ค์‹œ๋‚˜์š”??

์˜ˆ๋“ค ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ฉ์‹œ๋‹ค.

IdDescription
1"Cat is Cute"
2"Dog is Cute Too"
3"Cat is lazy"
4"Dog is super athletic"

์ผ๋ฐ˜์ ์ธ DBMS ์—์„œ๋Š” "athletic" ์„ ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‹ถ๋‹คํ•˜๋ฉด 1๋ฒˆ ,2๋ฒˆ, 3๋ฒˆ, 4๋ฒˆ ์ˆœ์„œ๋กœ ๊ฒ€์ƒ‰ํ•ด์„œ
"athleti" ์ด ์žˆ๊ตฌ๋‚˜ Id 4๋ฅผ ๋ณด์—ฌ์ค˜์•ผ์ง€. ๋ผ๊ณ  ํ•  ๊ฒ๋‹ˆ๋‹ค.

Inverted Index ๋Š” ์กฐ๊ธˆ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.
ํ† ํฐ๋งˆ๋‹ค ๊ฐ’์„ ๊ฐ€์ง€๋Š”๋ฐ, ์ด ๊ฐ’์€ "์ด ๋ฌธ์„œ์— ์ด ํ† ํฐ ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ์–ด."๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
์œ„์˜ ํ…Œ์ด๋ธ”์„ ์•„๋ž˜์™€ ๊ฐ™์€ ์‹์œผ๋กœ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

TokenId
"cat"1, 3
"dog"2, 4
"cute"1, 2
"lazy"3
"athletic"4
"super"4

์ด ๊ฐ™์€ ๊ฒฝ์šฐ "athletic" ํ† ํฐ์ด ํฌํ•จ๋œ 4๋ฒˆ ๋ฌธ์„œ๋งŒ ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๋‹ค๋งŒ ํ† ํฐ์„ ๋นจ๋ฆฌ ์ฐพ๊ธฐ ์œ„ํ•ด ์ €์žฅํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ข€ ๋ณต์žกํ•ด์งˆ๊ฒ๋‹ˆ๋‹ค.
๊ดœ์ฐฎ์Šต๋‹ˆ๋‹ค. Elasticsearch ๊ฐ€ ๋Œ€์‹ ํ•ด์ค„๊ฑฐ๊ฑฐ๋“ ์š”!!

๐Ÿ›‘๐Ÿ›‘ ๋‹จ ์ด๋ ‡๊ฒŒ ์ €์žฅ๋˜๋Š” ๊ฑด ์˜ค์ง ํ•„๋“œ๊ฐ€ text ํƒ€์ž…์ผ ๋•Œ๋งŒ ์ ์šฉ๋ฉ๋‹ˆ๋‹ค.
๋‹ค๋ฅธ๋ง๋กœ ํ•˜์ž๋ฉด text ํ•„๋“œ๊ฐ€ ์•„๋‹ˆ๋ฉด ์ „๋ฌธ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ์“ธ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค!!

๐Ÿ™‚Analizer

์œ„์˜ ์˜ˆ์‹œ์—์„œ ๋ˆˆ์น˜์ฑ„์‹  ๋ถ„๋„ ๊ณ„์‹œ๊ฒ ์ง€๋งŒ, ์ฒ˜์Œ ํ…Œ์ด๋ธ”์˜ ๋‚ด์šฉ์„ Inverted Index ๋ฐฉ๋ฒ•์œผ๋กœ ์ €์žฅํ•  ๋•Œ ์ „๋ถ€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๋€Œ์–ด์„œ ์ €์žฅ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ €์žฅ๋˜์ง€ ์•Š์€ ๋‹จ์–ด๋“ค๋„ ์žˆ๊ตฐ์š”. ์ด ๊ณผ์ •์„ Text Analysis ๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.
์ด ๊ณผ์ •์„ ๊ฑฐ์น˜์ง€ ์•Š๊ณ  ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด ๊ฒ€์ƒ‰์— ํ•„์š” ์—†๋Š”๋‹จ์–ด ๊ฐ€๋ น "is", "too" ๊นŒ์ง€ ์ €์žฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๊ฐ€ "dOG" ๋ผ๊ณ  ๊ฒ€์ƒ‰ํ•ด๋„ "Dog" ๊ฐ€ ํฌํ•จ๋œ ๋ฌธ์„œ๋ฅผ ๋ณด๊ณ  ์‹ถ๋‹ค๋ฉด ํ† ํฐํ™”ํ•˜๋Š” ๊ณผ์ •์— ์ „์ฒ˜๋ฆฌ ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

1. Character Filter

Character Filter ๋Š” ์ „์ฒ˜๋ฆฌ๊ณผ์ •์—์„œ ๊ฐ€์žฅ ๋จผ์ € ์ˆ˜ํ–‰๋˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค.
Character Filter ์—๋Š” HTML Strip, Mapping, Pattern Replace ์„ธ ๊ฐ€์ง€๊ฐ€ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

  • HTML Strip : ๋ฌธ์žฅ์— HTML ํƒœ๊ทธ๋“ค์„ ์—†์• ์ฃผ๋Š” Filter ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ๋“ค๋ฉด,
    <p>I&apos;m so <b>happy</b>!</p> ๋ผ๋Š” ๋ฌธ์žฅ์„ I'm so happy! ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.
  • Mapping : ํŠน์ • ๋ฌธ์ž์—ด์„ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • Pattern Replace : ํŠน์ • ์ •๊ทœ์‹ ํŒจํ„ด์„ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. Tokenizer Filter

Tokenizer Filter ๋Š” ๋ฌธ์ž์—ด์„ ๋ฐ›์•„ ๋‹จ์–ด๋“ค์„ ํ† ํฐํ™”ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค.
Tokenizer Filter ๋Š” ๋งŽ์€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๋Œ€ํ‘œ์ ์ธ 3๊ฐ€์ง€๋ฅผ ๋จผ์ € ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  • Standard : ํ† ํฐํ™” ํ•  ๋•Œ ๊ณต๋ฐฑ๊ณผ hyphen(-)์„ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•˜๋ฉฐ, ์ผ๋ถ€ ํŠน์ˆ˜๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
  • Letter : ํ† ํฐํ™” ํ•  ๋•Œ ๋ฌธ์ž์—ด ์ด์™ธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    ์˜ˆ๋“ค ๋“ค์–ด Brown-Foxes dog's ๊ฐ™์€ ๋ฌธ์ž์—ด์€ [Brown, Foxes, dog, s] ๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • Whitespace : ๊ณต๋ฐฑ ์„ ๊ธฐ์ค€์œผ๋กœ ํ† ํฐํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ณต๋ฐฑ, ์ค„๋ฐ”๊ฟˆ, ํƒญ ๋ชจ๋“  ๊ณต๋ฐฑ์„ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.

Email, URL, Path ์™€ ํŠน์ˆ˜ ๋ชฉ์ ์šฉ Tokenizer ๋“ค์ด ๋งŽ์œผ๋‹ˆ ์ž์„ธํ•œ ์ •๋ณด๋Š” Tokenizer Document ๋ฅผ ์ฐธ์กฐํ•ด์ฃผ์‹œ์™€์š”.!!

3. Token Filter

Token Filter ๋Š” ๊ฐ Tokenizer Filter ์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง„ Token ๋“ค์— ๋Œ€ํ•œ ํ›„์ฒ˜๋ฆฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ Lowercase Token Filterr ๋กœ Stop Token Filterr ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • Lowercase : ๋ง ๊ทธ๋Œ€๋กœ ํ† ํฐ๋“ค์„ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” Token filter ์ž…๋‹ˆ๋‹ค.
  • Stop : Search ๊ธฐ๋Šฅ์— ํ•„์š” ์—†๋Š” ๋‹จ์–ด๋“ค ์˜ˆ๋ฅผ ๋“ค๋ฉด is, be, to, a, as ๋“ฑ์„ ์—†์• ๋Š” ํ•„ํ„ฐ์ž…๋‹ˆ๋‹ค.

๋” ๋งŽ์€ Token filter ๋ฅผ ๋ณด๊ณ  ์‹ถ์œผ๋ฉด Token Filters ๋ฅผ ์ฐธ์กฐํ•ด์ฃผ์„ธ์š”!! ์˜ค๋ฅธ์ชฝ ๋ฉ”๋‰ด๋ฐ”์— ๋‹ค์–‘ํ•œ Token filters ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

4. Stemming

๋˜ํ•œ ๊ฐ ํ† ํฐ๋“ค์€ ๊ทธ ์›ํ˜•์„ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด working ์—์„œ ing ๋Š” ํ•„์š” ์—†๋Š” ๋ถ€๋ถ„์ด๋ฉฐ ์šฐ๋ฆฌ๋Š” work ๋งŒ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
์ด๋Ÿฌํ•œ ๊ณผ์ •์„ Stemming ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

ETC

์šฐ๋ฆฌ๊ฐ€ ๋ฌธ์„œ์— ์˜์–ด๋งŒ ์“ฐ๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ๊ทธ ๋‚˜๋ผ์—์„œ๋Š” ๊ทธ ๋‚˜๋ผ ์–ธ์–ด๋ฅผ ๋งŽ์ด ์“ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
ElasticSearch ์— ๋‚ด์žฅ๋œ ์–ธ์–ด๋ถ„์„๊ธฐ๊ฐ€ ์—†๋‹ค๋ฉด, Full-Text-Search ์— ๋‚œํ•ญ์„ ๊ฒช์„์ง€๋„ ๋ชจ๋ฆ…๋‹ˆ๋‹ค.
๋‹คํ–‰์ด๋„ ํ•œ๊ธ€์˜ ๊ฒฝ์šฐ๋Š” Nori ํ•œ๊ธ€ ํ˜•ํƒœ์†Œ ๋ถ„์„๊ธฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ด ๊ณผ์ •์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ Elasticsearch Document ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!! Nori Analysis Plugin ๊ตฌ๊ฒฝํ•˜๊ธฐ


์œ„ ๊ณผ์ •์„ ํ†ตํ•ด ์ €์žฅ๋œ ํ† ํฐ๋“ค์„ ํ†ตํ•ด Search API ๊ธฐ๋Šฅ๋“ค์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์—๋Š” query ๋ฅผ ํ†ตํ•ด ์‹ค์ œ ์˜ˆ์ œ๋กœ ์ฐพ์•„๋ต™๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ”ผ๋“œ๋ฐฑ์€ ํ•ญ์ƒ ํ™˜์˜์ž…๋‹ˆ๋‹ค.

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

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