synonyms Search functionality in OpenSearch using S3 service on AWS

Salonisuman
3 min readApr 3, 2024

--

Today if you will search synonyms search in OpenSearch on google you will get plenty of results how you can implement as per your use case. But Finding the right one could be very time consuming. today, I’ll be sharing one of the efficient way which I used for my organization and could be helpful for you as well.

USE CASE:

1. Domain specific Synonyms

2. Easy to update <synonyms>

3. easy code implementation

Today we will see how we can solve our problem using Custom packages for Amazon OpenSearch Service

  1. Create a Synonyms file in CSV/ text format:

create an excel sheet write all the words and there relative synonyms in the file formatted in csv or text format. You can use the following synonyms file for testing purposes. Save it as synonyms.txt.

danish, croissant, pastry
ice cream, gelato, frozen custard
sneaker, tennis shoe, running shoe
basketball shoe, hightop

2. Uploading packages to Amazon S3:

upload your created file on Amazon S3 bucket, After you upload the file, make note of its S3 path. The path format is s3://bucket-name/file-path/file-name.

3. Importing and associating packages:

  1. In the Amazon OpenSearch Service console, choose Packages.
  2. Choose Import package.
  3. Give the custom dictionary a descriptive name.
  4. Provide the S3 path to the file, and then choose Submit.
  5. Return to the Packages screen.
  6. When the package status is Available, select it. Optional plugins will automatically be Available.
  7. Choose Associate to a domain.
  8. Select a domain, and then choose Associate.

Note: These created packages can easily be updated and you can only update package to domain only when required.

once package is created , Back to navigation panel to see all the packages and in the listening corresponding to package details you will get analyzers/id.

4. Using package with OpenSearch:

Go to OpenSearch Dashboard, Dev Tool And add as mentioned

PUT my-index                        // index-name
{
"settings": { // index-settings modification
"index": {
"analysis": {
"analyzer": {
"my_analyzer": { // custom analyzer name
"type": "custom",
"tokenizer": "standard",
"filter": ["my_filter"] // filter-name
}
},
"filter": {
"my_filter": { // filter-name
"type": "synonym",
"synonyms_path": "analyzers/F111111111", // analyzer-id
"updateable": true // to accept update in our s3 file and package
}
}
}
}
},
"mappings": { // mapping-update
"properties": {
"description": {
"type": "text",
"analyzer": "standard",
"search_analyzer": "my_analyzer" // analyzer name
}
}
}
}

Note:

  1. Do not forget to close index before adding these changes, and open all changes are added open index for use again.
  2. Incase you are facing error while adding changes, add setting and mapping separately, It should work
POST /<index-name>/_close
POST /<index-name>/_open

5. Test the configured Analyzer:

GET <index-name>/_analyze
{
"analyzer" : "my_analyzer",
"text" : "gelato"
}

If you are getting the words ice cream, gelato, frozen custard, Hurray you have successfully implemented Synonyms search instead of just exact match search from OpenSearch.

If you have any doubt feel free to reach out will be happy to help you out.

Happy Coding..

--

--

Salonisuman
Salonisuman

Written by Salonisuman

Software Engineer | Talks about Technology

Responses (1)