Skip to content

Latest commit

 

History

History
71 lines (58 loc) · 1.75 KB

File metadata and controls

71 lines (58 loc) · 1.75 KB

Finding multiple exact values

The term filter is useful for finding a single value, but often you’ll want to search for multiple values. What if you want to find documents that have a price of $20 or $30?

Rather than using multiple term filters, you can instead use a single terms filter (note the `s'' at the end). The `terms filter is simply the plural version of the singular term filter.

It looks nearly identical to a vanilla term too. Instead of specifying a single price, we are now specifying an array of values:

{
    "terms" : {
        "price" : [20, 30]
    }
}

And like the term filter, we will place it inside of a filtered query to actually use it:

GET /my_store/products/_search
{
    "query" : {
        "filtered" : {
            "filter" : {
                "terms" : { (1)
                    "price" : [20, 30]
                }
            }
        }
    }
}
  1. The terms filter as seen above, but placed inside the filtered query

The query will return the second and third documents:

"hits" : [
    {
        "_id" :    "2",
        "_score" : 1.0,
        "_source" : {
          "price" :     20,
          "productID" : "KDKE-B-9947-#kL5"
        }
    },
    {
        "_id" :    "3",
        "_score" : 1.0,
        "_source" : {
          "price" :     30,
          "productID" : "JODL-X-1937-#pV7"
        }
    }
]