Synonyms
Step-by-Step guide to implement Synonyms in the search function
Incorporating synonyms into the search function makes it more robust, user-friendly and effective at delivering relevant results, ultimately enhancing the overall user experience.
This step-by-step guide will demonstrate how to incorporate synonyms into the search function, using the products repositories as an example.
First, we need to create a field type, which uses the SynonymAnalyzer:
- Go to the index where you want to implement the synonyms feature, in this example it is Settings/Repositories/SwiftProducts/Products
- Click the context menu in the Index fields section and select "Manage field types"
- Click the "+ New field type" button
- Provide the field type with a name, e.g. WithSynonyms
- Set the type to System.String
- Select the SynonymAnalyzer
- Save and close
Now, we need to add a field of this type to the index containing the data we want to match to the synonyms - In this example we will use the long description, but it can be applied to other types as well.
- Click the context menu in the Index fields section and select "Manage fields"
- Click "+ New index field" in the upper right corner and select "Field"
- Provide the field with a name, e.g. Long description with synonyms
- In the Field section, select the type you created before, in this case WithSynonyms
- Remember to check Indexed and Analyzed
- Set source to ProductLongDescription
- Save and close
The next step is to go to Assets/System/Repositories/{NameOfYourRepository} and create two files:
- A config file which handles what analyzers to use
- An xml file containing a list of synonyms
NB: Both files must be named bases on the system name of the field type you created previously – in this case WithSynonyms.config and WithSynonyms_synonyms.xml
Examples of both files are shows below:
WithSynonym.config:
<?xml version="1.0" encoding="utf-8" ?>
<SynonymAnalyzer>
<TokenizerType>
LowerCase
</TokenizerType>
<StemmerName>
English
</StemmerName>
<StopWords>
</StopWords>
</SynonymAnalyzer>
WithSynonyms_synonyms.xml:
<synonyms>
<group stem="inch">
<synonym>inch</synonym>
<synonym>in.</synonym>
<synonym>inches</synonym>
</group>
<group stem="fast">
<synonym>fast</synonym>
<synonym>quick</synonym>
<synonym>rapid</synonym>
<synonym>rapido</synonym>
</group>
<group stem="slow">
<synonym>slow</synonym>
<synonym>decrease</synonym>
</group>
<group stem="check">
<synonym>check</synonym>
<synonym>lookup</synonym>
<synonym>look</synonym>
</group>
<group stem="apk">
<synonym>armored personnel carrier</synonym>
</group>
</synonyms>
As given in the config file the TokenizerType is set to LowerCase. This means that you should ensure to keep the content of the XML file lower case.
Currently we do not supply any synonym lists out of the box, but you should be able to scrape data from an online source.
We now need to incorporate two query expressions based on the previously created field (Long description with synonyms) into the existing search section:
- Go to Settings/Repositories/SwiftProducts
- Click the Products.query in the Queries section
- Scroll down to the Expressions section and click "Manage"
- Find the group with the search expressions
- Click the context menu and select "+ New expresssion"
- Set field to "Long description with synonyms"
- Select the "Equal" operator
- Select the "Parameter" type
- Set the Parameter to eq
- Save
For the other expression, follow the steps from 1-8, but select the q as the Parameter in step 9. The group should now look like this:
Make sure you rebuild the index, and that you have products using the synonyms in your product catalog – searching for any of terms in the synonym file will now also return products using any of the other synonyms.