Elastic Search - 정확도 쿼리 (4)
bool 쿼리의 should 는 검색 점수를 조정하기 위해 사용할 수 있다.
위의 단어들에 대해 색인하고 검색을 해보자.
우선 요청과 응답에 대해 보자.
{
"query": {
"bool": {
"must": [
{
"match": {
"message": "메종키츠네 반팔티"
}
}
]
}
}
}
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 2.6352928,
"hits": [
{
"_index": "set_of_search_index",
"_id": "3",
"_score": 2.6352928,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "4",
"_score": 2.6352928,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 화이트 AW00103KJ0005-P100"
}
},
{
"_index": "set_of_search_index",
"_id": "8",
"_score": 1.532453,
"_source": {
"message": "제이린드버그 남성 카라 반팔티 GMJT06857 9999"
}
},
{
"_index": "set_of_search_index",
"_id": "10",
"_score": 1.0727327,
"_source": {
"message": "메종키츠네 폭스 헤드 여성 반팔 티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "11",
"_score": 1.0727327,
"_source": {
"message": "메종키츠네 폭스 헤드 여성 반팔 티 블랙 AW00103KJ0005-P199"
}
}
]
}
}
보면 중간에 제이린드버그 가 껴있는걸 볼 수 있다
쇼핑몰에서 검색을 할 때 검색어에 관련된 내용들을 다 가져오도록 하면서 정확도가 높은 상품들을 최상단에 노출시켜야 하는데
단어중 반팔티 라는 단어가 있어서 제이린드버그라는 상품이 중간에 치고 나왔다.
이럴때 shoud를 사용하면 된다.
{
"query": {
"bool": {
"must": [
{
"match": {
"message": {
"query": "메종키츠네 반팔티"
}
}
}
],
"should": [
{
"match": {
"message": "메종키츠네"
}
}
]
}
}
}
should절에 점수를 높이고 싶은 단어를 넣고 요청을 보내보면 메종키츠네 가 들어간 상품들의 score가 높아진것을 볼 수 있다.
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 3.8133962,
"hits": [
{
"_index": "set_of_search_index",
"_id": "3",
"_score": 3.8133962,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "4",
"_score": 3.8133962,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 화이트 AW00103KJ0005-P100"
}
},
{
"_index": "set_of_search_index",
"_id": "10",
"_score": 3.0152838,
"_source": {
"message": "메종키츠네 폭스 헤드 여성 반팔 티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "11",
"_score": 3.0152838,
"_source": {
"message": "메종키츠네 폭스 헤드 여성 반팔 티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "8",
"_score": 1.532453,
"_source": {
"message": "제이린드버그 남성 카라 반팔티 GMJT06857 9999"
}
}
]
}
}
그리고 반대로 점수에 영향없이 참/ 거짓 여부만 판단해서 결과를 가져오는것도 가능한데 이걸 정확값이라 한다.
이럴땐 스코어 계산을 하지 않으며 filter 를 사용하면 된다.
{
"query": {
"bool": {
"must": [
{
"match": {
"message": {
"query": "메종키츠네 반팔티"
}
}
}
],
"filter": [
{
"match": {
"message": "메종키츠네"
}
}
]
}
}
}
filter를 사용하면 filter를 사용하지 않았을때와 점수가 똑같은데 메종키츠네를 가진 상품만 필터링 된것을 볼 수 있다.
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 2.6352928,
"hits": [
{
"_index": "set_of_search_index",
"_id": "3",
"_score": 2.6352928,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "4",
"_score": 2.6352928,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 화이트 AW00103KJ0005-P100"
}
},
{
"_index": "set_of_search_index",
"_id": "10",
"_score": 1.0727327,
"_source": {
"message": "메종키츠네 폭스 헤드 여성 반팔 티 블랙 AW00103KJ0005-P199"
}
},
{
"_index": "set_of_search_index",
"_id": "11",
"_score": 1.0727327,
"_source": {
"message": "메종키츠네 폭스 헤드 여성 반팔 티 블랙 AW00103KJ0005-P199"
}
}
]
}
}
또한 문자열 데이터에 keyword 라는 형식으로 저장하면 문자열과 공백, 대소문자까지 정확히 일치하는 데이터만 결과로 리턴한다.
이 또한 점수를 계산하지 않고 일치여부만 따진다.
{
"query": {
"bool": {
"filter": [
{
"match": {
"message.keyword": "메종키츠네 폭스헤드 우먼즈 반팔티 블랙 AW00103KJ0005-P199"
}
}
]
}
}
}
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.0,
"hits": [
{
"_index": "set_of_search_index",
"_id": "3",
"_score": 0.0,
"_source": {
"message": "메종키츠네 폭스헤드 우먼즈 반팔티 블랙 AW00103KJ0005-P199"
}
}
]
}
}
또한 점수를 계산하지 않는 쿼리중 range 라는 놈도 있는데 이놈은 범위를 필터링 해주는 쿼리다.
문법은 gte , gt, lte , lt 의 쿼리파라미터를 가지고 있고 많이 익숙한 단어들 이다."
{
"query": {
"range": {
"price": {
"gte": 700,
"lt": 900
}
}
}
}
위의 그림처럼 사용 하면 되고 위 쿼리의 내용은 700이상 900미만이다.
이 range 쿼리 또한 정확도를 계산하는게 아니라 그 범위에 들어오는지 아닌지 참여부만 따지기 때문에 스코어는 항상 일정하다.
참고로 공식문서에는
filter 안에 넣은 검색 조건들은 스코어를 계산하지 않지만 캐싱이 되기 때문에 쿼리가 더 가볍고 빠르게 실행됩니다. keyword 와 뒤에 설명할 range 쿼리와 같이 스코어 계산이 필요하지 않은 쿼리들은 모두 filter 안에 넣어서 실행하는 것이 좋습니다.
라 나와있다. 이 점을 기억해두자.
이제 엘라스틱 서치의 기본적인 검색과 쿼리 기능에 대해 알아보았다.
다음번엔 실질적으로 검색기능을 구현하기 위한 단계로 넘어가보자.