[ES] Query
Basic Concept: https://logz.io/blog/elasticsearch-queries/
42 query examples: https://coralogix.com/blog/42-elasticsearch-query-examples-hands-on-tutorial/
Concepts
- Match Query - 返回哪些text中包含搜索的关键词
-
example query
POST employees/_search { "query": { "match": { "phrase": { "query" : "heuristic" } } } }
-
Search phrase instead of a word
POST employees/_search { "query": { "match": { "phrase": { "query" : "heuristic roots help", "operator" : "AND", "minimum_should_match": 3 } } } }
-
Multi Fields Query
POST employees/_search { "query": { "multi_match": { "query" : "research help" , "fields": ["position","phrase"] } } }
-
Match phrase
GET employees/_search { "query": { "match_phrase": { "phrase": { "query": "roots heuristic coherent", "slop": 1 } } } }
-
Match phrase prefix
GET employees/_search { "_source": [ "phrase" ], "query": { "match_phrase_prefix": { "phrase": { "query": "roots heuristic co", "max_expansions": 60 } } } }
-
- Term Query - 返回文本中的精确查找
-
Query exact values
POST employees/_search { "query": { "terms": { "gender": [ "female", "male" ] } } }
-
Exist query
GET employees/_search { "query": { "exists": { "field": "company" } } }
-
Range query
POST employees/_search { "query": { "range" : { "experience" : { "gte" : 5, "lte" : 10 } } } }
-
Id query
POST employees/_search { "query": { "ids" : { "values" : ["1", "4"] } } }
-
Prefix query
GET employees/_search { "query": { "prefix": { "name": "al" } } }
-
Wildcard Query
GET employees/_search { "query": { "wildcard": { "country": { "value": "c*a" } } } }
-
Regex query
GET employees/_search { "query": { "regexp": { "position": "res[a-z]*" } } }
-
Fuzzy query
GET employees/_search { "query": { "fuzzy": { "country": { "value": "Chnia", "fuzziness": "2" } } } }
-
-
Boosting
POST employees/_search { "query": { "multi_match" : { "query" : "versatile Engineer", "fields": ["position^3", "phrase"] } } }
- Sort
-
Sort by a field
GET employees/_search { "_source": ["name","experience","salary"], "sort": [ { "experience": { "order": "desc" } } ] }
-
Sort by multiple field
GET employees/_search { "_source": [ "name", "experience", "salary" ], "sort": [ { "experience": { "order": "desc" } }, { "salary": { "order": "desc" } } ] }
-
- Compound query
-
Bool 下面一共可以放这些字段
must The conditions or queries in this must occur in the documents to consider them a match. Also, this contributes to the score value. Eg: if we keep query A and query B in the must section, each document in the result would satisfy both the queries, ie query A AND query B should The conditions/queries should match. Result = query A OR query B filter Same as the must clause, but the score will be ignored must_not The conditions/queries specified must not occur in the documents. Scoring is ignored and kept as 0 as the results are ignored. POST _search { "query": { "bool" : { "must" : [], "filter": [], "must_not" : [], "should" : [] } } }
-
Multiple conditions
POST employees/_search { "query": { "bool": { "must": [ { "match": { "position": "manager" } }, { "range": { "experience": { "gte": 12 } } } ], "should": [ { "match": { "phrase": "versatile" } } ] } } }
-
Condition combo
如果要查询下面这样的SQL,对应的condition是?
SQL: (company = Yamaha OR company = Yozio ) AND (position = manager OR position = associate ) AND (salary>=100000)
POST employees/_search { "query": { "bool": { "must": [ { "bool": { "should": [{ "match": { "company": "Talane" } }, { "match": { "company": "Yamaha" } }] } }, { "bool": { "should": [ { "match": { "position": "manager" } }, { "match": { "position": "Associate" } } ] } }, { "bool": { "must": [ { "range": { "salary": { "gte": 100000 } } } ] } }] } } }
- Boosting
POST employees/_search { "query": { "boosting" : { "positive" : { "match": { "country": "china" } }, "negative" : { "match": { "company": "Talane" } }, "negative_boost" : 0.5 } } }
- Function score
- Change the score of search documents by weight
GET employees/_search { "_source": ["position","phrase"], "query": { "function_score": { "query": { "match": { "position": "manager" } }, "functions": [ { "filter": { "match": { "phrase": "coherent" } }, "weight": 2 }, { "filter": { "match": { "phrase": "emulation" } }, "weight": 10 } ], "score_mode": "multiply", "boost": "5", "boost_mode": "multiply" } } }
- Change the score of search documents by weight
-