콘텐츠로 이동

2024 09 10

2024-09-10

ES bool query

참고: https://juntcom.tistory.com/177 참고: https://stdhsw.tistory.com/entry/Elasticsearch-%EA%B2%80%EC%83%89%ED%95%98%EA%B8%B02-bool-must-mustnot-should 참고: https://esbook.kimjmin.net/05-search/5.2-bool

개요

  • bool Query는 4개의 인자를 가지고 있고, 그 인자 안에 다른 쿼리들을 배열로 넣는 방식
    • must: 쿼리가 참인 도큐먼트 검색
    • must_not: 쿼리가 거짓인 도큐먼트 검색
    • should: 검색 결과 중 이 쿼리에 해당하는 도큐먼트 점수 높임
    • filter: 쿼리가 참인 도큐먼트 검색. but 스코어 계산 X. must보다 빠르고 캐싱 가능
  • and 조건으로 검색 - must 안에 여러개의 조건을 추가
  • or 조건으로 검색 - must: [{match: "A B"}] 로 조건

예시 - must

  • keyword 타입 검색 : level 필드값이 info를 가지는 모든 도큐먼트 검색
    POST /index/_search
    {
      "query": {
        "bool": {
          "must": {
            "match": {
              "level": "info"
            }
          }
        }
      }
    }
    
  • 복합 쿼리 검색 : level 필드값 info, message에 "aaa"값 포함 도큐먼트
    POST /index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "level": "info"
              }
            },
            {
              "match": {
                "message": "aaa"
              }
            }
          }
        ]
      }
    }
    
  • text 타입 OR 검색 : "aaa" || "open" 갖는 도큐먼트 검색 => "aaa", "open" 모두 갖는다면 score 점수 높음
    POST /index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "message": "aaa open"
              }
            }
          ]
        }
      }
    }
    
  • text 타입 AND 검색 : "aaa" 와 "open" 모두 가지는 도큐먼트 => must 안에 배열로 엮어
    POST /index/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "message": "aaa"
              }
            },
            {
              "match": {
                "message": "open"
              }
            }
          ]
        }
      }
    }
    

예시 - must_not

  • level 필드에 info가 아닌 것
    {
      "query": {
        "bool": {
          "must_not": [
            {
              "match": {
                "level": "info"
              }
            }
          ]
        }
      }
    }
    
  • 복합 쿼리 검색 -> message 필드값 "open", level 필드값 "info" 아닌 것
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "message": "open"
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "level": "info"
              }
            }
          ]
        }
      }
    }
    

should

  • 있다면 score의 점수를 높여주는 것, OR 검색을 지원하지 않음
  • should 쿼리 검색 -> "debug" 일 경우 보다 높은 점수
    {
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "level": "debug"
              }
            }
          ]
        }
      }
    }
    
  • should 복합 쿼리 검색 -> "open" 값을 가진 도큐먼트 중에 level 필드에 "info" 값 가지면 더 높은 점수
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "message": "open"
              }
            }
          ],
          "should": [
            {
              "match": {
                "level": "info"
              }
            }
          ]
        }
      }
    }
    

Term vs Match

Term

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

  • 도큐먼트를 반환하되, 정확히 exact 한 term인 것 반환.
  • text에는 term 쓰지마. text는 ES에서 분석 대상인데, 이건 exact value로 찾는 term이다 보니 뇌절에 좋아
Terms

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

  • 도큐먼트를 반환하는데, 하나보다 많은 exact term인 것 반환
Match

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

  • 도큐먼트를 반환하되, 제공된 text, number, date, boolean과 일치하는 친구 반환
  • 제공된 text는 매칭전 분석된 친구들임.
  • full-text 서치에 걸맞는 친구. (fuzzy 매칭 포함 가능)

nested

https://wonyong-jang.github.io/elk/2021/07/13/ELK-Elastic-Search-Object-Nested.html

post filter

nested InnerHits

https://lingi04.github.io/backend/es/2_%EB%A7%A4%ED%95%91.html

  • Nested document는:
    • Nested query로 query됨
    • Nested and reverse_nested aggregation으로 분석 가능
    • Nested sorting으로 sort됨
    • Nested inner hits로 retrieved & highlight