Elasticsearch DSL Query

min·2021년 11월 13일
0

참고
https://oboki.net/workspace/data-engineering/elasticsearch/elasticsearch-index-template/
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
https://esbook.kimjmin.net/
https://unuseful.tistory.com/21

쿼리 짜기.. 😂
된다고 넘어가지 말고 이해하고 짜자..

ElasticSearch Index templates

  • 새로운 index가 생성될 때 index 설정 또는 특정 필드의 데이터 타입을 정의함
  • index template을 생성하면서 index 별로 특정 필드에 대한 개별 관리가 가능해진다.
  • 새로 생성되는 인덱스에 설정을 미리 해놓지 않아도 자동으로 템플릿이 적용됨

DSL Query

  • _score 점수가 높을수록 문서 관련성이 높아짐.
    • query context : 점수 계산함. “How well does this document match this query clause?” > match 잘 하고 있니? 얼마나 일치하는지 평가하는데 사용. 유사 검색 (문자 검색)
    • filter context : 점수 계산하지 않음. “Does this document match this query clause?” > match 하니? 안 하니? 일치하는 문서를 걸러내지만 _score에 영향을 주지 않음. 정확한 검색.
// title 필드에 Search가 포함되고, content는 Elasticsearch가 포함 된 것
// status가 published이고 publish_date가 2015-01-01 이후인 것
GET /_search
{
  "query": { 
		<!-- bool & match > query context -->
    "bool": { 
      "must": [
        { "match": { "title":   "Search"        }},
        { "match": { "content": "Elasticsearch" }}
      ],
			<!-- fitler > filter context -->
      "filter": [ 
        { "term":  { "status": "published" }},
        { "range": { "publish_date": { "gte": "2015-01-01" }}}
      ]
    }
  }
}
  • 풀 텍스트 쿼리 (Full Text Query)
    • match_all
    • match
    • match_pharse
    • query_string
  • Bool 복합 쿼리
    • must : 반드시 포함하는 조건 query context
    • must_not filter context
    • should : 여러개 중 하나를 포함 할 조건. query context
    • filter : 쿼리가 참인 도큐먼트를 검색. must보다 속도가 빠르고 캐싱 가능함. 필터링 할 조건. filter context
  • term
    • 검색어에 대한 분석을 하지 않고 검색어와 일치하는 문서를 검색함
  • match
    • 검색어에 대한 분석을 통해 검색함 > 토크나이징된 단어들을 찾아 문석을 검색하고 단어들이 최소 1개라도 들어있다면 검색 결과에 포함함
    • bool 쿼리 중 should 조건으로 검색함
profile
발등에 불이 따뜻하다..

0개의 댓글