Han Yi
April 30, 2018
GET products/_doc/_search?q=rating:5
POST products/_doc/_search
{
"query": {
"nested":{
"path":"child",
"score_mode":"max",
"query":{
"simple_query_string":{
"query":"red hat",
"fields":[
"child.color^2.0",
"child.name^1.0",
]
}
}
}
}
}
//1. filter inner bool query
POST context/_doc/_search
{
"query": {
"bool": {
"must": {
"match": {
"name": "red hat"
}
},
"filter": {
"term": {
"context": "generic"
}
}
}
}
}
//2. constant_score
POST products/_doc/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"status": "active"
}
}
}
}
}
//3. aggregation filter
POST products/_doc/_search
{
"aggs": {
"nested_aggs": {
"nested": {
"path":"child"
},
"aggs": {
"filtered_aggs": {
"filter": {
"bool": {
"must": [
{
"term": {
"child.color":"red"
}
}
]
}
},
"aggs": {
"lvl": {
"terms": {
"field": "child.lvl",
"order": {
"count":"desc"
}
},
"aggs": {
"count": {
"reverse_nested":{}
}
}
}
}
}
}
}
}
}
//4. Score functions
POST products/_doc/_search
{
"query": {
"function_score": {
"query": { "match_all": {} },
"boost": "5",
"functions": [
{
"filter": { "match": { "test": "bar" } },
"random_score": {},
"weight": 23
},
{
"filter": { "match": { "test": "cat" } },
"weight": 42
}
],
"max_boost": 42,
"score_mode": "max",
"boost_mode": "multiply",
"min_score" : 42
}
}
}
//1. term vs terms query
POST expmgrrules/doc/_search
{
"query": {
"term": {
"triggers": "26972"
}
}
}
POST expmgrrules/doc/_search
{
"query": {
"terms": {
"triggers": ["26972","20676"]
}
}
}
//2. range query
POST products/doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"range": {
"child.price": {
"lte": 5
}
}
}
}
}
}
//3. range with date query
POST products/doc/_search
{
"from": 0,
"size": 24,
"query": {
"nested": {
"path": "path",
"query": {
"range": {
"releaseDate": {
"gt": "now-1h"
}
}
}
}
}
}
//4. exists query
POST products/doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"exists": {
"field": "special"
}
}
}
}
}
//5. wildcard/regex query
//* means any match, ? means single match
POST products/doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"wildcard": {
"name": "boys*"
}
}
}
}
}
//1. basic match
POST products/_doc/_search
{
"query": {
"nested":{
"path":"child",
"query":{
"match": {
"child.name": "men hat"
}
}
}
}
}
//2. complete match with the operator
POST products/_doc/_search
{
"query": {
"nested":{
"path":"child",
"query":{
"match": {
"child.name": {
"query": "men hat",
"operator": "AND"
}
}
}
}
}
}
//3. multi match
POST products/_doc/_search
{
"query": {
"nested":{
"path":"child",
"query":{
"multi_match": {
"query": "red hat",
"fields": ["child.name^1.0", "child.color^2.0"]
}
}
}
}
}
//4. match phrase
POST products/doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"match_phrase": {
"child.name": {
"query": "men hat",
"slop": 2
}
}
}
}
}
}
//1. query string
POST products/_doc/_search
{
"query": {
"nested":{
"path":"child",
"score_mode":"max",
"query":{
"query_string":{
"query":"(red hat) OR (blue hat)",
"fields":[
"child.color^2.0",
"child.name^1.0",
]
}
}
}
}
}
//2. simple query string
POST products/_doc/_search
{
"query": {
"nested":{
"path":"child",
"score_mode":"max",
"query":{
"simple_query_string":{
"query":"(red hat) OR (blue hat)",
"fields":[
"child.color^2.0",
"child.name^1.0",
]
}
}
}
}
}
{
"query": {
"bool": {
"must": [],
"must_not": [],
"should": [],
"filter": [],
}
}
}
GET products/_doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"function_score": {
"query": {
"match": {
"child.color": "red"
}
},
"boost": "1",
"random_score": {},
"boost_mode":"multiply"
}
}
}
}
}
GET products/_doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"function_score": {
"query": {
"match": {
"child.color": "red"
}
},
"functions": [{
"filter": {
"match": {
"child.lvl1": "men"
}
},
"weight": 2
}, {
"filter": {
"match": {
"child.lvl1": "women"
}
},
"weight": 5
}],
"boost": "1",
"boost_mode":"multiply"
}
}
}
}
}
GET products/_doc/_search
{
"query": {
"nested": {
"path": "child",
"query": {
"function_score": {
"query": {
"match": {
"child.color": "red"
}
},
"script_score" : {
"script" : {
"source": "doc['child.price'].value"
}
},
"boost": 0.1,
"boost_mode":"sum"
}
}
}
}
}
GET products/_doc/_search
{
"query": {
"bool": {
"must": {
"term": {
"name": "jacket"
}
},
"should": [
{
"range": {
"price": {
"lte": 20,
"boost": 0.5
}
}
},
{
"range": {
"price": {
"lte": 15,
"boost": 2
}
}
}
]
}
}
}
POST products/doc/_search
{
"query": {
"nested": {
"type": "child",
"query": {
"match_all": {}
}
}
},
"rescore": {
"window_size" : 500,
"query": {
"rescore_query" : {
"nested": {
"type": "child",
"query": {
"function_score": {
"script_score": {
"script": {
"inline": "doc['price'].value"
}
}
}
}
}
}
}
}
}
GET /products/_doc/{_id}/_explain
POST products/_doc/_search
{
"explain": true,
"query": {
//......
}
}
POST products/doc/_search
{
"from": 0,
"size": 24,
"_source": ["pro*"],
"query": {
"nested": {
"path": "child",
"query": {
"term": {
"child.productName": "hat"
}
}
}
}
}
POST products/doc/_search
{
"from": 0,
"size": 24,
"_source": [],
"script_fields": {
"max_price_including_unit": {
"script": {
"inline": "'$' + params['_source']['price']"
}
}
},
"query": {
"nested": {
"path": "child",
"query": {
"term": {
"name": "hat"
}
}
}
}
}
POST products/doc/_search
{
"from": 0,
"size": 24,
"query": {
"nested": {
"path": "child",
"query": {
"term": {
"child.name": "hat"
}
}
}
},
"sort": [{
"child.price": "asc"
}, {
"child.no": "asc"
}]
}
POST products/doc/_search
{
"from": 0,
"size": 24,
"query": {
"nested": {
"path": "child",
"query": {
"term": {
"name": "hat"
}
}
}
}
}
POST products/doc/_search
{
"query": {
"nested" : {
"type" : "child",
"score_mode" : "sum",
"query": {
"simple_query_string" : {
"fields" : ["color^2", "size^1"],
"query": "Red"
}
},
"inner_hits": {
"size": 1,
"highlight": {
"fields" : {
"color" : {},
"size" : {},
}
}
}
}
}
}