Customize Elastic Search and Search by Synonyms in Liferay

Q-Have you ever wondered if you can customize your Elastic Search, so that on your searches in Liferay, not only the words you're searching for come up in the results but also synonyms of these words?

A-Yes, you can!

Below, I'll show you an example of how I did it (by customizing my indexes and mappings settings).

Let's say that I have a web content article containing the word "small" and I search for the word "tiny".

 
  1. Navigate to Control Panel → Configuration → System Settings → Foundation
     
  2. Search for the com.liferay.portal.search.elasticsearch.configuration.ElasticsearchConfiguration system setting.
     
  3. Go to "Aditional index configurations" and add your own.

    You can start by copying the contents of your index-settings.json file there. Your index-settings.json file is packed in the Elastic Search module of your Liferay bundle.

    Now, modify it as this document describes: https://www.elastic.co/guide/en/elasticsearch/guide/current/using-synonyms.html .

    It should look like the image below (note the "synonyms" part of the json, where we wrote our list of synonyms):
      Inline image 1
     
  4. Now go to "Override type mappings" and copy into the text area input the contents of the file "liferay-type-mappings.json", also packed in the Elastic Search module (a jar that you will find in your Liferay bundle) into it.

    Modify it, also following the steps this document describes: https://www.elastic.co/guide/en/elasticsearch/guide/current/using-synonyms.html (as in the previous step).

    For example, you could add something like this:
      "content": {
         "index": "analyzed",
         "store": "yes",
                   "search_analyzer" : "my_synonym_analyzer",
                   "analyzer" : "my_synonym_analyzer",
                   "term_vector": "with_positions_offsets",
                   "type": "string"
      },
    and something like this:
     "title": {
    "index": "analyzed",
    "store": "yes",
                   "search_analyzer" : "my_synonym_analyzer",
                   "analyzer" : "my_synonym_analyzer",
    "term_vector": "with_positions_offsets",
    "type": "string"
    },
    to it:
    Inline image 2
      
     
  5. Save your changes
     
  6. Navigate to Control Panel → Configuration → Server Administration and execute "Reindex all search indexes" under the section "Index Actions"
    Inline image 3

     
  7. Perform a search and... Voila!, the magic happens:
    Inline image 4

    Easy, right?

    If Elastic Search can do it, Liferay will do it too (since it leverages on Elastic Search for indexing its documents). You just need to know it can be done, and where in the control panel you can configure it.
     

Write a blogpost too!

Write a deep dive into how you use Liferay projects in your technology stack. Or let people know useful tips and tricks for a particular functionality. The Liferay community needs you!

Login or Create an account