preface
Search for Elasticsearch when searching for Elasticsearch, you can find Elasticsearch for Elasticsearch if it’s starred by Stephen Chow, has a score of 8 or above, and dates from 1990 to 2001. Here we can use a bool query to implement the requirement. This query combines multiple queries into the bool query that the user wants.
Boolean query
A bool query that can contain one or more query statements.
There are four parameters
- Must: Documents must match these criteria to be included. Contribution counts.
- Should: documents are selectively matched. If any of these statements are met, _score will be added; otherwise, there is no effect. Contribution counts.
- Must_not: Documents must not match these conditions to be included.
- Filter: Must match, but it is carried out in unrated, filtered mode. These statements do not contribute to the rating, but simply exclude or include documents based on filtering criteria. No contribution counts.
The basic grammar
- Subqueries inside bool continue to nest bool queries
- Subqueries can appear in any order
- If there is no must statement, you need to be able to match at least one of the should statements. However, if there is at least one MUST statement, there is no requirement for the should statement to match.
- Must and so on can be followed by an object (” {} “) or an array (” [] “)
{ "bool": { "must": { "match": { "title": "how to make millions" }}, "must_not": { "match": { "tag": "spam" }}, "should": [ { "match": { "tag": "starred" }} ], "filter": { "bool": { "must": [ { "range": { "date": {" gte ":" 2014-01-01 "}}}, {" range ": {" price" : {" lte ": 29.99}}}]," must_not ": [{" term" : {" category ": "ebooks" }} ] } } } }Copy the code
An example of a flight query, search for flights to the United States, the local weather is clear, not from Japan, the ticket price is less than or equal to 1000 flights.
GET kibana_sample_data_flights/_search
{
"size": 5,
"query": {
"bool": {
"must": [
{
"term": {
"DestCountry": "US"
}
},
{
"term": {
"DestWeather": "Sunny"
}
}
],
"must_not": {
"term": {
"OriginCountry": "JP"
}
},
"filter": {
"range": {
"AvgTicketPrice": {
"lte": 1000
}
}
}
}
}
}
Copy the code
Control correlation
So how can we affect the correlation score of a query with multiple fields?
Nested hierarchy
Fields at the same level are competitive and have the same weight, so their influence on scoring can be changed through nesting.
GET animals/_search
{
"query": {
"bool": {
"should": [
{"term": {"text": "brown"}},
{"term": {"text": "red"}},
{"term": {"text": "quick"}},
{"term": {"text": "dog"}}
]
}
}
}
GET animals/_search
{
"query": {
"bool": {
"should": [
{"term": {"text": "brown"}},
{"term": {"text": "red"}},
{"bool": {
"should": [
{"term": {"text": "quick"}},
{"term": {"text": "dog"}}
]
}
}
]
}
}
}
Copy the code
boosting
Boosting controls the weight of the fields. You can use boosting, which defaults to 1 and can be positive or negative.
- When boost>1, the relative correlation of ratings increased
- When 0
- When boost<0, contribution is negative
You can set boost weights in match, such as DestCountry.
GET kibana_sample_data_flights/_search
{
"explain": true,
"size": 5,
"query": {
"bool": {
"must": [
{
"match": {
"DestCountry": {
"query": "US",
"boost": 10
}
}
},
{
"term": {
"DestWeather": "Sunny"
}
}
],
"must_not": {
"term": {
"OriginCountry": "JP"
}
},
"filter": {
"range": {
"AvgTicketPrice": {
"lte": 1000
}
}
}
}
}
}
Copy the code
Boosting complete Boosting syntax, with positive, negative, and negative_boost weights, can be used to demote matching documents, unlike the “NOT” logical operation that directly removes related documents
GET movies/_search { //"explain": true, "query": { "boosting": { "positive": { "term": { "title": { "value": "Beautiful"}}}, "negative" : {" term ": {" title" : {" value ":" mind "}}}, "negative_boost" : 0.2}}}Copy the code
Constant_score query
Although not as frequently used as bool queries, the constant_score query is a useful query tool in our toolbox. It applies a constant constant score to all matching documents. It is often used when you only need to execute a filter and no other queries (for example, scoring queries).
GET movies/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"title": "beautiful"
}
}
}
}
}
Copy the code
The resources
- www.elastic.co/guide/cn/el…
- Elasticsearch Core Technology and Actual Combat (Ruan Yiming — Geek Time)