我正在使用elasticSearch的Java API . 我想搜索某个点附近的帖子 . 因此,帖子具有坐标字段 . 这是我实现映射的代码:
String mappingString =
"{"
+ "\"posts\" : {"
+ "\"properties\" : {"
+ "\"coordinates\" : {"
+ "\"type\" : \"geo_point\""
+ "}"
+ "}"
+ "}"
+"}";
Client.admin().indices().preparePutMapping("posts").setType("posts").setSource(mappingString).execute().actionGet ();
我通过ElasticSearch Head验证了我确实在索引中有应该通过过滤器的帖子 . 帖子看起来像这样:
{
"link":"http://instagram.com/p/hBV...EjHk/",
"pictureURL":"http://distilleryimage0.s3.amazonaws.com/b5887...8f21ef_6.jpg",
"userID":"358...3",
"commentsCount":0,
"likesCount":2,
"coordinates":{"lat":32.519634,"lon":33.414997},
"userName":"tuvs...yahde",
"nGrams":[],
"id":"IG_5948523...879524_358...163",
"indexDate":1385131949
}
这将存储在名为“posts”的索引中 . 但是,我在该索引中确实有不同的类型,所有这些类型共享坐标,id和indexDate字段,但在其他字段中可能不同(这是因为某些帖子有图片,其他帖子有文本,其他都有文字) .
最后,这是我用于查询ElasticSearch的代码:
GeoDistanceFilterBuilder gdFilter = FilterBuilders.geoDistanceFilter("posts.coordinates")
.point(lat, lon)
.distance(distanceInMeters, DistanceUnit.METERS)
.optimizeBbox("memory")
.geoDistance(GeoDistance.ARC);
SearchResponse response = Client.prepareSearch("posts")
.setQuery(QueryBuilders.matchAllQuery())
.setFilter(gdFilter)
.setSize(100)
.execute()
.actionGet();
问题是,无论我对查询和过滤器中的变量使用什么值,我总是得到零结果 . 有人能看到,我做错了什么?
1 回答
它应该是
.point(lat, lon)
.