【问题标题】:elasticsearch - fuzziness with bool_prefix typeelasticsearch - bool_prefix 类型的模糊性
【发布时间】:2021-01-20 20:31:28
【问题描述】:

我有以下疑问:

{
        size: 6,
        query: {
          multi_match: {
            query,
            type: 'bool_prefix',
            fields: ['recommendation', 'recommendation._2gram', 'recommendation._3gram'],
          },
        },
        highlight: {
          fields: {
            recommendation: {},
          },
        },
      }

我想将fuzziness: 1 添加到此查询中,但type: 'bool_prefix' 存在问题。我需要type: 'bool_prefix 保留在那里,因为它是查询工作方式不可或缺的一部分,但我还想给它添加一些模糊性。有什么想法吗?

【问题讨论】:

  • 什么样的问题? “模糊性对从最终术语构造的前缀查询没有影响”?
  • 不,当你添加它时,它不起作用 - 试试吧

标签: elasticsearch fuzzy-search


【解决方案1】:

正如bool_prefix的官方ES documentation所提到的

fuzziness、prefix_length、max_expansions、fuzzy_rewrite 和 下列术语支持模糊转置参数 用于构造术语查询,但对 从最后一个词构造的前缀查询。

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

索引映射:

{
  "mappings": {
    "properties": {
      "recommendation": {
        "type": "search_as_you_type",
        "max_shingle_size": 3
      }
    }
  }
}

索引数据:

{
    "recommendation":"good things"
}
{
    "recommendation":"good"
}

搜索查询:

可以用bool_prefix添加模糊参数,如下图

  {
  "size": 6,
  "query": {
    "multi_match": {
      "query": "goof q",
      "type": "bool_prefix",
      "fields": [
        "recommendation",
        "recommendation._2gram",
        "recommendation._3gram"
      ],
      "fuzziness": 1
    }
  },
  "highlight": {
    "fields": {
      "recommendation": {}
    }
  }
}

搜索结果:

 "hits": [
      {
        "_index": "65817192",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.1203322,
        "_source": {
          "recommendation": "good things"
        },
        "highlight": {
          "recommendation": [
            "<em>good</em> things"
          ]
        }
      },
      {
        "_index": "65817192",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.1583319,
        "_source": {
          "recommendation": "good"
        },
        "highlight": {
          "recommendation": [
            "<em>good</em>"
          ]
        }
      }
    ]

【讨论】:

  • @Brian Guan 你有没有机会去浏览答案,期待得到你的反馈:)
  • @Brian Guan 你能回复一下吗?
【解决方案2】:

我最终得到了与 bool 的 multi_match 相结合的附加模糊查询。在您的情况下,它看起来像这样:

{
  "size": 6,
  "query": {
    "bool": {
      "should": [
        {
          "multi_match": {
            "query": "goof q",
            "type": "bool_prefix",
            "fields": [
              "recommendation",
              "recommendation._2gram",
              "recommendation._3gram"
            ]
          }
        },
        {
          "fuzzy": {
            "nameSearch": {
              "value": "goof q",
              "fuzziness": "AUTO"
            }
          }
        }
      ]
    }
  },
  "highlight": {
    "fields": {
      "recommendation": {}
    }
  }
}

【讨论】:

    猜你喜欢
    • 2022-01-08
    • 2023-03-20
    • 2019-07-18
    • 2019-05-08
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    相关资源
    最近更新 更多