Notebooks
E
Elastic
05 Query Rules

05 Query Rules

openai-chatgptlangchain-pythonchatgptgenaielasticsearchelasticopenaiAIchatlogvectordatabasenotebooksPythonsearchgenaistackvectorelasticsearch-labslangchainapplications

Search using query rules

Open In Colab

This interactive notebook will introduce you to how use query rules, using the official Elasticsearch Python client. You'll store query rules in Elasticsearch using the query rules API and query them using rule_query.

Create Elastic Cloud deployment

If you don't have an Elastic Cloud deployment, sign up here for a free trial.

Once logged in to your Elastic Cloud account, go to the Create deployment page and select Create deployment. Make sure the Elasticsearch version is 8.10.0 or newer. Leave all other settings with their default values.

Install packages and import modules

To get started, we'll need to connect to our Elastic deployment using the Python client. Because we're using an Elastic Cloud deployment, we'll use the Cloud ID to identify our deployment.

First we need to install the elasticsearch Python client.

[ ]

Initialize the Elasticsearch client

Now we can instantiate the Elasticsearch python client, providing the cloud id and password in your deployment.

[1]

If you're running Elasticsearch locally or self-managed, you can pass in the Elasticsearch host instead. Read more on how to connect to Elasticsearch locally.

Enable Telemetry

Knowing that you are using this notebook helps us decide where to invest our efforts to improve our products. We would like to ask you that you run the following code to let us gather anonymous usage statistics. See telemetry.py for details. Thank you!

[ ]

Test the Client

Before you continue, confirm that the client has connected with this test.

[3]
{'name': 'instance-0000000011', 'cluster_name': 'd1bd36862ce54c7b903e2aacd4cd7f0a', 'cluster_uuid': 'tIkh0X_UQKmMFQKSfUw-VQ', 'version': {'number': '8.11.1', 'build_flavor': 'default', 'build_type': 'docker', 'build_hash': '6f9ff581fbcde658e6f69d6ce03050f060d1fd0c', 'build_date': '2023-11-11T10:05:59.421038163Z', 'build_snapshot': False, 'lucene_version': '9.8.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}

Index some test data

Our client is set up and connected to our Elastic deployment. Now we need some data to test out the basics of Elasticsearch queries. We'll use a small index of products with the following fields:

  • name
  • description
  • price
  • currency
  • plug_type
  • voltage

Index test data

Run the following command to upload some sample data.

[4]
ObjectApiResponse({'errors': False, 'took': 10, 'items': [{'index': {'_index': 'products_index', '_id': 'us1', '_version': 2, 'result': 'updated', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 4, '_primary_term': 3, 'status': 200}}, {'index': {'_index': 'products_index', '_id': 'uk1', '_version': 2, 'result': 'updated', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 5, '_primary_term': 3, 'status': 200}}, {'index': {'_index': 'products_index', '_id': 'eu1', '_version': 2, 'result': 'updated', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 6, '_primary_term': 3, 'status': 200}}, {'index': {'_index': 'products_index', '_id': 'preview1', '_version': 2, 'result': 'updated', 'forced_refresh': True, '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 7, '_primary_term': 3, 'status': 200}}]})

Search the test data

First, let's search our data for a reliable wireless charger.

Before we search our data, we'll define some convenience functions to output the raw JSON responses from Elasticsearch into a more human-readable format.

[5]

Next, do the search:

[6]

ID: eu1
Name: PureJuice Pro - Wireless Charger suitable for European plugs
Description: PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.
Price: 18.0
Currency: EUR
Plug type: C
Voltage: 230V
Score: 14.5004

ID: preview1
Name: PureJuice Pro - Pre-order next version
Description: Newest version of the PureJuice Pro wireless charger, coming soon! The newest model of the PureJuice Pro boasts a 2x faster charge than the current model, and a sturdier cable with an eighteen month full warranty. We also have a battery backup to charge on-the-go, up to two full charges. Pre-order yours today!
Price: 36.0
Currency: USD
Plug type: ['B', 'C', 'G']
Voltage: ['230V', '120V']
Score: 1.0668641

ID: us1
Name: PureJuice Pro
Description: PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.
Price: 15.0
Currency: USD
Plug type: B
Voltage: 120v
Score: 0.11306982

ID: uk1
Name: PureJuice Pro - UK Compatible
Description: PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.
Price: 20.0
Currency: GBP
Plug type: G
Voltage: 230V
Score: 0.11306982

As we can see from the response, the European result is ranked first. This might not be desirable if, for example, I know that my searcher is coming from the US or the UK which have different plugs and specifications.

Query rules can help here!

Creating rules

Let's assume that separately, we know what country our users are coming from (perhaps geolocation from IP addresses or logged in user account information). Now, we want to create query rules to boost wireless chargers based on that information when people search for anything containing the phrase wireless charger.

[7]
ObjectApiResponse({'result': 'created'})

In order for these rules to match, one of the following must be true:

  • my_query contains the string "wireless charger" AND country is "us"
  • my_query contains the string "wireless charger" AND country is "uk"

We can view our ruleset using the API as well (with another pretty_ruleset function for readability):

[8]
Ruleset ID: promotion-rules

Rule ID: us-charger
	Type: pinned
	Criteria:
		my_query contains ['wireless charger']
		country exact ['us']
	Pinned ids: ['us1']

Rule ID: uk-charger
	Type: pinned
	Criteria:
		my_query contains ['wireless charger']
		country exact ['uk']
	Pinned ids: ['uk1']

Next, we use the rule_query to perform a search using the same organic query as above, but with the addition of query rules:

[9]

ID: us1
Name: PureJuice Pro
Description: PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.
Price: 15.0
Currency: USD
Plug type: B
Voltage: 120v
Score: 1.7014122e+38

ID: eu1
Name: PureJuice Pro - Wireless Charger suitable for European plugs
Description: PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.
Price: 18.0
Currency: EUR
Plug type: C
Voltage: 230V
Score: 14.5004

ID: preview1
Name: PureJuice Pro - Pre-order next version
Description: Newest version of the PureJuice Pro wireless charger, coming soon! The newest model of the PureJuice Pro boasts a 2x faster charge than the current model, and a sturdier cable with an eighteen month full warranty. We also have a battery backup to charge on-the-go, up to two full charges. Pre-order yours today!
Price: 36.0
Currency: USD
Plug type: ['B', 'C', 'G']
Voltage: ['230V', '120V']
Score: 1.0668641

ID: uk1
Name: PureJuice Pro - UK Compatible
Description: PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.
Price: 20.0
Currency: GBP
Plug type: G
Voltage: 230V
Score: 0.11306982

The rule query boosts the documents that we want to be displayed first.

Note that all criteria in a rule must match in order for a rule to be applied. If we update the country to be "ca" for example, neither query rule will be applied and we will return the organic, unmodified result set.

[10]

ID: eu1
Name: PureJuice Pro - Wireless Charger suitable for European plugs
Description: PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.
Price: 18.0
Currency: EUR
Plug type: C
Voltage: 230V
Score: 14.5004

ID: preview1
Name: PureJuice Pro - Pre-order next version
Description: Newest version of the PureJuice Pro wireless charger, coming soon! The newest model of the PureJuice Pro boasts a 2x faster charge than the current model, and a sturdier cable with an eighteen month full warranty. We also have a battery backup to charge on-the-go, up to two full charges. Pre-order yours today!
Price: 36.0
Currency: USD
Plug type: ['B', 'C', 'G']
Voltage: ['230V', '120V']
Score: 1.0668641

ID: us1
Name: PureJuice Pro
Description: PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.
Price: 15.0
Currency: USD
Plug type: B
Voltage: 120v
Score: 0.11306982

ID: uk1
Name: PureJuice Pro - UK Compatible
Description: PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.
Price: 20.0
Currency: GBP
Plug type: G
Voltage: 230V
Score: 0.11306982

It's also possible for multiple rules to apply to a single rule query. Let's update our ruleset, to always pin a promotional result for a pre-order page for our newest model.

Because rules are applied in order, we'll put the pre-order document at the beginning of the ruleset.

[11]
ObjectApiResponse({'result': 'updated'})

Now, doing a search from the UK will pin the pre-order page first, then pin the UK result second, before returning the organic search results.

[12]

ID: preview1
Name: PureJuice Pro - Pre-order next version
Description: Newest version of the PureJuice Pro wireless charger, coming soon! The newest model of the PureJuice Pro boasts a 2x faster charge than the current model, and a sturdier cable with an eighteen month full warranty. We also have a battery backup to charge on-the-go, up to two full charges. Pre-order yours today!
Price: 36.0
Currency: USD
Plug type: ['B', 'C', 'G']
Voltage: ['230V', '120V']
Score: 1.7014124e+38

ID: uk1
Name: PureJuice Pro - UK Compatible
Description: PureJuice Pro: Redefining wireless charging. Seamlessly merging swift charging capabilities with a refined aesthetic, it guarantees your devices receive rapid and secure power. Welcome to the next generation of charging.
Price: 20.0
Currency: GBP
Plug type: G
Voltage: 230V
Score: 1.7014122e+38

ID: eu1
Name: PureJuice Pro - Wireless Charger suitable for European plugs
Description: PureJuice Pro: Elevating wireless charging. Combining unparalleled charging speeds with elegant design, it promises both rapid and dependable energy for your devices. Embrace the future of wireless charging.
Price: 18.0
Currency: EUR
Plug type: C
Voltage: 230V
Score: 14.5004

ID: us1
Name: PureJuice Pro
Description: PureJuice Pro: Experience the pinnacle of wireless charging. Blending rapid charging tech with sleek design, it ensures your devices are powered swiftly and safely. The future of charging is here.
Price: 15.0
Currency: USD
Plug type: B
Voltage: 120v
Score: 0.11306982

These examples start to show the power of promoting documents based on contextual query metadata. For more information on how to get started using query rules, check out our blog post and search guide.