Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Planning and Creating Azure Search Indexes

DZone's Guide to

Planning and Creating Azure Search Indexes

Learn how to create a searchable product index for an online beer store. Mmm... beer...

· Big Data Zone
Free Resource

See how the beta release of Kubernetes on DC/OS 1.10 delivers the most robust platform for building & operating data-intensive, containerized apps. Register now for tech preview.

 Planning for search is a mandatory first step when starting to build a search index. If we build search indices as we go, we easily end up with messed up and hard to use indices. Even worse, we may end up with very complex search queries that need more resources than we expect. This post goes through the simple analysis of an example beer store search index, and shows how to create an Azure Search index for products.

I take one not-so-simple case for indexing because indexing your cat’s homepage or guestbook is not a challenge you can’t take on your own. We'll take a bunch of tables used by my example beer store, and based on those tables we will build the products search index.

Beer store product related classes

I left out the BaseEntity class that gives all classes an ID field and offers some common internal functionalities. The class diagram given above is not something big, but it’s still something we don’t map to search index 1:1 and like always — the devil is in the details. This is something we will explore later.

Analyzing Product Indexes

Before creating index on Azure Search we have to do some analysis to find out what we want to index and how we plan to use those indexes. In short, this means that there will be two types of data in our search index:

  • Data to search
  • Data to display search results

NB! We may need some additional data in search index that is not something we perform search on but we need this data to display search results without querying our product database for missing information.

At the same time, we have to keep our search index as small and optimal as possible. We are on the cloud and therefore we have to consider the economic aspects of our solutions.

Suppose we want to display search results similar to the image below. Suppose we must also be ready to show results with a product description and category in the near future. It must be easy to find products by the most important text fields, and users must be able to filter beers by alcohol content and price.

Search for "Rodenbach"

We start with finding data that we want to use in free-text search and filters. Taking a look at the class diagram, above we can come up with a table like this:

Index field Model Search Filter Output
Id Id + + +
CategoryId Category.Id +
CategoryName Category.Name +
Descrtiption Description +
ManufacturerName Manufacturer.Name + +
Country Manufacturer.Country +
Name Name + +
ShortDescription ShortDescription +
UnitPrice UnitPrice + +
AlcVol Fields[“AlcVol”] + +
Hops[] Fields[“Hops”] +
Malts[] Fields[“Malts”] +

Now let’s dig into this table. We have the following columns:

  • Index field – Field name in search index and possibly in search index DTO.
  • Model – How we get field from domain class.
  • Search – Is given field used for full-text search.
  • Filter – Is given field used by visible filter block or some hidden filters.
  • Output – Do we want this field to be returned as response from search service.

As you can see then our index doesn’t contain all information we have in database. We just took enough information to get useful search and to support filtering and building of results list.

Creating Product Index

Now it’s time to log in to Microsoft Azure and create new search service. As we are experimenting you can create free instance of service. I don’t stop on details about how to do it. Instead let’s jump to creating a new index.

Product index fields

Before you save anything make sure you inserted everything with no typos. After saving there will be no way to change existing fields anymore. To do so you have to drop the whole index and create it again!

Wrapping Up

We took the product catalogue from a beer store and analyzed how to create a search index for it. We added also some fields to index that we actually don’t use in search, but we need these fields to display search outputs without asking for product data from the database. We ended up with a pretty simple index that should fill our beer store customers' needs.

New Mesosphere DC/OS 1.10: Production-proven reliability, security & scalability for fast-data, modern apps. Register now for a live demo.

Topics:
azure

Published at DZone with permission of Gunnar Peipman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}