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,