So I am trying to implement synonyms (at search time) in Elastic Search, so far i've defined synonyms like this in Kibana:
PUT /<my_index>/_settings
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym": {
"analyzer": "standard",
"search_analyzer": "synonym_analyzer",
"type": "synonym",
"synonyms": [
"foo => bar"
]
}
}
}
}
}
}
But then when I try to search for foo
I dont get back anything.
My search request looks like this:
GET <my_index>/_search
{
"size": "12",
"from": "0",
"min_score": "0.1",
"query": {
"function_score": {
"functions": [
{
"field_value_factor": {
"field": "product.sales_count",
"missing": 0,
"modifier": "log1p"
}
},
{
"field_value_factor": {
"field": "product.image_count",
"missing": 0,
"modifier": "log1p"
}
}
],
"query": {
"bool": {
"filter": [
{
"term": {
"product.is_visible": true
}
}
],
"should": [
{
"query_string": {
"default_field": "product.i18ns.ro_RO.*.analyzed",
"query": "foo",
"analyzer": "synonym_analyzer"
}
},
{
"query_string": {
"default_field": "categories.i18ns.ro_RO.*.analyzed",
"query": "foo",
"analyzer": "synonym_analyzer"
}
},
{
"query_string": {
"default_field": "brands.i18ns.ro_RO.*.analyzed",
"query": "foo",
"analyzer": "synonym_analyzer"
}
},
{
"query_string": {
"default_field": "product.images.i18ns.ro_RO.*.analyzed",
"query": "foo",
"analyzer": "synonym_analyzer"
}
},
{
"query_string": {
"default_field": "features.i18ns.ro_RO.*.analyzed",
"query": "foo",
"analyzer": "synonym_analyzer"
}
},
{
"query_string": {
"default_field": "product.ref",
"query": "foo",
"boost": 2
}
}
]
}
}
}
},
"aggs": {
"by_categories": {
"terms": {
"field": "categories.i18ns.ro_RO.title.raw",
"size": 100
}
}
}
}
NOTE: I do have bar
in my index.
There is some issue with the way you have defined the synonym analyzer. The analyzer
part must come under the analysis
part and not under the filter
part of the index setting. Refer to this official documentation, to know more about the settings of synonym token filter
Adding a working example with index data, mapping, search query, and search result.
Index Mapping:
{
"settings": {
"index": {
"analysis": {
"filter": {
"synonym_filter": {
"type": "synonym",
"synonyms": [
"foo => bar"
]
}
},
"analyzer": {
"synonym_analyzer": {
"filter": [
"synonym_filter"
],
"tokenizer": "standard"
}
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text"
}
}
}
}
Index Data:
{
"title": "bar"
}
Search Query:
{
"query": {
"match": {
"title": {
"query": "foo",
"analyzer": "synonym_analyzer"
}
}
}
}
Search Result:
"hits": [
{
"_index": "67566386",
"_type": "_doc",
"_id": "2",
"_score": 0.9808292,
"_source": {
"title": "bar"
}
}
]