Elasticsearch 살펴보기 3탄 - 검색 API

bradley·2022년 6월 13일
1

ELK

목록 보기
5/8

Search API는 Elasticsearch에 있는 content를 검색할 때 사용한다. parameter로서 query string을 가진 get request를 보냄으로서 검색할 수 있다. 또는 post request의 message body에 query를 전송할 수 있다. 주로 모든 Search API는 다중 index, 다중 type을 지원한다.

다중 Index

Elasticsearch는 모든 index 또는 몇몇 특정 index 안에 있는 현재 document를 검색할 수 있게 한다.

Command Example
모든 index에서 city field가 paprola인 데이터를 검색한다.

GET /_all/_search?q=city:paprola

정상 출력

{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 7,
    "successful" : 7,
    "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"
        }
      }
    ]
  }
}

검색 방법

URL 검색

URI(Uniform Resource Identifier)를 사용하여 search 하는 방법
여태 본 예제들이 이 방식에 해당함

Parameter

  • q: 특정 query string에 사용된다.
  • lenient: 특정 query string에 사용된다. true로 설정함으로서 Format 기반 오류를 무시할 수 있다. 기본값은 false
  • field: 특정 query string에 사용된다.
  • sort: 결과를 정렬한다. 이 파라미터의 가능한 값은 fieldName, fieldName:asc, fieldName:desc
  • timeout: 검색 시간을 제한하며 response에는 지정된 시간 내 조회한 것만 포함한다. 기본값은 no timeout
  • terminate_after: 각 샤드에 대해 지정된 수의 document에 대해 response를 제한한다. 해당 document에 도달하면 query는 조기 종료된다. 기본값은 no terminate_after
  • form: return할 찾은 index로부터의 시작. 기본값은 0
  • size: return할 찾은 수. 기본값은 10

Request Body 검색

request body에 query DSL을 사용하여 query를 명세할 수 있다.

Command Example
school index에서 up이란 키워드를 가진 데이터를 검색한다.

POST /schools/_search
{
   "query":{
      "query_string":{
         "query":"up"
      }
   }
}

정상 출력
"state":"UP"이 포함된 데이터가 검색된다.

{
  "took" : 11,
  "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"
        }
      }
    ]
  }
}
profile
데이터 엔지니어링에 관심이 많은 홀로 삽질하는 느림보

0개의 댓글