Elasticsearch: can I define synonyms with boost?

Say A , B , C are synonyms, I want to define B โ€œcloserโ€ to A than C

so when I search for keyword A , the first appears in the search results A , B comes second, and C last.

Any help?

+4
source share
1 answer

The search-time mechanism (as yet) does not exist to distinguish between coincidence of synonyms and source fields. This is due to the fact that when indexing, synonyms of the fields are placed in the inverted index along with the original term, leaving all words equal.

This does not mean, however, that you cannot do any magic during the index to get the information you need.

Create an index with two parsers: one with a synonym filter and one without it.

 PUT /synonym_test/ { settings : { analysis : { analyzer : { "no_synonyms" : { tokenizer : "lowercase" }, "synonyms" : { tokenizer : "lowercase", filter : ["synonym"] } }, filter : { synonym : { type : "synonym", format: "wordnet", synonyms_path: "prolog/wn_s.pl" } } } } } 

Use multi-field mapping so that the field of interest is indexed twice:

 PUT /synonym_test/mytype/_mapping { "properties":{ "mood": { "type": "multi_field", "fields" : { "syn" : {"type" : "string", "analyzer" : "synonyms"}, "no_syn" : {"type" : "string", "analyzer" : "no_synonyms"} } } } } 

Specify a test document:

 POST /synonym_test/mytype/1 { mood:"elated" } 

During the search, increase the number of hits on the field without synonyms.

 GET /synonym_test/mytype/_search { query: { bool: { should: [ { match: { "mood.syn" : { query: "gleeful", "boost": 3 } } }, { match: { "mood.no_syn" : "gleeful" } } ] } } } 

_Score results ": 0.2696457

Searching for the original term returns the best result:

 GET /synonym_test/mytype/_search { query: { bool: { should: [ { match: { "mood.syn" : { query: "elated", "boost": 3 } } }, { match: { "mood.no_syn" : "elated" } } ] } } } 

Results in: _score ": 0.6558018,

+6
source

Source: https://habr.com/ru/post/1488407/


All Articles