How to programatically make changes in the index and query for new synonms in synonm.txt

StackOverflow https://stackoverflow.com/questions/11578042

  •  22-06-2021
  •  | 
  •  

Question

Please can any one suggest me how we can programtically make chages in the index and query part for new synonms enter.

Was it helpful?

Solution

I did something very similar. I had a list of synonyms in a table and I had to make solr take care of these synonyms both while indexing and in the query. I wrote a python script which wrote the synonyms to a file - which is the synonyms.txt in your solr conf directory. Just make sure that the filename is linked in schema.xml as explained here - http://wiki.apache.org/solr/AnalyzersTokenizersTokenFilters#solr.SynonymFilterFactory

Also, make sure that the text file has no "," or spaces following and has no blank lines at the end.

Here is the snippet of my python script. Hope it helped

for key in synonym_dict:
    values = synonym_dict[key]
    for val in values:
        f.write(val + ',')
    f.seek(-2, 1)
    f.write('\n')
f.truncate()
f.close()

You need the seek and truncate to avoid preceeding ";" and empty lines.

Hope it helped

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top