Using a prefix query/filter

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

Getting ready

You need a working ElasticSearch cluster and an index populated with the script available in online code.

How to do it...

For executing a prefix query/filter, we need to perform the following steps:

  1. We execute a prefix query, from command line as follows:
    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search' -d '{
        "query": {
            "prefix": {
                "uuid": "333"
            }
        }
    }'
  2. The result, returned by ElasticSearch, is the same as the previous recipe.
  3. If you want use the terms query in a filter, the query should be as follows:
    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search' -d '{
        "query": {
            "filtered": {
                "filter": {
                    "prefix": {
                        "uuid": "333"
                    }
                },
                "query": {
                    "match_all": {}
                }
            }
        }
    }'

How it works…

When a prefix query/filter is executed, Lucene has a special method to skip to terms that start with a common prefix: so the execution of a prefix query is very fast.

The prefix query/filter is used, in general, in scenarios where term completion is required. Some of them are as follows:

  • Name completion
  • Code completion
  • On type completion

When a tree structure is designed in ElasticSearch, if the ID of the item is designed to contain the hierarchical relation, it can greatly speed up the application filtering. Some of the examples are as follows:

Id

Element

001

Fruit

00102

Apple

0010201

Green Apple

0010202

Red Apple

00103

Melon

0010301

White Melon

002

Vegetables

In the previous table we have structured ID to contain information about the tree structure, which allows us to create the following queries:

  • Filtering by all the fruits is done using the following code:
    "prefix": {"fruit_id": "001" }
  • Filtering by all apple types is done by using the following code:
    "prefix": {"fruit_id": "001002" }
  • Filtering by all the vegetables is done using the following code:
    "prefix": {"fruit_id": "002" }

If it's compared to a standard SQL parent_id table on a very large dataset, the reduction in join and the fast search performance of Lucene can filter the results in a few milliseconds as compared to some seconds/minutes.

Tip

Structuring the data in the correct way can give impressive performance boost!

See also

  • Refer to the Querying/filtering for terms recipe in this chapter
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.236.133