弹性搜索 - 与多个字段进行模糊和严格匹配



我们希望利用ElasticSearch为我们找到类似的对象。

假设我有一个包含 4 个字段的对象: product_name、seller_name、seller_phone、platform_id。

类似商品在不同平台上可以有不同的商品名称和卖家名称(模糊匹配)。

虽然,电话是严格的,单个变体可能会导致产生错误的记录(严格匹配)。

尝试创建的是一个查询,它将:

  1. 考虑当前记录和OR的所有字段 他们之间。
  2. 任务platform_id是我想具体研究的。(和)
  3. 模糊product_name和seller_name
  4. 严格匹配电话号码或在字段之间的OR中忽略它。

如果我用伪代码编写它,我会写这样的东西:

(

(product_name像"some_product_name")或(seller_name像 "some_seller_name")或(seller_phone = "some_phone"))和(platform_id = 123)

为了对seller_phone进行精确匹配,我在没有 ngram 分析器的情况下索引此字段以及用于product_nameseller_name的fuzzy_query

映射

PUT index111
{
"settings": {
"analysis": {
"analyzer": {
"edge_n_gram_analyzer": {
"tokenizer": "whitespace",
"filter" : ["lowercase",  "ednge_gram_filter"]
}
},
"filter": {
"ednge_gram_filter" : {
"type" : "NGram",
"min_gram" : 2,
"max_gram": 10
}
}
}
},
"mappings": {
"document_type" : {
"properties": {
"product_name" : {
"type": "text",
"analyzer": "edge_n_gram_analyzer"
},
"seller_name" : {
"type": "text",
"analyzer": "edge_n_gram_analyzer"
},
"seller_phone" : {
"type": "text"
},
"platform_id" : {
"type": "text"
}
}
}
}
}

索引文档

POST index111/document_type
{
"product_name":"macbok",
"seller_name":"apple",
"seller_phone":"9988",
"platform_id":"123"
}

对于以下伪 sql 查询

((product_name like 'some_product_name') OR (seller_name like 'some_seller_name') OR (seller_phone = 'some_phone')) AND (platform_id = 123)

弹性查询

POST index111/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"platform_id": {
"value": "123"
}
}
},
{
"bool": {
"should": [{
"fuzzy": {
"product_name": {
"value": "macbouk",
"boost": 1.0,
"fuzziness": 2,
"prefix_length": 0,
"max_expansions": 100
}
}
},
{
"fuzzy": {
"seller_name": {
"value": "apdle",
"boost": 1.0,
"fuzziness": 2,
"prefix_length": 0,
"max_expansions": 100
}
}
},
{
"term": {
"seller_phone": {
"value": "9988"
}
}
}
]
}
}]
}
}
}

希望这有帮助

最新更新