'I want to implement a synonyms filter which give me a results like :

for Example If I type "US" in search box then it must return synonyms like ["USA", "United States of America", "United Stated"] only. I do not want "United" or "States" seperately in my search results

My Synonyms.txt File US => US, USA, United States, United, States

enter image description here



Solution 1:[1]

I would suggest to use the SynonymFilterFactory only at index time, not query time. Try the below field type for your field.

<fieldType name="text" class="solr.TextField">
<analyzer type="index">
  <tokenizer class="solr.StandardTokenizerFactory"/>
  <filter class="solr.SynonymFilterFactory" synonyms="mysynonyms.txt"/>
  <filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
  <tokenizer class="solr.KeywordTokenizerFactory"/>
  <filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>

Note : If there is already a fieldType defined with name "text", please change the name of the above field type.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abhijit Bashetti