I am building a query to Elastic 5 (using nest in .net), i am trying to achive this result:
Must have value1 and value 2
Should have value3 or value 4
and should have value5 or value6
Here is my query:
{
"query": {
"bool": {
"must": [
{
"match": {
"code": {
"query": "value1"
}
}
},
{
"match": {
"code": {
"query": "value2"
}
}
}
],
"should": [
{
"match": {
"code": {
"query": "value3"
}
}
},
{
"match": {
"code": {
"query": "value4"
}
}
}
],
"should": [
{
"match": {
"code": {
"query": "value5"
}
}
},
{
"match": {
"code": {
"query": "value6"
}
}
}
],
"minimum_should_match": 1
}
}
}
I dont get the desired answer (for example i dont have anywhere value 5 and value 6 but still getting results)
Thank you
must means: Clauses that must match for the document to be included. should means: If these clauses match, they increase the _score ; otherwise, they have no effect. They are simply used to refine the relevance score for each document. Yes you can use multiple filters inside must .
Using must_not tells Elasticsearch that document matches cannot include any of the queries that fall under the must_not clause. should – It would be ideal for the matching documents to include all of the queries in the should clause, but they do not have to be included. Scoring is used to rank the matches.
Boolean queryedit. A query that matches documents matching boolean combinations of other queries. The bool query maps to Lucene BooleanQuery . It is built using one or more boolean clauses, each clause with a typed occurrence.
Boolean, or a bool query in Elasticsearch, is a type of search that allows you to combine conditions using Boolean conditions. Elasticsearch will search the document in the specified index and return all the records matching the combination of Boolean clauses.
Then you need something like this:
{
"query": {
"bool": {
"must": [
{
"match": {
"code": {
"query": "value1"
}
}
},
{
"match": {
"code": {
"query": "value2"
}
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"match": {
"code": {
"query": "value3"
}
}
},
{
"match": {
"code": {
"query": "value4"
}
}
}
]
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"match": {
"code": {
"query": "value5"
}
}
},
{
"match": {
"code": {
"query": "value6"
}
}
}
]
}
}
]
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With