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.
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.
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:
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.
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!
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.