Search
URI Searches
POST /my_index/_doc/
{
"content": "quick book",
"publish_date": "2024-02-01"
}
POST /my_index/_doc/
{
"content": "quick walk",
"publish_date": "2024-03-01"
}
# URI Searches
GET /my_index/_search?q=content:quick
GET /my_index/_search?q=content:quick AND content:walk
DSL (Domain Specific Language) Queries
Basic Search
GET /my_index/_search
{
"query": {
"match": {
"content": "quick"
}
}
}
Full-Text Search with Query String
GET /my_index/_search
{
"query": {
"query_string": {
"query": "quick"
}
}
}
GET /my_index/_search
{
"query": {
"query_string": {
"query": "quick OR brown",
"fields": ["content"]
}
}
}
Boolean Queries
여러 개의 쿼리를 논리적으로 결합하여 검색 결과를 필터링하는 데 사용됩니다.
- must: 반드시 일치해야 하는 조건
- should: 선택적으로 일치할 수 있는 조건
- must_not: 반드시 일치하지 않아야 하는 조건
- filter: 점수에 영향을 미치지 않고 문서를 필터링하는 조건
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "Elasticsearch" } }
],
"should": [
{ "match": { "content": "search engine" } }
],
"must_not": [
{ "match": { "status": "draft" } }
],
"filter": [
{ "term": { "category": "tech" } }
]
}
}
}
- 제목에 "Elasticsearch"가 포함되어야 합니다.
- 내용에 "search engine"가 포함될 수도 있습니다.
- 상태가 "draft"가 아니어야 합니다.
- 카테고리가 "tech"여야 합니다.
Range Queries
- (e.g., numeric ranges, date ranges)
GET /my_index/_search
{
"query": {
"range": {
"publish_date": {
"gte": "2024-01-01",
"lte": "2024-02-28"
}
}
}
}
Phrase Search
특정한 순서(Sequence)로 나열된 단어를 포함하는 문서를 검색하는 방법입니다. 즉, 검색어의 순서와 위치를 고려하여 정확한 문구를 찾는데 유용합니다.
GET /my_index/_search
{
"query": {
"match_phrase": {
"content": "quick book"
}
}
}
Prefix Search
특정한 문자열로 시작하는 문서를 검색하는데 사용됩니다. 자동 완성 기능에 사용되며, 대소문자를 구분합니다.
GET /my_index/_search
{
"query": {
"prefix": {
"title": "Ela"
}
}
}
Wildcard Search
- *: matches any character sequence (including an empty sequence)
- ?: matches any single character
GET /my_index/_search
{
"query": {
"wildcard": {
"content": "qui*"
}
}
}
Regular Expression Search
정규 표현식 문법을 사용하여 문서를 검색하는데 사용됩니다.
GET /my_index/_search
{
"query": {
"regexp": {
"title": {
"value": "elasti.*"
}
}
}
}
Ids Query
여러 개의 ID로 문서를 검색하는데 사용됩니다.
GET /my_index/_search
{
"query": {
"ids": {
"values": ["1", "2", "3"]
}
}
}
Term Level Query
필드의 값과 정확히 일치하는 문서를 검색하는데 사용됩니다. 분석되지 않는 keyword, numeric, date, or boolean 필드에 사용됩니다.
# term Query (단수)
GET /my_index/_search
{
"query": {
"term": {
"status": {
"value": "active"
}
}
}
}
# terms Query (복수)
GET /my_index/_search
{
"query": {
"terms": {
"status": ["active", "inactive"]
}
}
}
Exists Query
특정 필드가 존재하는 문서를 검색하는데 사용됩니다. 이는 필드가 비어 있지 않은지, 즉 null or 빈 배열이 아닌 값을 가지고 있는지를 확인합니다.
GET /my_index/_search
{
"query": {
"exists": {
"field": "user_id"
}
}
}
Source Filter
검색 결과에서 반환되는 필드를 제어하는 기능입니다. 필요한 필드만 선택적으로 가져올 수 있어 네트워크 비용을 줄이고 성능을 향상시킬 수 있습니다.
- 소스 비활성화: "_source": false로 설정하면 검색 결과에 원본 문서를 포함하지 않습니다.
- 특정 필드만 포함: "_source": ["firstname", "lastname"]와 같이 배열로 필드를 지정합니다.
- 와일드카드 사용: "_source": "obj.*"와 같이 패턴을 사용하여 필드를 선택할 수 있습니다.
- 포함/제외 조합: includes와 excludes를 사용하여 더 복잡한 필터링이 가능합니다.
# 소스 비활성화
GET /my_index/_search
{
"_source": false,
"query": {
"match_all": {}
}
}
# 특정 필드 포함
GET /my_index/_search
{
"_source": ["firstname", "lastname"],
"query": {
"match_all": {}
}
}
# 중첩 객체의 필드 선택
GET /my_index/_search
{
"_source": "obj.*",
"query": {
"term": { "user": "kimchy" }
}
}
# 포함/제외
GET /my_index/_search
{
"_source": {
"includes": [ "obj1.*", "obj2.*" ],
"excludes": [ "*.description" ]
},
"query": {
"term": { "user": "kimchy" }
}
}
Pagination
from과 size 파라미터를 사용합니다. 기본적으로 최대 10,000개의 결과까지만 접근 가능합니다 (index.max_result_window 설정으로 제한).
GET /index_name/_search
{
"from": 0,
"size": 10,
"query": {
"match_all": {}
}
}
Sort
기본적으로 검색 결과를 관련성 점수(_score)에 따라 정렬합니다.
- 단일 필드 정렬: 특정 필드를 기준으로 오름차순(asc) 또는 내림차순(desc)으로 정렬
- 다중 필드 정렬: 여러 필드를 사용한 정렬 (순차적으로 적용)
- 스크립트 기반 정렬: 사용자 정의 스크립트를 사용한 정렬
# 다중 필드 정렬
GET /products/_search
{
"sort": [
{ "category": "asc" },
{ "price": "desc" }
]
}
# 스크립트 기반 정렬
# "kimchy"라는 사용자의 문서를 검색한 후, 각 문서의 field_name 필드 값에 1.1을 곱한 결과를 기준으로 오름차순 정렬합니다.
GET /_search
{
"query": { "term": { "user": "kimchy" } },
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "doc['field_name'].value * params.factor",
"params": { "factor": 1.1 }
},
"order": "asc"
}
}
}
[참고자료]