Elastic, developing search apps


In this post, im going to cover several features built into Elasticsearch that help to develop search applications. First is Search Terms. This is, I think, something that is fairly common and what a lot of people expect when they’re using a search application, is to have their actual search terms highlighted in the results. Next is sorting the search results. Users aren’t really going to mess too much with sorting, but it is still a useful feature that they expect to have to be able to sort the search results based on their specific criteria. Next, we have pagination. Oftentimes when you’re searching for something, even if you get hundreds or thousands of search results, you really only care about that first page. If it’s a good search application, then whatever you’re looking for should hopefully be within that first page, as those should be the most relevant results.
Pagination is a really great way to just eliminate the noise of having tons
of results and just showing you the most relevant ones first, but still giving you the option to page through the others. Aliases is something i have already introduced in in my previous posts, but i am going to more formerly focus on it iwith Defining Index Aliases, and to showcase a few ways of creating and even removing them. Then lastly, i am going to take everything we’ve learned in my previous posts and combine them in, probably, the most useful feature in Elasticsearch when developing a search application; search templates. Templating a query and parameterizing that query so that all you have to do when you go to use it within your search application is to just pass a few variables.

Highlighting Search Terms

Terms are common thing that we expect to see whenever we’re using a search application, but, essentially, we’re just going to emphasize
the search terms usimng highlight . Elasticsearch actually lets you customize the way it does this by specifying which fields, or the way in which you’re going to actually highlight the search terms. This is the basic concept. Lets use an example, first as already covered Term must be analyzed. For example product name… lets use sweatshirt. In the query we must define fields that we want to highlight using the specific terms we want to use (remember ….sweatshirt). Sweatshirt will be wrapped in an HTML emphasis tags,
which is emphasizing the text or the search term within the search results. In your search application, you can look for this highlight array and use that to visually highlight the search terms on the page. What if we want to change the way it’s being highlighted? So in this case we have EM tags, or emphasis tags. Maybe you want to change that to bold tags. Under highlight setting ,
we can actually specify a pre-tag and a post tag and can do the HTML bold tag , and post tags, straightforward.

Sorting search results

Sorting search results is just a way to meaningfully organize our search results by sorting them based on some criteria that we have control over.
There’s 3 main components to sorting in Elasticsearch. We must specify which fields and even a hierarchy of fields to sort on. Then we have the sort order for each type of sorts and for string field sorting, it’s going to be ascending by default, for numerical fields or the relevancy score, it’s going to be descending by default, but we ultimately have control over which one of these it is. And then lastly, for multi-value fields — arrays of objects, or just an array of numbers, or whatever it might be — we have the sort mode.
But what do we do with those values? How do we aggregate them?
We can do a min, max, sum, average or median mode on each of those multi-value fields. Back to the product name, sweatshirt, we’re highlighting it with some bold tags. We can search, or sort rather, by the customer name, in the string fields, the order is, by default, ascending. If you do a search,
you get a descending search, but if remove the order entirely, you will find that the default search — or sort, rather — is going to be ascending.
We can search by multiple things. For example a customer_first_name.keyword, would be the second thing we sort on, we can even sort on some numerical fields and multi-value numerical field and sort on a product’s price. A search may include multiple products in each order , so we can do products_price. Because we may have multi-values, we need to control how we’re going to aggregate this data.
Like sum all products price into one value and then use it as sort value.
For numerical fields, the order by default is descending. We start with the large values first, with string fields, it’s going to be ascending. I can change this from descending to ascending if I wanted to. You can change the order even for numerical fields.

Paginating search results

Paginating search result is actually really important because it splits our search results into these discrete pages. And more often than not,
all we really care about is that first page, and having too many search results displayed at once can really be quite overwhelming. It helps to show the first page of the most relevant search results and still give the option to page through more results if you need to. But 9 times out of 10, really,
you’re going to find what you’re looking for in that first page.

Index aliases

I have covered aliases already and i want to take a close look at them .
This is just a way to simplify how to reference our data in Elasticsearch by assigning an alias to one or more indices, to quickly reference what we care about. And believe it or not, a major usage of aliases is simple to reduce typing by aliase a short name to a long index name. To add or remove an alias, we can use a POST to the aliases’ API. This is one way to add an alias to an index using the aliases’ API. The other way is through component templates and index templates. To do that we need to do PUT API call for component template, using them I can create an alias that references multiple different indices that represent a category of something. If i will create an index template ill define an index patterns by composing an index template of this component template, i will be able to pick and use an alias. And i can also define aliases within the index template itself. Another thing we can do with aliases, we can actually filter our aliases down. We can have an alias that references an index, but with a query. It only references certain documents in that index.

Search templates

Search templates in Elasticsearch is probably the most important feature
when developing a search application, because you need some way to create
a reusable and parameterized query so that your search application
doesn’t actually have to craft these queries over and over again with different inputs. Instead, you can create a search template up front in Elasticsearch,
parameterize that template out so that your search application, all it has to do is just pass in the variables or parameters and let Elasticsearch create the query for you. To do this, you will need to do a PUT to the script’s API. This is where you can store different scripts in Elasticsearch to use them later.
A search template is really just a script. We need to define the language
we’re going to use for the script. Then, define source for the script. I don’t have to create a whole new query every single time. I can just pass a different from parameter in, and then just get the next set of results. It simplifies querying Elasticsearch from an application.


Let’s summarize he things in this post. First cover about highlight our search terms and how to even change not only what we highlight, but the way we highlight it, then search results, that we can do a hierarchy of search terms,
and we can change the order. We can even change the mode of aggregation
for multi-value fields. Paginated search results enabling to page through our search results Next, aliases API along with some component and index template settings to create and remove aliases for various indices.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Amit Cohen

Amit Cohen

Im a technical product professional and AWS SA with hands-on expertise crating products from incubation to multi million $$$. During my career as product manag