Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Elasticsearch 8.x Cookbook

You're reading from   Elasticsearch 8.x Cookbook Over 180 recipes to perform fast, scalable, and reliable searches for your enterprise

Arrow left icon
Product type Paperback
Published in May 2022
Publisher Packt
ISBN-13 9781801079815
Length 750 pages
Edition 5th Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Alberto Paro Alberto Paro
Author Profile Icon Alberto Paro
Alberto Paro
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Getting Started 2. Chapter 2: Managing Mappings FREE CHAPTER 3. Chapter 3: Basic Operations 4. Chapter 4: Exploring Search Capabilities 5. Chapter 5: Text and Numeric Queries 6. Chapter 6: Relationships and Geo Queries 7. Chapter 7: Aggregations 8. Chapter 8: Scripting in Elasticsearch 9. Chapter 9: Managing Clusters 10. Chapter 10: Backups and Restoring Data 11. Chapter 11: User Interfaces 12. Chapter 12: Using the Ingest Module 13. Chapter 13: Java Integration 14. Chapter 14: Scala Integration 15. Chapter 15: Python Integration 16. Chapter 16: Plugin Development 17. Chapter 17: Big Data Integration 18. Chapter 18: X-Pack 19. Other Books You May Enjoy

Mapping a Percolator field

The Percolator is a special type of field that makes it possible to store an Elasticsearch query inside the field and use it in a percolator query.

The Percolator can be used to detect all the queries that match a document.

Getting ready

You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1Getting Started.

To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.

How to do it...

To map a percolator field, follow these steps:

  1. We want to create a Percolator that matches some text in a body field. We can define the mapping like so:
    PUT test-percolator
    { "mappings": {
        "properties": {
          "query": { "type": "percolator"  },
          "body": { "type": "text" }
    } } }
  2. Now, we can store a document with a percolator query inside it, as follows:
    PUT test-percolator/_doc/1?refresh
    { "query": { "match": { "body": "quick brown fox"  }}}
  3. Now, let's execute a search on it, as shown in the following code:
    GET test-percolator/_search
    { "query": {
        "percolate": {
          "field": "query",
          "document": { "body": "fox jumps over the lazy dog" } } } }
  4. This will result in us retrieving the hits of the stored document, as follows:
    {
       ... truncated...
       "hits" : [
         {
         "_index" : "test-percolator", "_id" : "1",
         "_score" : 0.13076457,
         "_source" : {
             "query" : {
                 "match" : { "body" : "quick brown fox" }
             }
         },
         "fields" : { "_percolator_document_slot" : [0]       } } ] } }

How it works…

The percolator field stores an Elasticsearch query inside it.

Because all the Percolators are cached and are always active for performances, all the fields that are required in the query must be defined in the mapping of the document.

Since all the queries in all the Percolator documents will be executed against every document, for the best performance, the query inside the Percolator must be optimized so that they're executed quickly inside the percolator query. 

You have been reading a chapter from
Elasticsearch 8.x Cookbook - Fifth Edition
Published in: May 2022
Publisher: Packt
ISBN-13: 9781801079815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime