How to Configure Synonyms in Solr

To improve search results, one of the easiest way is to add synonyms. For example, a product may have an official name as well as commonly used name. When it is indexed against only the official name because the product name field doesn’t have a commonly used name, typing the common name does not bring the result. This is frustrating to the user. In this scenario, we can configure the synonym that maps the common name to the official name.

Another use case would be catching misspelling. If a particular word associated to a product is often misspelt by users, we can map the misspelt word to the correct spelling. In this way, we can ensure the user get the search result even if they misspelt the word. This simple trick can enhance the search results and prevent it from returning no result.

The easiest way to make synonyms work in Solr is to do it at index time by adding a synonym field and a synonym file. As far as I know, this works for both Solr 5 and 6. Let’s get started.

Steps

(1) Go to the conf folder in the index folder.

cd [path to the solr folder]\solr-x.x.x\server\solr\catalog_search_index\conf

(2) Add a synonym file in the folder

We need to add a text file containing synonym in the folder. By default, you will find the file called synonym.txt which contains the example of how to configure synonyms. We can use either => to map synonyms or just have comma separated words. When you use comma separated words, order does not seem to matter. You can just put a bunch of words and they all get indexed as synonyms at index time.

You can map synonyms with an arrow.

prednisolone => steroid

Or comma separated if you have a lot of them.

prednisolone, steroid, asthma, inflammation, rheumatoid arthritis, hello world

(3) Add field type in schema.xml

Find schema.xml and add fieldtype that indicates the synonym for the field. You need to use the synonym filter and indicate the text file which contains synonym.

(4) Add field mapping to the field type

Once the filed type is added, we can add field that maps to the field type by using the name. Name can be whatever you decided to call.

(5) Reload the schema

Update on the schema.xml file can be reflected by reloading the schema by the url below. You can make a curl call or simply use a web browser. Updating the schema does not need Solr restart. Core is the name of index.

curl http://solr-base-url:port/solr/admin/cores?action=RELOAD&core=product-index

(6) Run index

In this method, synonyms are defined at index time. Once reload the schema, make sure to re-index.

That’s it.

If you are still not sure how this works, I have a working config file you can check here. The example contains three fields.

Front-End
TypeScript: type aliases to check type equality

This post is to analyse how the type equality check by using type aliases proposed by Matt Pocock in his twitter post. These type aliases allow us to elegantly express type equality checks in unit tests. All we need to do is to pass the output and expected types in …

Front-End
Fixing it.only type error in Jest

If you are getting a type error with it.only in Jest, it could be due to incorrect TypeScript typings or incompatible versions of Jest and TypeScript. To resolve this issue, you can try the following steps: Make sure you have the latest versions of Jest and its TypeScript typings installed …

Front-End
yup conditional validation example

Here’s an example of a Yup validation logic where the first input field is optional but, if filled, it must contain only alphabetic characters, and the second input field is required: import * as Yup from “yup”; const validationSchema = Yup.object().shape({ firstField: Yup.string().matches(/^[A-Za-z]*$/, { message: “First field must contain only …