How to Query with Multiple Criteria in Elasticsearch
Introduction
In the real world, we often query data for more than one criteria. For instance, say within a user index you want to query males—that’ s one criteria. In addition, you want to query those males older than age 35—that’s the second criteria. What’s more, you’ll likely want to continue to further narrow your target data search to even more specific criteria. Well, you can query multiple criteria within Elasticsearch. This tutorial shows you step-by-step how it’s done. If you’re already familiar with multiple criteria querying, click here to go directly to Just the Code.
Add multiple criteria by using the bool data type
Use the bool
data type to combine two or more criteria. This way, you’ll be certain the results match what you need. The boolean data type, hence bool
, is the same logical AND (&&) operator in the computer programming language.
Here’s an example shown in JSON format. It illustrates how we can set up a dataset to see the bool
data type in action. We have a hypothetical supermarket. The index is store
. We have a data type in the index named products
. It lists the items available for sale in the store. The fields go across from left to right, starting with the header columns for “id” through “department.”
id | name | price | quantity | department | |
---|---|---|---|---|---|
1 | Multi-Grain Cereal | 4.99 | 4 | Packaged Foods | |
2 | 1lb Ground Beef | 3.99 | 29 | Meat and Seafood | |
3 | Dozen Apples | 2.49 | 12 | Produce | |
4 | Chocolate Bar | 1.29 | 2 | Packaged Foods | Checkout |
5 | 1 Gallon Milk | 3.29 | 16 | Dairy | |
6 | 0.5lb Jumbo Shrimp | 5.29 | 12 | Meat and Seafood | |
7 | Wheat Bread | 1.29 | 5 | Bakery | |
8 | Pepperoni Pizza | 2.99 | 5 | Frozen | |
9 | 12 Pack Cola | 5.29 | 6 | Packaged Foods | |
10 | Lime Juice | 0.99 | 20 | Produce | |
11 | 12 Pack Cherry Cola | 5.59 | 5 | Packaged Foods | |
12 | 1 Gallon Soy Milk | 3.39 | 10 | Dairy | |
13 | 1 Gallon Vanilla Soy Milk | 3.49 | 9 | Dairy | |
14 | 1 Gallon Orange Juice | 3.29 | 4 | Juice |
This is how it is outlined for our mapping order:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | curl -H "Content-Type: application/json" -XPUT 127.0.0.1:9200/store -d ' { "mappings": { "products": { "properties" : { "name": { "type": "text"}, "price": { "type": "double"}, "quantity": { "type": "integer"}, "department": { "type": "keyword"} } } } } ' |
Now we’re ready to conduct a query with multiple criteria. We want to show only products with a price of “less than $1.00” located in the “Produce” department. We already know the answer: lime juice. Let’s test it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/store/_search?pretty" -d ' { "query": { "bool": { "must": {"match": {"department": "Produce"}}, "filter": { "range": {"price": {"lt": 1.00}}} } } } ' { "took" : 70, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.3862944, "hits" : [ { "_index" : "store", "_type" : "products", "_id" : "10", "_score" : 1.3862944, "_source" : { "id" : "10", "name" : "Lime Juice", "price" : 0.99, "quantity" : 20, "department" : [ "Produce" ] } } ] } } |
We’re correct. Lime juice it is. That’s because we used the bool
data type to combine multiple criteria. There are four types of occurrences in bool.
Three are perceptive and the fourth, not so much. The intuitive ones are filter
, must
, must_not
. The last one is should
, and it isn’t relevant for our purposes here. Just to satisfy your curiosity though, the should
occurrence has no effect on querying multiple criteria unless you’re trying to define a relevance score. Even in that case, to add more attributes, the filter
occurrence may be better suited.
The bool
occurrences we use most often are:
must
– Our criteria must state what the results must have. In the above example, we queried thedepartment
for “Produce” only.must_not
– By contrast, we could omit all Produce in the results by using the following schema.
1 | "must_not": {"match": {"department": "Produce"}}, |
filter
let us use a range to filter our results.1"filter": { "range": {"price": {"lt": 1.00}}}
You can keep filtering using the occurrences in bool
as needed. It’s the easiest way to drill down and return your desired results when making queries with multiple criteria.
Conclusion
Queries aren’t just singular. It’s helpful to make queries with multiple criteria every day to understand our target markets, upsell to existing clients, and discover new opportunities for expanding business in undiscovered territories. This tutorial was designed to show you the possibilities of querying in Elasticsearch using bool
. Find out more about the bool
data type and Elasticsearch by reading some additional documentation.
Just the Code
Here’s the code if you already know about querying with multiple criteria and want to see an example in Elasticsearch.
1 2 3 4 5 6 7 8 9 | curl -H "Content-Type: application/json" -XGET "127.0.0.1:9200/store/_search?pretty" -d ' { "query": { "bool": { "must": {"match": {"department": "Produce"}}, "filter": { "range": {"price": {"lt": 1.00}}} } } } |
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started