liveBook · Manning

Percolate query

As explained in Elasticsearch in Action, Second Edition

An Elasticsearch percolate query matches stored queries against a supplied document.

A percolate query reverses normal search: instead of running a query against documents, it searches stored queries using a supplied document. This supports alerts when previously unavailable items become available. section 12.9.4

Create an index for stored queries, map its query field as type percolator, save a query such as name matching Python, and submit a candidate document. section 12.9.4

Finding a query that matches a newly stocked book

PUT tech_books_percolator { "mappings": { "properties": { "query": { "type": "percolator" }, "name": { "type": "text" }, "tags": { "type": "text" } } } } PUT tech_books_percolator/_doc/1 { "query": { "match": { "name": "Python" } } } GET tech_books_percolator/_search { "query": { "percolate": { "field": "query", "document": { "name": "Python in Action", "tags": ["Python", "Software Programming"] } } } } The percolate search tests the supplied book against the stored match query, identifying the user criterion that now has a result. section 12.9.4

Listing 12.32 Searching for queries in the percolator index
GET tech_books_percolator/_search
{
  "query": {
    "percolate": {          #1
      "field": "query",     #2
      "document": {         #3
        "name":"Python in Action",
        "tags":["Python","Software Programming"]
      }
    }
  }
}

Inspect the complete REST request that searches the percolator index for matching stored queries. section 12.9.4

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains the reversed search model, percolator mapping, stored query, and matching document, without detailing execution internals.
Examples
Uses a technical-books inventory, a missing Python title, a stored name query, and a newly indexed book document.
What it assumes
Assumes familiarity with indexing documents and running match or term queries.
What you can do after
Operate it to identify stored queries matched by a newly supplied document and support availability notifications.
How it connects
Connects normal search queries with the percolator field type and match queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.