为 elasticsearch 查询添加模糊性

add fuzziness to elasticsearch query

我有一个 autocomplete/suggestions 索引的查询,如下所示:

{
    "size": 10,
    "query": {
        "multi_match": {
            "query": "'"+search_text+"'",
            "type": "bool_prefix",
            "fields": [
                "company_name",
                "company_name._2gram",
                "company_name._3gram"  
            ]
        }       
      }
  }

这个查询完全符合我的要求。但是我想添加 fuzziness:"AUTO" 到这个查询。我阅读了文档并尝试像这样添加它:

{
    "size": 10,
    "query": {
        "multi_match": {
            "query": {
                "fuzzy": {
                    "value": "'"+search_text+"'",
                    "fuzziness": "AUTO"
                }
            },
            "type": "bool_prefix",
            "fields": [
                "company_name",
                "company_name._2gram",
                "company_name._3gram"
            ]
        }
    }
}

但是我得到了这个错误

```
"type": "parsing_exception",
"reason": "[multi_match] unknown token [START_OBJECT] after [query]",```

这导致我的查询不起作用。

无需添加 fuzzy query. To add fuzziness to a multi-match query 您需要添加模糊性 属性,如此处所述:

因为您在每个字段上使用 bool_prefix as the type of multi-match query, so it creates a match_bool_prefix 来分析其输入并根据术语构建 bool 查询。除了最后一个术语外,每个术语都用于术语查询。最后一个术语用于前缀查询。

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

索引映射:

{
  "mappings": {
    "properties": {
      "company_name": {
        "type": "search_as_you_type",
        "max_shingle_size": 3
      },
      "serviceTitle": {
        "type": "search_as_you_type",
        "max_shingle_size": 3
      },
      "services": {
        "type": "search_as_you_type",
        "max_shingle_size": 3
      }
    }
  }
}

索引数据:

{
    "company_name":"sequencing how shingles are actually used"
}

搜索查询:

   {
    "size": 10,
    "query": {
        "multi_match": {
            "query": "sequensing how shingles",
            "type": "bool_prefix",
            "fields": [
                "company_name",
                "company_name._2gram",
                "company_name._3gram"  
            ],
            "fuzziness":"auto"
        }       
      }
  }

搜索结果:

"hits": [
      {
        "_index": "65153201",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.5465959,
        "_source": {
          "company_name": "sequencing how shingles are actually used"
        }
      }
    ]

如果要查询sequensing,得到上面的文档,那么需要将multi-match的类型由bool_prefix改为type ] 根据您的用例。