🏡 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.

Use Case

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.

Approach

which'd yield:

"aggregations" : {
  "by_unique_titles" : {
    "buckets" : [
      { "key" : "Complete Blood Count" },
      { "key" : "Preventing Blood Clots" }
    ]
  }
}

now yielding all 3 titles: