当我搜索同义词时,Elastic Search 搜索查询未返回

Elastic Search search query not returning when I search for synonyms

所以我正在尝试在 Elastic Search 中实现同义词(在搜索时),到目前为止我已经在 Kibana 中定义了这样的同义词:


PUT /<my_index>/_settings
{
 "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym": {
            "analyzer": "standard",
            "search_analyzer": "synonym_analyzer",
            "type": "synonym",
            "synonyms": [
              "foo => bar"
            ]
          }
        }
      }
    }
  }
}

但是当我尝试搜索 foo 时,我什么也没有得到。

我的搜索请求如下所示:


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
      }
    }
  }
}

注意:我的索引中确实有 bar

您定义同义词分析器的方式存在一些问题。 analyzer 部分必须位于索引设置的 analysis 部分之下,而不是 filter 部分之下。参考这个official documentation,了解更多关于同义词标记过滤器的设置

添加包含索引数据、映射、搜索查询和搜索结果的工作示例。

索引映射:

{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym_filter": {
            "type": "synonym",
            "synonyms": [
              "foo => bar"
            ]
          }
        },
        "analyzer": {
          "synonym_analyzer": {
            "filter": [
              "synonym_filter"
            ],
            "tokenizer": "standard"
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      }
    }
  }
}

索引数据:

{
  "title": "bar"
}

搜索查询:

{
  "query": {
    "match": {
      "title": {
        "query": "foo",
        "analyzer": "synonym_analyzer"
      }
    }
  }
}

搜索结果:

"hits": [
      {
        "_index": "67566386",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.9808292,
        "_source": {
          "title": "bar"
        }
      }
    ]