Elasticsearch inner hits in java api

不想你离开。 提交于 2019-12-23 10:07:04

问题


I am trying to implement inner hits with elasticsearch using Java API, but I cannot find much of any documentation on it or examples that other people are using. I have my JSON search that works as follows:

{
  "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": {}
        }
      }
    }
  }
}

I see an InnerHitsBuilder and addInnerHit methods in the elasticsearch library but I cannot find the documentation on how to use them.


回答1:


Note that there are plenty of test cases in the ES source code where each feature is being tested, so browsing the ES code is a incredibly rich source of information. Inner hits makes no exception and you can find all inner_hits test cases in the InnerHitsTests.java class.

So your query above can be created like this:

    // 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);


来源:https://stackoverflow.com/questions/32830299/elasticsearch-inner-hits-in-java-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!