使用同义词分析器时来自Elastic搜索的奇怪结果

时间:2020-09-24 23:57:59

标签: elasticsearch

我运行此查询时

"multi_match": {
    "query": "paper copier ", 
    "fields": [ "allStringFields" ],
    "type": "cross_fields",
    "operator": "and",
    "analyzer": "synonym"   
}

我得到1342个结果

但是当我运行此查询时(注意单词顺序)

 "multi_match": {
    "query": " copier paper ", 
    "fields": [ "allStringFields" ],
    "type": "cross_fields",
    "operator": "and",
    "analyzer": "synonym"   
 }

我得到零结果

我正在使用同义词分析器,这是导致此行为的原因

对此有解决方案吗?

1 个答案:

答案 0 :(得分:0)

添加带有索引数据,映射,搜索查询和搜索结果的工作示例。在下面的示例中,我采用了两个同义词tabletables

I get zero results

请再次进行索引映射。根据以下示例,搜索关键字为table chair,将在字段titlecontent中进行搜索。以下查询将返回同时包含table AND chair的文档。要获取详细说明,请参阅Multi match querysynonym token filter上的ES文档。

索引映射:

{
  "settings": {
    "index": {
      "analysis": {
        "filter": {
          "synonym_filter": {
            "type": "synonym",
            "synonyms": [
              "table, tables"
            ]
          }
        },
        "analyzer": {
          "synonym_analyzer": {
            "filter": [
              "lowercase",
              "synonym_filter"
            ],
            "tokenizer": "standard"
          }
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      }
    }
  }
}

索引数据:

{ "title": "table chair" }
{ "title": "tables chair" }
{ "title": "table fan" }
{ "title": "light fan", "content": "chair" }

搜索查询:

{
    "query": {
        "multi_match": {
            "query": "table chair",
            "operator": "and",
            "type":"cross_fields",
            "fields": [
                "title","content"
            ],
            "analyzer": "synonym_analyzer"
        }
    }
}

搜索结果:

"hits": [
      {
        "_index": "synonym",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.7227666,
        "_source": {
          "title": "table chair"
        }
      },
      {
        "_index": "synonym",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.3862942,
        "_source": {
          "title": "tables chair"
        }
      }
    ]

搜索table chairchair table时,将得到与上述相同的搜索结果。

相关问题