如何在elasticsearch中添加模糊搜索查询?

时间:2016-03-24 02:00:15

标签: elasticsearch

我正在尝试在跨字段查询中的特定字段上实现模糊性。但这有点困难。

所以查询应该:

  1. 跨字段匹配短语。
  2. 匹配partNumber和条形码的完全匹配(无模糊)
  3. 将模糊术语与标题和副标题匹配。
  4. 我到目前为止的查询是在下面 - 请注意到目前为止,查询中的模糊性根本不起作用。

    所以这应该匹配1个结果,即标题中的“Amazing T-Shirt”和副标题中的Blue。 (注意拼写错误)。

    是否可以在索引映射级别实现模糊性?标题和副标题在数据集中非常短 - 可能最多30到40个字符。

    否则如何在查询中为标题和副标题添加模糊性?

    {
      "query": {
        "multi_match": {
            "query": "Bleu Amazing T-Shirt",
            "fuzziness": "auto",
            "operator": "and",
            "fields": [
              "identity.partNumber^4",
              "identity.altIdentifier^4",
              "identity.barcode",
              "identity.mpn",
              "identity.ppn",
              "descriptions.title",
              "descriptions.subtitle"
            ],
            "type": "cross_fields"
          }
      },
      "fields": [
        "identity.partNumber",
        "identity.barcode",
        "identity.ppn",
        "descriptions.title",
        "descriptions.subtitle"
      ]
    }
    

1 个答案:

答案 0 :(得分:0)

好吧,似乎不支持使用cross_fields进行模糊搜索,但有一些相关问题。因此,我没有使用跨域搜索,而是复制了标题&在索引时将字幕添加到新字段并按如下方式拆分查询。似乎至少为我的测试用例工作....

"query": {
      "bool": {
        "should": [
          {
            "multi_match": {
              "query": "{{searchTerm}}",
              "operator": "and",
              "fields": [
                "identity.partNumber^4",
                "identity.altIdentifier^4",
                "identity.barcode",
                "identity.mpn",
                "identity.ppn"
              ],
              "type": "best_fields"
            }
          },
          {
            "match": {
              "fuzzyFields": {
                "query": "{{searchTerm}}",
                "operator": "and",
                "fuzziness": "auto"
              }
            }
          }
        ]
      }
    }