🏡 Home 📖 Chapter Home 👈 Prev 👉 Next
⚡ ElasticsearchBook is crafted by Jozef Sorocin (🟢 Book a consulting hour) and powered by:
As hinted in the previous chapter, there's a new kid on the block to support search-as-you-type use cases — a field data type aptly named search_as_you_type
.
Let's explore it in the common context of multi-word autocomplete queries where the order of the terms does not matter.
I'm building an autocomplete for my catalogue of medical articles. Each article has a title
:
{ "id": 1, "title": "Complete Blood Count" }
{ "id": 2, "title": "Preventing Blood Clots" }
{ "id": 3, "title": "What Is the CEA Test?" }
{ "id": 4, "title": "Coughing up Blood" }
...
When I search for the query blood c
, I want the docs #1, #2, and #4 to be suggested. #4 too despite the fact that the word blood
is not directly followed by the letter c
.
On top of that, since some articles have duplicate titles, I want to only show unique titles in my autocomplete.
Let's set up an index where the title
is a multi-field covering search_as_you_type
to query on and keyword
to aggregate on:
Let's then sync some medical articles' titles:
Now, assuming that people search for things in-order, we could start with a phrase_prefix
query followed by a terms
aggregation ordered by the max score of each individual term:
which'd yield:
"aggregations" : {
"by_unique_titles" : {
"buckets" : [
{ "key" : "Complete Blood Count" },
{ "key" : "Preventing Blood Clots" }
]
}
}
Coughing up Blood
... The most efficient way to catch it would be to use a multi_match
query of type bool_prefix
that targets the title.as_you_type
field and its shingle subfields. This can match the query terms in any order, but will score documents higher if they contain the terms in order in a shingle subfield:now yielding all 3 titles: