The prefix query/filter is used only when the starting part of a term is known. It allows completing truncated or partial terms.
You need a working ElasticSearch cluster and an index populated with the script available in online code.
For executing a prefix
query/filter, we need to perform the following steps:
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" } } }'
curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search' -d '{ "query": { "filtered": { "filter": { "prefix": { "uuid": "333" } }, "query": { "match_all": {} } } } }'
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:
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 |
---|---|
|
Fruit |
|
Apple |
|
Green Apple |
|
Red Apple |
|
Melon |
|
White Melon |
|
Vegetables |
In the previous table we have structured ID to contain information about the tree structure, which allows us to create the following queries:
"prefix": {"fruit_id": "001" }
"prefix": {"fruit_id": "001002" }
"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.
18.117.236.133