elasticsearchfull-text-searchliferayanalyzerelasticsearch-analyzers

How to create a custom Elasticsearch analyzer in liferay


In Elasticsearch I wanted to index some fields with my custom analyzer. So first, I added to additional configurations my analyzer

Liferay -> control panel -> System Settings -> " Serach Elasticsearch and select '-Elasticsearch 7-' -->"

Additional Index Configurations

{
  "analysis": {
        "analyzer": {
            "mmc_custom": {
                "tokenizer": "standard",
                "filter": [
                    "standard",
                    "my_ascii_folding",
                    "lowercase"
                ]
            }
        },
        "filter": {
            "my_ascii_folding": {
                "type": "asciifolding",
                "preserve_original": true
            }
        }
    }
}

Override Type Mappings

"title": {
    "mapping": {
        "analyzer": "mmc_custom",
        "index": "analyzed",
        "store": "true",
        "term_vector": "with_positions_offsets",
        "type": "string"
    }
}

After adding this property in Liferay Elasticsearch, I reset the index, restarted Liferay. Portal created a new index with my mapping and my analyzer correctly. Then I reindexed my docs. And when I search sth in Elasticsearch it shows expected results, I see that it analyzed my fields as I wanted. But when I search through the Liferay portal I see no change, my field is not analyzed. What I am doing wrong? I have the new index and data comes from it so why Liferay does not see it?

I referred to this site.


Solution

  • If you want to use a custom analyzer in Liferay, you need to perform two steps.

    1. Write your analyzer
    2. Define the mapping to override Liferay mapping

    Let me explain

    1. Write your analyzer Write below analyzer in the additional index configuration

       {
      "analysis":{
         "filter":{
            "my_synonym_filter":{
               "type":"synonym",
               "synonyms":[
                  "british,english",
                  "queen,monarch"
               ]
            }
         },
         "analyzer":{
            "custom_analyzer":{
               "tokenizer":"standard",
               "filter":[
                  "lowercase",
                  "my_synonym_filter",
                  "asciifolding",
                  "stemmer"
               ]
            }
         }
      }
       }
      
    2. Now your analyzer is ready, now you can override the Liferay mapping

    To override the mapping you can copy the Liferay mapping file (control-panel -> search -> field mapping)

    do change as per your requirement (change liferay-{company name} to LiferayDocumentType)

    So now the new file will look like below

    {
        "LiferayDocumentType" : {
           "dynamic_templates" : [
                  //Your mappings
           ]
        }
    }
    

    Add this override file in the field (Override type mapping)

    Then reindex the data and then you can use search to test your analyzer