Elastcisearch 검색에 사용되는 주요 쿼리들을 살펴보도록 하겠음
예제들을 실행하기 위해 my_index 인덱스에 다음의 5개 도큐먼트를 먼저 입력
$ curl -XPOST "<http://localhost:9200/my_index/_bulk>" -H 'Content-Type: application/json' -d'
{"index":{"_id":1}}
{"message":"The quick brown fox"}
{"index":{"_id":2}}
{"message":"The quick brown fox jumps over the lazy dog"}
{"index":{"_id":3}}
{"message":"The quick brown fox jumps over the quick dog"}
{"index":{"_id":4}}
{"message":"Brown fox brown dog"}
{"index":{"_id":5}}
{"message":"Lazy jumping dog"}
'
match_all 은 별다른 조건 없이 해당 인덱스의 모든 도큐먼트를 검색하는 쿼리
검색 시 쿼리를 넣지 않으면 elasticsearch는 자동으로 match_all을 적용해서 해당 인덱스의 모든 도큐먼트를 검색
다음 두 예제는 결과가 동일함
$ curl -XGET "<http://localhost:9200/my_index/_search>"
$ curl -XGET "<http://localhost:9200/my_index/_search>" -H 'Content-Type: application/json' -d'
{
"query":{
"match_all":{}
}
}
'
풀 텍스트 검색에 사용되는 가장 일반적인 쿼리
다음은 match 쿼리를 이용하여 my_index 인덱스의 message 필드에 dog 가 포함되어 있는 모든 문서를 검색함
$ curl -XGET "<http://localhost:9200/my_index/_search>" -H 'Content-Type: application/json' -d'
{
"query":{
"match_all":{
"message": "dog"
}
}
}
'
match 검색에 여러 개의 검색어를 집어넣게 되면 디폴트로 OR 조건으로 검색이 되어 입력된 검색어 별로 하나라도 포함된 모든 문서를 모두 검색함
다음은 검색어로 quick dog 를 검색 한 결과
$ curl -XGET "<http://localhost:9200/my_index/_search>" -H 'Content-Type: application/json' -d'
{
"query":{
"match_all":{
"message": "quick dog"
}
}
}
'