vuestorefront / magento2-vsbridge-indexer

This is official Vue Storefront, native, Magento2 indexer
https://vuestorefront.io
MIT License
69 stars 89 forks source link

Synonyms support for products #270

Open Fifciu opened 4 years ago

Fifciu commented 4 years ago

Hello. I've just discovered we could pretty easily get support for synonyms with small modification of indexer. As I am not really Magento's developer I decided to prepare all knowledge needed to prepare this feature here.

To add synonyms support for ES Index we have to send PUT /indexName request to Elasticsearch. Payload should looke like: a) For Elasticsearch 5.x:

{
    "settings": {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "synonym" : {
                        "tokenizer" : "whitespace",
                        "filter" : ["synonym"] 
                    }
                },
                 "filter" : {
                    "synonym" : {
                        "type" : "synonym", 
                        "synonyms_path" : "analysis/synonym.txt",
                        "tokenizer" : "whitespace" 
                    }
                }
            }
        }
    }
}

b) For Elasticsearch 7.x:

{
    "settings": {
        "index" : {
            "analysis" : {
                "analyzer" : {
                    "synonym" : {
                        "tokenizer" : "whitespace",
                        "filter" : ["synonym"]
                    }
                },
                "filter" : {
                    "synonym" : {
                        "type" : "synonym",
                        "synonyms_path" : "analysis/synonym.txt"
                    }
                }
            }
        }
    }
}

As you might see payload is really similar expect synonym block inside filter block. Above, we have example with external file with synonyms in Solr format. Obviously, we should be able to inline synonyms. For that, we just modify synonym block (it is universal version for 5.x & 7.x):

"filter" : {
  "synonym" : {
    "type" : "synonym",
    "synonyms" : [
      "i-pod, i pod => ipod",
      "universe, cosmos"
    ]
  }
}

We should sign it is better to use the external file.
However, it is recommended to define large synonyms set in a file using synonyms_path, because specifying them inline increases cluster size unnecessarily. - from ES docs.

synonyms_path has a base in /etc/elasticsearch.

Todo list

Based on Elasticsearch documentation, after we PUT these analyzer's settings - synonyms should work - no need for changes in PWA.

Synonyms' docs: Elasticsearch 5.x: https://www.elastic.co/guide/en/elasticsearch/reference/5.5/analysis-synonym-tokenfilter.html Elasticsearch 7.x: https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-synonym-tokenfilter.html