elasticsearch GEO从近到远的距离搜索顺序

时间:2017-06-20 07:27:40

标签: java elasticsearch spring-boot

映射address属性:id name typelocation。 搜索映射:

{
  "address": {
    "properties": {
      "id": {
        "type": "long"
      },
      "type": {
        "type": "long"
      },
      "name": {
        "type": "string"
      },
      "location": {
        "type": "geo_point"
      }
    }
  }
}

搜索cmd(过滤器address.type = 1distance = 100km):

XGET /_search
{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "type": 1
        }
      },
      "filter": {
        "geo_distance": {
          "distance": "100km",
          "location": {
            "lat": 24.46667,
            "lon": 118.1
          }
        }
      }
    }
  }
}

我想搜索地址匹配type = 1和地理distance = 100km;我希望得到order by distance ASC的结果。怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

因为你是根据距离进行排序的,所以我在布尔过滤器中移动了匹配查询。

这应该有效

{
    "query": {
        "bool": {
            "must": [{
                    "geo_distance": {
                        "distance": "1000000km",
                        "location": {
                            "lat": 24.46667,
                            "lon": 118.1
                        }
                    }
                },
                {
                    "term": {
                        "type": {
                            "value": 1
                        }
                    }
                }
            ]
        }
    },
    "sort": {
        "_geo_distance": {
            "location": "24.46667,118.1",
            "order": "asc"
        }
    }
}