返回OpenSearch/ElasticSearch中按最短距离排序的唯一地址

t5zmwmid  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(150)

我有一个数据库与以下样本。

"_index" : "python-address-test",
    "_type" : "_doc",
    "_id" : "1",
    "_score" : 0.0,
    "_source" : {
      "address" : "Graha Blok A",
      "geohash6" : "qqgft0",
      "location" : {
        "lat" : -6.5881896,
        "lon" : 106.747485
      }
    }
  },
  {
    "_index" : "python-address-test",
    "_type" : "_doc",
    "_id" : "2",
    "_score" : 0.0,
    "_source" : {
      "address" : "Graha",
      "geohash6" : "qqgft0",
      "location" : {
        "lat" : -6.5895002,
        "lon" : 106.7488968
      }
    }
  },
  {
    "_index" : "python-address-test",
    "_type" : "_doc",
    "_id" : "3",
    "_score" : 0.0,
    "_source" : {
      "address" : "Graha",
      "geohash6" : "qqgft0",
      "location" : {
        "lat" : -6.5884867,
        "lon" : 106.749212

预期退货应为,其中地址应唯一
{格拉哈A区,格拉哈}
我已经写了下面的代码,并能够得到最短距离的地址输入lat,long。但我无法得到唯一的地址。请告知

{ "size": 10, 
  "query": {
    "bool": {
      "must": {
        "geo_distance": {
          "distance": "1km",
          "location": [
            106.7485418,
            -6.5875987
          ]
        }
      },
      "filter": {"match" : {"geohash6" : "qqgft0"}}
    }
  },
  "sort": [
    "_score",
    {
      "_geo_distance": {
        "location": { 
          "lat":  -6.5875987,
          "lon": 106.7485418
        },
        "order":         "asc",
        "unit":          "m", 
        "distance_type": "plane" 
      }
    }
  ]
}
ee7vknir

ee7vknir1#

您可以使用折叠查询,使每个地址仅显示1个结果:
https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html
下面是示例代码:

Map

PUT test_geo
{
  "mappings": {
    "properties": {
      "address": {
        "type": "text",
        "fields": {
          "keyword": {
            "type":"keyword"
          }
        }
      },
      "geohash6": {
        "type": "keyword"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}

文件

POST test_geo/_doc
{
  "address": "Graha Blok A",
  "geohash6": "qqgft0",
  "location": {
    "lat": -6.5881896,
    "lon": 106.747485
  }
}

POST test_geo/_doc
{
  "address": "Graha Blok A",
  "geohash6": "qqgft0",
  "location": {
    "lat": -6.5881896,
    "lon": 106.747485
  }
}

POST test_geo/_doc
{
  "address": "Graha",
  "geohash6": "qqgft0",
  "location": {
    "lat": -6.5895002,
    "lon": 106.7488968
  }
}

查询

GET test_geo/_search
{
  "size": 10,
  "collapse": {
    "field": "address.keyword"
  }, 
  "query": {
    "bool": {
      "must": {
        "geo_distance": {
          "distance": "1km",
          "location": [
            106.7485418,
            -6.5875987
          ]
        }
      },
      "filter": {
        "match": {
          "geohash6": "qqgft0"
        }
      }
    }
  },
  "sort": [
    "_score",
    {
      "_geo_distance": {
        "location": {
          "lat": -6.5875987,
          "lon": 106.7485418
        },
        "order": "asc",
        "unit": "m",
        "distance_type": "plane"
      }
    }
  ]
}

结果

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "test_geo",
        "_id": "PBC34oQBtNG1OrZokQMs",
        "_score": 1,
        "_source": {
          "address": "Graha Blok A",
          "geohash6": "qqgft0",
          "location": {
            "lat": -6.5881896,
            "lon": 106.747485
          }
        },
        "fields": {
          "address.keyword": [
            "Graha Blok A"
          ]
        },
        "sort": [
          1,
          133.96461555682822
        ]
      },
      {
        "_index": "test_geo",
        "_id": "hHa34oQB5Gw0WET8m33u",
        "_score": 1,
        "_source": {
          "address": "Graha",
          "geohash6": "qqgft0",
          "location": {
            "lat": -6.5895002,
            "lon": 106.7488968
          }
        },
        "fields": {
          "address.keyword": [
            "Graha"
          ]
        },
        "sort": [
          1,
          215.04628939232288
        ]
      }
    ]
  }
}

相关问题