Must match multiple values

dontberude

I have a query that works fine when I need the property of a document to match just one value.

However I also need to be able to search with must with two values.

So if a banana has id 1 and a lemon has id 2 and I search for yellow I will get both if I have 1 and 2 in the must clause. But if i have just 1 I will only get the banana.

{
  "from": 0, 
  "size": 20,
  "query": {
  "bool": {
    "should": [
      { "match": 
        { "fruit.color":  "yellow" }}
    ],
    "must" : [ 
      { "match": { "fruit.id" : "1" } }
    ]
  }
 } 
}

I haven´t found a way to search with two values with must. is that possible?

BrookeB

If the document "must" be returned only if the id is 1 or 2, that sounds like another should clause. If I'm understanding your question properly, you want documents with either id 1 OR id 2. Additionally, if the color is yellow, give it a higher score.

Here's one way you might achieve what you're looking for:

{
  "query": {
    "bool": {
      "should": {
        "match": {
          "fruit.color": "yellow"
        }
      },
      "must": {
        "bool": {
          "should": [
            {
              "match": {
                "fruit.id": "1"
              }
            },
            {
              "match": {
                "fruit.id": "2"
              }
            }
          ]
        }
      }
    }
  }
}

Here I put the two match queries in the should clause of a separate bool query. This achieves the OR behavior you are looking for.

Have another look at the Bool Query documentation and take note of the nuances of should. It behaves differently by default depending on whether or not there is a sibling must clause and whether or not the bool query is being executed in filter context.

Another key option that is adjustable and can help you achieve your expected results is the minimum_should_match parameter. Have a look at this documentation page.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Match multiple values in array

Match values on multiple rows then match differing values

Use Index Match Match To Find Multiple Values

Pattern match multiple values in a sentence

Best match for multiple values in MySQL

Match multiple values in a case of a switch

Partial match in excel, multiple values in match criteria to match against list

Using Regex to match on multiple possible values unless ignored values match

Values must match the following regular expression: '\d+' error

gcloud stop exception - regex / Values must match the following regular expression

Join tables using IN clause where must match ALL values

How to perform SELECT where a column must match ALL values?

regex with multiple conditional groups in lookahead that must also be captured in match

MySQL key value schema, returning results that must match multiple keys

solr - string search field multiple value, all word must match

Select rows that match values in multiple columns in pandas

Elasticsearch multiple values match without analyzer

Python regex to match multiple unique values

Sum multiple values in Index/Match function

How to match multiple columns and get values in Excel

Match certain column values with other multiple

Match multiple keys values to database entry in TinyDB?

Match values of multiple columns by using 2 columns

searching datatable with multiple values not returning exact match

Elasticsearch query match multiple values to single field

Multiple columns/values with index/match in Excel

Replace column values that match multiple different patterns

JavaScript match multiple values in comma separated strings

Search with match() trough multiple values in an array