我正在尝试使用Java API通过Elasticsearch实现内部匹配,但是我找不到关于它的许多文档或其他人正在使用的示例。我的JSON搜索工作如下:
{ "query": { "filtered": { "query": { "match_all": {} }, "filter": { "nested": { "path": "locations", "filter": { "geo_distance": { "distance": "20km", "locations.address.geoLocation": { "lat": 38.07061, "lon": -76.77514 } } }, "inner_hits": {} } } } } }
我在elasticsearch库中看到了InnerHitsBuilder和addInnerHit方法,但是找不到有关如何使用它们的文档。
请注意,ES源代码中有大量测试用例,正在测试每个功能,因此浏览ES代码是非常丰富的信息源。内部命中也不例外,您可以inner_hits在InnerHitsTests.java课程中找到所有测试用例。
inner_hits
InnerHitsTests.java
因此,上面的查询可以这样创建:
// build the geo_distance filter GeoDistanceFilterBuilder geo = FilterBuilders .geoDistanceFilter("locations.address.geoLocation") .distance("20km") .lat(38.07061) .lon(-76.77514); // build the nested filter and add inner_hits NestedFilterBuilder nested = FilterBuilders .nestedFilter("locations", geo) .innerHit(new QueryInnerHitBuilder()); <--- this is what you're looking for // wrap it all inside a filtered query FilteredQueryBuilder query = QueryBuilders .filteredQuery(QueryBuilders.matchAllQuery(), nested);