전체 그래프
Elasticsearch

Query DSL

data-engineeringelasticsearchsearchquery-dsl

상위: Elasticsearch

요약

Query DSL(Domain Specific Language)은 JSON 기반의 강력한 검색 질의 언어입니다.

Query Context vs Filter Context

  • Query Context: "이 문서가 쿼리와 얼마나 유사한가?" (Relevance Score 계산).
  • Filter Context: "이 문서가 조건에 맞는가?" (Yes/No). Score 계산 안 함, 캐싱됨 (빠름).

주요 쿼리

Full Text Queries (전문 검색)

  • match: 텍스트를 분석(Analyzer)하여 검색
  • match_phrase: 단어의 순서까지 일치해야 함
  • match_phrase_prefix: 접두어 기반 검색 (자동완성 용도)
  • multi_match: 여러 필드에서 검색

Match with Operator

{
  "match": {
    "name": {
      "query": "Samsung Ultra",
      "operator": "AND"
    }
  }
}

Multi Match 타입

타입설명
best_fields가장 높은 점수의 필드만 반영
most_fields모든 필드 점수 합산
cross_fields여러 필드를 조합 (단어가 다른 필드에 있어도 매치)

Term Level Queries (정확한 값 검색)

  • term: 정확한 값 일치 (Keyword 필드 등에 사용).
  • terms: 여러 값 중 하나 일치 (SQL IN).
  • range: 범위 검색 (숫자, 날짜).

Compound Queries (조합)

  • bool: 여러 쿼리를 조합 (must, must_not, should, filter).
    • must: AND
    • must_not: NOT
    • should: OR (단, must와 함께 쓰이면 점수만 높임)
    • filter: 필터링 (Filter Context)

기타 쿼리

  • query_string: Lucene 쿼리 구문 사용 (AND, OR, 정규식 등)
  • exists: 필드가 존재하는 문서만 검색

Nested Query

배열 형태의 JSON 객체에서 특정 조건을 만족하는 문서를 검색합니다.

Nested vs Object

타입동작
object문서가 평탄화됨, 배열 내부 관계 유지 안 됨
nested배열 내 객체의 독립성 유지

Nested 매핑

"features": {
  "type": "nested",
  "properties": {
    "feature_name": { "type": "keyword" },
    "feature_value": { "type": "keyword" }
  }
}

Nested 쿼리

{
  "nested": {
    "path": "features",
    "query": {
      "bool": {
        "must": [
          { "term": { "features.feature_name": "RAM" }},
          { "term": { "features.feature_value": "16GB" }}
        ]
      }
    }
  }
}