Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using a prefix query/filter


The prefix query/filter is used when only the starting part of a term is known. It allows you to complete truncated or partial terms.

Getting ready

You need a working ElasticSearch cluster and an index populated with the script chapter_05/populate_query.sh, available in the code bundle for this book.

How to do it...

In order to execute a prefix query/filter, perform the following steps:

  1. Execute a prefix query from the command line:

    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
      "query": {
        "prefix": {
          "uuid": "333"
        }
      }
    }'
    
  2. The result returned by ElasticSearch is the same as in the previous recipe.

  3. If you want to use the prefix query in a filter, this is how the query should look:

    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
      "query": {
        "filtered": {
          "filter": {
            "prefix": {
              "uuid": "333"
            }
          },
          "query": {
            "match_all": {}
      ...