【问题标题】:Elasticsearch partial matching on postal address and customer number邮政地址和客户编号的 Elasticsearch 部分匹配
【发布时间】:2022-01-20 17:00:38
【问题描述】:

我正在尝试将搜索词部分匹配到给定架构以进行自动完成。我希望 customerNumber 和 AddressLine1 和 Zip 匹配以 419 开头的任何文档(因此 4191 应该匹配客户编号 41915678 和地址 4191 Board Street 和邮政编码 41912)

"mappings": {
    "companyName": {
        "type": "text"
    },
    "customerNumber": {
        "type": "long"
    }
    "address": {
        "addressLine1": {
            "type": "text"
        },
        "city": {
            "type": "text"
        },
        "state": {
            "type": "text"
        },
        "zip": {
            "type": "text"
        }
    }
}

有人对查询有很好的解决方案吗?最终我需要使用 NEST 客户端将此查询转换为 C#。

【问题讨论】:

  • 你的数据量大概是多少?
  • 您可以在映射中使用前缀查询(查询时间慢)或边 n-gram(索引时间慢)。
  • 90 万条记录,但数据相当静态。
  • 谁能协助n-gram查询?缓慢的索引时间比缓慢的查询时间更容易接受。

标签: elasticsearch nest


【解决方案1】:

一种简单的方法是利用completion suggester field type

基本上,您可以通过在映射中添加completion field 来修改映射,例如

  "suggest": {
    "type": "completion"
  },

但是,完成字段的默认分析器(即simple analyzer)不索引数字,我们需要创建能够正确执行此操作的自定义分析器:

PUT my-index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "suggest_analyzer": {         <--- custom analyzer
          "type": "custom",
          "tokenizer": "classic",
          "filter": [
            "lowercase"
          ]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      ...,
      "suggest": {                    <--- the new completion field with the right analyzer
        "type": "completion",
        "analyzer": "suggest_analyzer"
      }
    }
  }
}

然后您只需要通过在建议字段中添加您想要建议的所有值来填充您的索引,如下所示:

PUT my-index/_doc/1
{
  "address": {
    "addressLine1": "1234 Main Street",
    "zip": "34526"
  },
  "customerNumber": "41915678",
  "suggest": [
    "1234 Main Street",
    "34526",
    "41915678"
  ]
}
PUT my-index/_doc/2
{
  "address": {
    "addressLine1": "4191 Board Street",
    "zip": "45263"
  },
  "customerNumber": "45267742",
  "suggest": [
    "4191 Board Street",
    "45263",
    "45267742"
  ]
}
PUT my-index/_doc/3
{
  "address": {
    "addressLine1": "5662 4th Avenue",
    "zip": "41912"
  },
  "customerNumber": "24442561",
  "suggest": [
    "5662 4th Avenue",
    "41912",
    "24442561"
  ]
}

然后,您可以使用以下建议查询搜索419

POST my-index/_search
{
  "suggest": {
    "customer-suggest": {
      "prefix": "419",
      "completion": {
        "field": "suggest"
      }
    }
  }
}

您将获得所有三个文档,因为每个文档都有一个与 419 匹配的字段

【讨论】:

  • 谢谢,初始运行似乎按预期工作。无论如何要在响应的“命中”节点而不是“建议”节点中获得结果?
  • 补全提示器的工作方式与常规搜索查询不同,即它不适用于倒排索引,而是来自他们所谓的完全加载到内存中的有限状态传感器 (FST),因此速度。所以结果不会与命中混合。您也可以尝试使用 search_as_you_type 字段类型,该字段类型将处理常规查询并在命中部分返回结果。
  • 感谢您的解释和解决方案。
  • 酷,很高兴它有帮助!
猜你喜欢
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-07
  • 1970-01-01
相关资源
最近更新 更多