elasticsearch API 中的 AND & OR 条件

普拉哈维尔马

我的 elasticSearch 中存储了带有地址组件的地址,每个地址在我的 ES 中如下所示:

 {
                 "_index": "properties",
                 "_type": "_doc",
                 "_id": "property_5235354",
                 "_score": 32.839436,
                 "_source": {
                     "id": 5235354,
                     "branchid": 1,
                     "suburb": "Lyons",
                     "postcode": "2606",
                     "state": "ACT",
                     "@timestamp": "2021-09-27T08:56:08.827Z",
                     "agencycode": "X",
                     "address": "54-5 Burnie St Lyons ACT 2606 AUS",
                     "streetnumber": "5",
                     "branchcode": "X_ACT",
                     "unitnumber": "54",
                     "agencyid": 1,
                     "streetname": "Burnie St",
                     "@version": "1"
                 }
}

要根据组件搜索特定地址,我正在考虑以下几点:

  1. 可能有街道名称的缩写,如“詹姆斯街”->“詹姆斯街”
  2. 以不区分大小写的方式通过具有精确匹配的地址组件进行匹配
  3. 如果您认为我应该考虑其他事情,请告诉我

为此,我尝试了以下操作:

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "streetname.keyword": "Burnie Street"
                    }
                },
                {
                    "match": {
                        "streetname.keyword": "Burnie St"
                    }
                }
            ],
            "must": [
                {
                    "match": {
                        "unitnumber.keyword": "54"
                    }
                },
                {
                    "match": {
                        "streetnumber.keyword": "5"
                    }
                },
                {
                    "match": {
                        "suburb.keyword": "Lyons"
                    }
                },
                {
                    "match": {
                        "state": "ACT"
                    }
                },
                {
                    "match": {
                        "postcode.keyword": "2606"
                    }
                }
            ]
        }
    },
    "size": 1000
}

需要您的帮助来解决这些问题:

  1. 上面的查询也返回无效的结果,如地址:54-5 Burnie Avenue Lyons ACT 2606 AUS这是伯尼大道而不是伯尼街。
  2. 如果我给burnie street而不是Burnie Street,则无法找到数据。

更多信息:这是关于以上请求体,其中地址_search API的全部结果54-5 Burnie St Lyons ACT 2606 AUS54/5 Burnie Street Lyons ACT 2606是正确的匹配,但54-5 Burnie Avenue Lyons ACT 2606 AUS是一个无效的比赛

{
    "took": 1476,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 32.839436,
        "hits": [
            {
                "_index": "properties",
                "_type": "_doc",
                "_id": "property_5235354",
                "_score": 32.839436,
                "_source": {
                    "id": 5235354,
                    "branchid": 1,
                    "suburb": "Lyons",
                    "postcode": "2606",
                    "state": "ACT",
                    "@timestamp": "2021-09-27T08:56:08.827Z",
                    "agencycode": "X",
                    "address": "54-5 Burnie St Lyons ACT 2606 AUS",
                    "streetnumber": "5",
                    "branchcode": "X_ACT",
                    "unitnumber": "54",
                    "agencyid": 1,
                    "streetname": "Burnie St",
                    "@version": "1"
                }
            },
            {
                "_index": "properties",
                "_type": "_doc",
                "_id": "property_11081",
                "_score": 28.954222,
                "_source": {
                    "id": 11081,
                    "branchid": 1,
                    "suburb": "Lyons",
                    "postcode": "2606",
                    "state": "ACT",
                    "@timestamp": "2021-09-27T08:56:08.163Z",
                    "agencycode": "X",
                    "address": "54/5 Burnie Street Lyons ACT 2606",
                    "streetnumber": "5",
                    "branchcode": "X_ACT",
                    "unitnumber": "54",
                    "agencyid": 1,
                    "streetname": "Burnie Street",
                    "@version": "1"
                }
            },
            {
                "_index": "properties",
                "_type": "_doc",
                "_id": "property_5235356",
                "_score": 22.677355,
                "_source": {
                    "id": 5235356,
                    "branchid": 1,
                    "suburb": "Lyons",
                    "postcode": "2606",
                    "state": "ACT",
                    "@timestamp": "2021-09-27T08:56:08.847Z",
                    "agencycode": "X",
                    "address": "54-5 Burnie Avenue Lyons ACT 2606 AUS",
                    "streetnumber": "5",
                    "branchcode": "X_ACT",
                    "unitnumber": "54",
                    "agencyid": 1,
                    "streetname": "Burnie Avenue",
                    "@version": "1"
                }
            }
        ]
    }
}
编码器

您需要使用bool/must/should查询子句、术语查询(忽略大小写的精确匹配)和match_phrase_prefix查询的组合

索引映射:

{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "@version": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "agencycode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "agencyid": {
        "type": "long"
      },
      "branchcode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "branchid": {
        "type": "long"
      },
      "id": {
        "type": "long"
      },
      "postcode": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "state": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "streetname": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "streetnumber": {
        "type": "integer"
      },
      "suburb": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "unitnumber": {
        "type": "integer"
      }
    }
  }
}

搜索查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "streetnumber": "5"
          }
        },
        {
          "term": {
            "unitnumber": "54"
          }
        },
        {
          "bool": {
            "should": [
              {
                "term": {
                  "streetname.keyword": {
                    "value": "Burnie Street",
                    "case_insensitive": "true"
                  }
                }
              },
              {
                "match_phrase_prefix": {
                  "streetname": "Burnie St"
                }
              }
            ]
          }
        },
        {
          "term": {
            "suburb.keyword": {
              "value": "Lyons",
              "case_insensitive": "true"
            }
          }
        },
        {
          "term": {
            "postcode.keyword": "2606"
          }
        },
        {
          "term": {
            "state.keyword": {
              "value": "ACT",
              "case_insensitive": "true"
            }
          }
        }
      ]
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章