Percolate queries help you find which stored queries match a new document. It is like searching backwards: instead of finding documents for a query, you find queries for a document.
Percolate queries (reverse search) in Elasticsearch
Start learning this pattern below
Jump into concepts and practice - no test required
{
"query": {
"percolate": {
"field": "query_field",
"document": {
"field1": "value1",
"field2": "value2"
}
}
}
}The field is where your stored queries live in the index mapping.
The document is the new data you want to test against those queries.
{
"query": {
"percolate": {
"field": "query",
"document": {
"message": "Elasticsearch is cool"
}
}
}
}{
"query": {
"percolate": {
"field": "query",
"document": {
"user": "alice",
"action": "login"
}
}
}
}This program creates an index with a percolator field, stores a query that matches documents containing "error", then percolates a new document with a message containing "error" to find matching queries.
PUT /my-index
{
"mappings": {
"properties": {
"query": {
"type": "percolator"
},
"message": {
"type": "text"
}
}
}
}
# Index a stored query
PUT /my-index/_doc/1
{
"query": {
"match": {
"message": "error"
}
}
}
# Percolate a new document to find matching queries
GET /my-index/_search
{
"query": {
"percolate": {
"field": "query",
"document": {
"message": "There was an error in the system"
}
}
}
}Make sure your index mapping includes a percolator type field to store queries.
Percolate queries are useful for alerting and matching new data against saved conditions.
Stored queries must be indexed before you can percolate documents against them.
Percolate queries let you find stored queries that match a new document.
You need a special percolator field in your index mapping to store queries.
Use percolate queries to build alerting and reverse search features.
Practice
What is the main purpose of a percolate query in Elasticsearch?
Solution
Step 1: Understand percolate query concept
A percolate query is used to find stored queries that match a new document, reversing the usual search direction.Step 2: Compare options with concept
The other options describe other Elasticsearch operations, not percolate queries.Final Answer:
To find stored queries that match a new document -> Option AQuick Check:
Percolate query = find matching stored queries [OK]
- Confusing percolate query with regular search
- Thinking it updates or deletes documents
- Mixing it with aggregation queries
Which mapping type must be included in an Elasticsearch index to use percolate queries?
{
"mappings": {
"properties": {
"query": {
"type": "???"
}
}
}
}Solution
Step 1: Identify required field type for percolate queries
Elasticsearch requires a special field type called "percolator" to store queries for percolate queries.Step 2: Match options with required type
Only "percolator" uses "percolator" type; others are for different purposes.Final Answer:
"percolator" -> Option AQuick Check:
Percolate field type = "percolator" [OK]
- Using "text" or "keyword" instead of "percolator"
- Confusing nested type with percolator
- Omitting the percolator field in mapping
Given the following percolate query, what will it return?
{
"query": {
"percolate": {
"field": "query",
"document": {
"message": "Elasticsearch alerting"
}
}
}
}Assuming the index has stored queries matching documents containing "alerting".
Solution
Step 1: Understand percolate query behavior
The percolate query matches stored queries against the provided document, returning matching stored queries.Step 2: Analyze the given query
The query uses "document" with a message field; it will find stored queries matching this document's content.Final Answer:
Stored queries that match the document with message "Elasticsearch alerting" -> Option DQuick Check:
Percolate query returns matching stored queries [OK]
- Thinking it returns documents instead of queries
- Assuming document ID is required for percolate query
- Confusing percolate with regular search
Identify the error in this percolate query:
{
"query": {
"percolate": {
"field": "query"
"document": {
"content": "Test document"
}
}
}
}Solution
Step 1: Check JSON syntax in query
Between "field" and "document" keys, a comma is missing, causing invalid JSON.Step 2: Validate other parts
"field" name is correct, "document" can omit "id", and "content" is valid as document content.Final Answer:
Missing comma between "field" and "document" fields -> Option BQuick Check:
JSON syntax error = missing comma [OK]
- Forgetting commas between JSON keys
- Assuming document must have an ID
- Changing field names unnecessarily
You want to build an alert system that triggers when new documents match any stored queries. Which steps are necessary to implement this using percolate queries?
Solution
Step 1: Setup index with percolator field
Define an index mapping with a "percolator" type field to store queries for reverse matching.Step 2: Store queries and percolate new documents
Index the alert queries into the percolator field, then use percolate queries to check if new documents match any stored queries.Final Answer:
Create an index with a percolator field, store queries, then percolate new documents against stored queries -> Option CQuick Check:
Percolate queries enable alerting by matching docs to stored queries [OK]
- Using regular search instead of percolate queries
- Not defining percolator field in mapping
- Trying to use aggregations for alerting
