English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Elasticsearch 쿼리 DSL

Elasticsearch에서는 JSON 기반의 쿼리를 사용하여 검색을 수행합니다. 쿼리는 두 부분으로 구성됩니다-

  • 잎 쿼리 문장 - 이러한 문장은 매칭, 테르미널 또는 범위로, 특정 필드에서 특정 값을 찾습니다.

  • 복합 쿼리 문장 - 이러한 쿼리는 잎 쿼리 문장과 복합 쿼리의 조합으로, 필요한 정보를 추출하기 위해 사용됩니다.

Elasticsearch는 많은 쿼리를 지원합니다. 쿼리는 쿼리 키워드로 시작되고, 그 다음 JSON 객체 형식으로 조건과 필터를 포함합니다. 다음은 다른 유형의 쿼리를 설명합니다.

모든 쿼리에 매칭

이것은 가장 기본적인 쿼리입니다; 모든 내용을 반환하며, 각 객체의 점수는1.0。

POST /schools/_search
{
   "query":{
      "match_all":{}
   }
}

위의 코드를 실행하면 다음과 같은 결과를 얻습니다.-

{
   "took": : 7,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
         "value": : 2,
         "relation": "eq"
      }
      "max_score": 1.0,
      "hits": [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : ""5",
            "_score": 1.0,
            "_source": {
               "name" : "Central School",
               "description" : "CBSE Affiliation",
               "street" : "Nagan",
               "city" : "paprola",
               "state" : "HP",
               "zip" : ""176115",
               "location" : [
                  31.8955385,
                  76.8380405
               ],
               "fees": : 2200,
               "tags": [
                  "Senior Secondary",
                  "beautiful campus"
               ],
               "rating": ""3.3"
            }
         }
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : ""4",
            "_score": 1.0,
            "_source": {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : ""250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees": : 3500,
               "tags": [
                  "fully computerized"
               ],
               "rating": ""4.5"
            }
         }
      }
   }
}

전체 텍스트 쿼리

이러한 쿼리는 전문가 문서나 뉴스 기사와 같은 전체 텍스트를 검색하는 데 사용됩니다. 이 쿼리는 특정 인덱스나 문서와 관련된 분석기에 따라 작동합니다. 이 장에서는 전체 텍스트 쿼리의 다른 유형에 대해 논의할 것입니다.

일치 쿼리

이 쿼리는 텍스트나 문장을 하나 이상의 필드의 값과 일치시킵니다.

POST /schools*/_search
{
   "query":{
      "match" : {
         "rating":"4.5"
      }
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "took": : 44,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
         "value": : 1,
         "relation": "eq"
      }
      "max_score" : 0.47000363,
      "hits": [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : ""4",
            "_score" : 0.47000363,
            "_source": {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : ""250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees": : 3500,
               "tags": [
                  "fully computerized"
               ],
               "rating": ""4.5"
            }
         }
      }
   }
}

다중 비교 쿼리

이 쿼리는 하나 이상의 필드에 맞는 텍스트나 문장을 일치시킵니다.

POST /schools*/_search
{
   "query":{
      "multi_match" : {
         "query": "paprola"
         "fields": [  "city",  "state" ]
      }
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "took": : 12,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
         "value": : 1,
         "relation": "eq"
      }
      "max_score" : 0.9808292,
      "hits": [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : ""5",
            "_score" : 0.9808292,
            "_source": {
               "name" : "Central School",
               "description" : "CBSE Affiliation",
               "street" : "Nagan",
               "city" : "paprola",
               "state" : "HP",
               "zip" : ""176115",
               "location" : [
                  31.8955385,
                  76.8380405
               ],
               "fees": : 2200,
               "tags": [
                  "Senior Secondary",
                  "beautiful campus"
               ],
               "rating": ""3.3"
            }
         }
      }
   }
}

쿼리 문자열 쿼리

이 쿼리는 쿼리 분석기와 query_string 키워드를 사용합니다.

POST /schools*/_search
{
   "query":{
      "query_string":{
         "query":"beautiful"
      }
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "took": : 60,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
      "value": : 1,
      "relation": "eq"
   }
………………………………….

단어 수준 쿼리

이러한 쿼리는 주로 구조화된 데이터, 예를 들어 숫자, 날짜, 열거형을 처리합니다.

POST /schools*/_search
{
   "query":{
      "term":{"zip":"176115}
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

……………………………..
hits" : [
   {
      "_index" : "schools",
      "_type" : "school",
      "_id" : ""5",
      "_score" : 0.9808292,
      "_source": {
         "name" : "Central School",
         "description" : "CBSE Affiliation",
         "street" : "Nagan",
         "city" : "paprola",
         "state" : "HP",
         "zip" : ""176115",
         "location" : [
            31.8955385,
            76.8380405
         ],
      }
   }
}   
…………………………………………..

범위 쿼리

이 쿼리는 지정된 값 범위에 있는 값이 있는 객체를 찾기 위해 사용됩니다. 이를 위해 계산자를 사용해야 합니다. 예를 들어-

  • gte − 큰 것보다 큰

  • gt − 큰

  • lte − 작은 것보다 작은

  • lt − 작은

예를 들어, 아래에 제공된 코드를 관찰하세요-

POST /schools*/_search
{
   "query":{
      "range":{
         "rating":{
            "gte":3.5
         }
      }
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "took": : 24,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
         "value": : 1,
         "relation": "eq"
      }
      "max_score": 1.0,
      "hits": [
         {
            "_index" : "schools",
            "_type" : "school",
            "_id" : ""4",
            "_score": 1.0,
            "_source": {
               "name" : "City Best School",
               "description" : "ICSE",
               "street" : "West End",
               "city" : "Meerut",
               "state" : "UP",
               "zip" : ""250002",
               "location" : [
                  28.9926174,
                  77.692485
               ],
               "fees": : 3500,
               "tags": [
                  "fully computerized"
               ],
               "rating": ""4.5"
            }
         }
      }
   }
}

또한 다른 종류의 테마 수준 쿼리가 있습니다. 예를 들어,-

  • 존재 쿼리 −어떤 필드의 값이 비어있지 않다면.

  • 미시 쿼리 −이 쿼리는 존재 쿼리와 정반대로, 특정 필드나 값이 없는 객체를 검색합니다.

  • 패턴이나 regexp 쿼리 −이 쿼리는 객체에서 패턴을 찾기 위해 정규 표현식을 사용합니다.

복합 쿼리

이 쿼리는 다른 쿼리의 집합으로, 이 쿼리는 부울 연산자(예: AND)를 사용하여 다른 쿼리를 결합합니다./또는, 아니면) 또는 다른 인덱스나 함수 호출 등을 사용하여 각각을 결합합니다.

POST /schools/_search
{
   "query": {
      "bool": {
         "must": {
            "term": {"state": "UP"}}
         }
         "filter": {
            "term": {"fees": ""22"minimum_should_match": "00"}
         }
         "boost": : 1,
         .0 1"value": 0,
      }
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "took": : 6,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
         "max_score": null,
         "relation": "eq"
      }
      "hits": [
      지리적 쿼리
   }
}

이 쿼리는 지리적 위치와 지리적 위치를 처리합니다. 이 쿼리는 학교나 다른 어떤 지리적 위치 근처의 지리적 객체를 찾는데 도움이 됩니다. 지리적 데이터 유형을 사용해야 합니다.

PUT

"mappings": { /geo_example
{
   "properties": {
      "type": "geo_shape"
         "location": {
            "acknowledged": true,
         }
      }
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "shards_acknowledged": true,
   "index": "geo_example"
}

이제, 데이터를 위에서 생성한 인덱스에 배포합니다.

POST /geo_example/_doc?refresh
{
   "name": "Chapter One, London, UK",
   "location": {
      "type": "point",
      "coordinates": [:11.660544, 57.800286}
   }
}

위의 코드를 실행한 후, 다음과 같은 응답을 얻습니다:

{
   "took": : 1,
   "timed_out": false,
   "_shards": {
      "total": : 1,
      "successful": : 1,
      "skipped": 0,
      "failed": 0
   }
   "hits": {
      "total": {
         "value": : 2,
         "relation": "eq"
      }
      "max_score": 1.0,
      "hits": [
         "_index": "geo_example",
         "_type": "_doc",
         "_id": "hASWZ2oBbkdGzVfiXHKD
         "_score": 1.0,
         "_source": {
            "name": "Chapter One, London, UK",
            "location": {
               "type": "point",
               "coordinates": [
                  11.660544,
                  57.800286
               }
            }
         }
      }
   }