GET /product/_search?q=name:past
요런 식으로도 search가능하다.GET /product/_search
{
"query": {
"query_string": {
"query": "name:pasta"
}
}
}
GET /products/_search?q=*
GET /products/_search?q=name:Lobster
GET /products/_search?q=tags:Meat AND name:Tuna
leaf query, compound query 두 종류가 있다.(용어 자체는 중요치 않다)
leaf query는 특정 필드의 값을 search한다.
compound query는 여러개의 leaf query로 이뤄져있다. (recursive하다는데 아직은 이해 안감)
아래 예시를 보면 leaf 쿼리는 category가 Fruit인 doc을 찾는다.
compound query는 bool query를 날리고 있는데, category=Fruit이거나 Vegetable인 doc을 찾는다.
key로 쿼리의 이름(match_all)을, value로 쿼리문을 날리는게 일반적이다.
GET /reviews/_search
{
"query": {
"match_all": {}
}
}
query 수행 속도. (밀리세컨)
매칭된 doc 개수
GET /products/_search
{"query": {
"term": {
"is_active": true
}
}}
GET /products/_search
{"query": {
"term": {
"is_active": {
"value": true
}
}
}}
GET /products/_search
{
"query": {
"terms": {
"tags.keyword": [ "Soup", "Cake" ]
}
}
}
GET /products/_search
{
"query": {
"ids": {
"values": [ 1, 2, 3 ]
}
}
}
GET /products/_search
{
"query": {
"range": {
"in_stock": {
"gte": 1,
"lte": 5
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "2010/01/01",
"lte": "2010/12/31"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "01-01-2010",
"lte": "31-12-2010",
"format": "dd-MM-yyyy"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "2010/01/01||-1y"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "2010/01/01||-1y-1d"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "2010/01/01||-1y/M"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "2010/01/01||/M-1y"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "now/M-1y"
}
}
}
}
GET /products/_search
{
"query": {
"range": {
"created": {
"gte": "now"
}
}
}
}
GET /products/_search
{
"query": {
"exists": {
"field": "tags"
}
}
}
GET /products/_search
{
"query": {
"prefix": {
"tags.keyword": "Vege"
}
}
GET /products/_search
{
"query": {
"wildcard": {
"tags.keyword": "Veg*ble"
}
}
}
GET /products/_search
{
"query": {
"wildcard": {
"tags.keyword": "Veg?ble"
}
}
}
GET /products/_search
{
"query": {
"wildcard": {
"tags.keyword": "Veget?ble"
}
}
}
GET /products/_search
{
"query": {
"regexp": {
"tags.keyword": "Veg[a-zA-Z]+ble"
}
}
}
GET /recipe/_search
{
"query": {
"match": {
"title": "Recipes with pasta or spaghetti"
}
}
}
모든 단어가 들어간 결과만 리턴된다.
GET /recipe/_search
{
"query": {
"match": {
"title": {
"query": "Recipes with pasta or spaghetti",
"operator": "and"
}
}
}
}
GET /recipe/_search
{
"query": {
"match": {
"title": {
"query": "pasta or spaghetti",
"operator": "and"
}
}
}
}
GET /recipe/_search
{
"query": {
"match": {
"title": {
"query": "pasta spaghetti",
"operator": "and"
}
}
}
}
GET /recipe/_search
{
"query": {
"match_phrase": {
"title": "spaghetti puttanesca"
}
}
}
GET /recipe/_search
{
"query": {
"match_phrase": {
"title": "puttanesca spaghetti"
}
}
}
GET /recipe/_search
{
"query": {
"multi_match": {
"query": "pasta",
"fields": ["title", "description"]
}
GET /recipe/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"ingredients.name": "parmesan"
}
},
{
"range": {
"preparation_time_minutes": {
"lte": 15
}
}
}
]
}
}
}
GET /recipe/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"ingredients.name": "parmesan"
}
}
],
"filter": [
{
"range": {
"preparation_time_minutes": {
"lte": 15
}
}
}
]
}
}
}
GET /recipe/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"ingredients.name": "parmesan"
}
}
],
"must_not": [
{
"match": {
"ingredients.name": "tuna"
}
}
],
"filter": [
{
"range": {
"preparation_time_minutes": {
"lte": 15
}
}
}
]
}
}
}
if the bool query is in a query context and contains must or filter objects, then the should query do not need to match for a doc to measure bool query as a whole. 이경우, should query의 유일한 목적은 relevance score에 영향을 주는 것이다.
On the other hand, if the bool query is in the filter context or if it doesn't have a must or filter object, then at least one of the should queries must match that because the should queries are intended for boosting documents matching a set of queries.
But the documents should still satisfy some constraints.
Otherwise, every single document in an index would be matched.
GET /recipe/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"ingredients.name": "parmesan"
}
}
],
"must_not": [
{
"match": {
"ingredients.name": "tuna"
}
}
],
"should": [
{
"match": {
"ingredients.name": "parsley"
}
}
],
"filter": [
{
"range": {
"preparation_time_minutes": {
"lte": 15
}
}
}
]
}
}
}
GET /recipe/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"ingredients.name": "pasta"
}
}
],
"should": [
{
"match": {
"ingredients.name": "parmesan"
}
}
]
}
}
}
GET /recipe/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"ingredients.name": "parmesan"
}
}
]
}
}
}
GET /recipe/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"ingredients.name": {
"query": "parmesan",
"_name": "parmesan_must"
}
}
}
],
"must_not": [
{
"match": {
"ingredients.name": {
"query": "tuna",
"_name": "tuna_must_not"
}
}
}
],
"should": [
{
"match": {
"ingredients.name": {
"query": "parsley",
"_name": "parsley_should"
}
}
}
],
"filter": [
{
"range": {
"preparation_time_minutes": {
"lte": 15,
"_name": "prep_time_filter"
}
}
}
]
}
}
}
"matched_queries" : [
"prep_time_filter",
"parmesan_must",
"parsley_should"
]
"matched_queries" : [
"prep_time_filter",
"parmesan_must"
]