Elasticsearch 未在 query_string 查询中使用同义词分析器

Elasticsearch not using synonyms analyzer on query_string query

我正在尝试创建一个使用自定义同义词分析器的弹性搜索索引。但是,它并没有像我期望的那样工作,因此没有返回任何结果。

我创建的索引如下:

{
    "settings": {
        "analysis": {
            "filter": {
                "english_keywords": {
                    "type": "keyword_marker",
                    "keywords": [
                        "microsoft"
                    ] // stem_exclusion
                },
                "synonym": {
                    "type": "synonym",
                    "synonyms": [
                        "carcinoma => cancer"
                    ]
                }
            },
            "analyzer": {
                "custom_english_analyzer": {
                    "tokenizer": "standard",
                    "filter": [
                        "apostrophe",
                        "asciifolding",
                        "lowercase",
                        "synonym",
                        "stop",
                        "english_keywords",
                        "kstem"
                    ]
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "my_id": {
                "type": "keyword"
            },
            "titles": {
                "type": "keyword",
                "fields": {
                    "fulltext": {
                        "type": "text",
                        "analyzer": "custom_english_analyzer"
                    }
                }
            }
        }
    }
}

然后我索引一条记录:

{
  "my_id": "HF124",
  "titles": [
    "skin cancer"
  ]
}

If if 运行 使用 /test_index/_analyze

在分析仪上进行测试
{
  "analyzer" : "custom_english_analyzer",
  "text" : "carcinoma"
}

我得到以下信息:

{
    "tokens": [
        {
            "token": "cancer",
            "start_offset": 0,
            "end_offset": 9,
            "type": "SYNONYM",
            "position": 0
        }
    ]
}

但是,当我 运行 下面的查询时,我没有得到任何返回的记录。我希望 'carcinoma' 被替换为 'cancer' 并匹配。我错过了什么?

{
    "explain": true,
    "track_scores": true,
    "track_total_hits": true,
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "carcinoma*",
                        "fields": [
                            "titles.fulltext"
                        ]
                    }
                }
            ]
        }
    }
}

您需要从查询部分中删除通配符 (*)。将 carcinoma* 替换为 carcinoma.

将您的搜索查询修改为

{
  "explain": true,
  "track_scores": true,
  "track_total_hits": true,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "carcinoma",       // note this
            "fields": [
              "titles.fulltext"
            ]
          }
        }
      ]
    }
  }
}