弹性搜索本机查询中只有 return 个特定字段 Java api
Only return specific fields in elastic search native query Java api
我正在构建本机查询,但我只想 return 某些字段,所有这些字段都包含在一个父字段中。我想我正在寻找与 REST API 的 _source 等效的 QueryBuilders 或 NativeSearchQueryBuilder。这是一个代码示例:
NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
.withIndices("myIndex")
.withTypes("myType")
.withQuery(QueryBuilders.queryStringQuery("parent.field2:Foo*"));
.withFields("parent.field1");
我希望这是一个 return 列表,其中只有 parent.field1 与具有 parent.field2 的对象相关联,例如 Foo*。但它 return 没什么。
感谢您的帮助!
经过一番研究,我发现答案在 NativeSearchQueryBuilder 中。我只是使用旧版本的 spring-data 弹性搜索,所以我看不到这个方法:withSourceFilter。这样做的方法是:
NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
.withIndices("myIndex")
.withTypes("myType")
.withQuery(QueryBuilders.queryStringQuery("parent.field2:Foo*"));
.withSourceFilter(new FetchSourceFilter(<String array of includes>, null));
FetchSourceFilter 有 2 个参数,一个包含的 String[] 数组和一个排除的数组。在我的示例中,我将一个类似 new String[]{"parent.field1"} 的数组传递给 FetchSourceFilter,后者又传递给 withSourceFilter。上面的搜索将 return(一旦构建并 运行)parent.field1 和 parent.field2 的列表,如 Foo*。
我升级到的版本是spring-data-elasticsearch 2.0.2。
我正在构建本机查询,但我只想 return 某些字段,所有这些字段都包含在一个父字段中。我想我正在寻找与 REST API 的 _source 等效的 QueryBuilders 或 NativeSearchQueryBuilder。这是一个代码示例:
NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
.withIndices("myIndex")
.withTypes("myType")
.withQuery(QueryBuilders.queryStringQuery("parent.field2:Foo*"));
.withFields("parent.field1");
我希望这是一个 return 列表,其中只有 parent.field1 与具有 parent.field2 的对象相关联,例如 Foo*。但它 return 没什么。
感谢您的帮助!
经过一番研究,我发现答案在 NativeSearchQueryBuilder 中。我只是使用旧版本的 spring-data 弹性搜索,所以我看不到这个方法:withSourceFilter。这样做的方法是:
NativeSearchQueryBuilder sb = new NativeSearchQueryBuilder()
.withIndices("myIndex")
.withTypes("myType")
.withQuery(QueryBuilders.queryStringQuery("parent.field2:Foo*"));
.withSourceFilter(new FetchSourceFilter(<String array of includes>, null));
FetchSourceFilter 有 2 个参数,一个包含的 String[] 数组和一个排除的数组。在我的示例中,我将一个类似 new String[]{"parent.field1"} 的数组传递给 FetchSourceFilter,后者又传递给 withSourceFilter。上面的搜索将 return(一旦构建并 运行)parent.field1 和 parent.field2 的列表,如 Foo*。
我升级到的版本是spring-data-elasticsearch 2.0.2。