弹性搜索内部命中java api [英] Elasticsearch inner hits in java api

查看:279
本文介绍了弹性搜索内部命中java api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用java api实现弹性搜索的内部命中,但是我找不到任何文档或其他人使用的示例。我的JSON搜索工作如下:

  {
查询:{
过滤:{
查询:{
match_all:{}
},
过滤器:{
嵌套:{
path:locations,
filter:{
geo_distance:{
distance:20km,
locations.address.geoLocation {
lat:38.07061,
lon:-76.77514
}
}
},
inner_hits:{}
}
}
}
}
}

我在elasticsearch库中看到一个InnerHitsBuilder和addInnerHit方法,但是我找不到有关如何使用它们的文档。



任何帮助或指针都不胜感激,



Jason

解决方案

请注意,ES源代码中有许多测试用例正在测试每个功能,因此浏览ES代码是一个非常丰富的信息来源。内部命中也不例外,您可以在 inner_hits 测试用例/test/java/org/elasticsearch/search/innerhits/InnerHitsTests.javarel =noreferrer> InnerHitsTests.java 类。



所以上面的查询可以这样创建:

  //构建geo_distance过滤器
GeoDistanceFilterBuilder geo = FilterBuilders
.geoDistanceFilter(locations.address.geoLocation)
.distance(20km)
.lat(38.07061)
.lon -76.77514);

//构建嵌套过滤器并添加inner_hits
NestedFilterBuilder嵌套= FilterBuilders
.nestedFilter(locations,geo)
.innerHit(new QueryInnerHitBuilder()) ; < ---这是你要找的

//将所有内容都包含在过滤的查询中
FilteredQueryBuilder query = QueryBuilders
.filteredQuery(QueryBuilders.matchAllQuery() ,嵌套);


I am trying to implement inner hits with elasticsearch using the 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.

Any help or pointers are appreciated,

Jason

解决方案

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

这篇关于弹性搜索内部命中java api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆