7

我正在尝试使用 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 方法,但我找不到有关如何使用它们的文档。

4

1 回答 1

9

请注意,在 ES 源代码中有大量测试用例,每个功能都在测试,因此浏览 ES 代码是一个非常丰富的信息来源。Inner hits 也不例外,你可以在课堂上找到所有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);
于 2015-09-30T06:51:29.493 回答