使用Elasticsearch Java API检索特定字段 [英] Retrieving specific fields using the Elasticsearch Java API

查看:112
本文介绍了使用Elasticsearch Java API检索特定字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java API for Elasticsearch。
将实体保存到索引中,可以与完整的源一起检索它们。但是,我只想检索所选的字段,而且这不起作用。



下面的示例代码:

  SearchResponse response = client.prepareSearch(my-index)
.setTypes(my-type)
.setSearchType(SearchType.QUERY_AND_FETCH)
.setFetchSource(true)
.setQuery(QueryBuilders.termsQuery(field1,1234))
.addFields(field1)
.execute()
。 actionGet();

for(SearchHit命中:response.getHits()){
Map< String,SearchHitField> fields = hit.getFields();
System.out.println(fields.size());
地图map = hit.getSource();
map.toString();
}

将从索引中检索正确的实体,包括完整的来源。 p>

例如,这是一个响应的片段:

 命中:{
total:1301,
max_score:0.99614644,
hits:[{
_index:my-index,
_type:my-type,
_id:AU2P68COpzIypBTd80np,
_score:0.99614644,
_source:{field1:1234 ...}}}
},{

然而,虽然 response.getHits()返回预期的匹配数,字段每个命中都是空的。



我期望每个命中都包含该行中指定的字段:

  .addFields(field1)

注释行

  .setFetchSource(true)

将导致响应不包括源



Elasticsearch的版本是1.5.0



以下是Java API的maven依赖关系:

 <依赖关系> 
< groupId> com.sksamuel.elastic4s< / groupId>
< artifactId> elastic4s_2.11< / artifactId>
< version> 1.5.5< / version>
< / dependency>

因为性能原因,我不想要检索完整的实体。
有没有人知道如何将检索限制到选定的字段?
谢谢

解决方案

您可以使用 setFetchSource(String [] includes,String []不包括)方法。尝试这样做

  SearchResponse response = client.prepareSearch(my-index)
.setTypes(my-类型)
.setSearchType(SearchType.QUERY_AND_FETCH)
.setFetchSource(new String [] {field1},null)
.setQuery(QueryBuilders.termsQuery(field1,1234 ))
.execute()
.actionGet(); ($)

(SearchHit命中:response.getHits()){
Map map = hit.getSource();
map.toString();
}

map 包含您指定的字段。



请注意, .setFetchSource(field1,null)(如果您需要单个字段)或 .setFetchSource(field *,null)(如果您需要几个通配符字段)也可以。


I am using the Java API for Elasticsearch. Having saved entities into indexes, it is possible to retrieve them together with the complete source. However, I only want to retrieve selected fields, and that is not working.

The folowing sample code:

SearchResponse response = client.prepareSearch("my-index")
   .setTypes("my-type")
   .setSearchType(SearchType.QUERY_AND_FETCH)
   .setFetchSource(true)
   .setQuery(QueryBuilders.termsQuery("field1", "1234"))
   .addFields("field1")
   .execute()
   .actionGet();

for (SearchHit hit : response.getHits()){
   Map<String, SearchHitField> fields = hit.getFields();
   System.out.println(fields.size());
   Map map = hit.getSource();
   map.toString();
}

will retrieve the correct entities from the index, including the complete source.

For example, this is a snippet of the response :

"hits" : {
  "total" : 1301,
  "max_score" : 0.99614644,
  "hits" : [ {
  "_index" : "my-index",
  "_type" : "my-type",
  "_id" : "AU2P68COpzIypBTd80np",
  "_score" : 0.99614644,
  "_source":{"field1":"1234", ...}]}
}, {

However, while response.getHits() returns the expected number of hits, the fields and source within each hit is empty.

I am expecting each hit to contain the field specified in the line:

.addFields("field1")

Commenting out the line

.setFetchSource(true)

will cause the response not to include the source at all.

The version of Elasticsearch is 1.5.0

The following is the maven dependency the Java API:

<dependency>
   <groupId>com.sksamuel.elastic4s</groupId>
   <artifactId>elastic4s_2.11</artifactId>
   <version>1.5.5</version>
</dependency>

Obiously, for performance reasons, I don't want to have to retrieve the complete entity. Does anyone know how to limit the retrieval to selected fields? Thanks

解决方案

You can specify the fields you need using the setFetchSource(String[] includes, String[] excludes) method. Try this instead

SearchResponse response = client.prepareSearch("my-index")
   .setTypes("my-type")
   .setSearchType(SearchType.QUERY_AND_FETCH)
   .setFetchSource(new String[]{"field1"}, null)
   .setQuery(QueryBuilders.termsQuery("field1", "1234"))
   .execute()
   .actionGet();

for (SearchHit hit : response.getHits()){
   Map map = hit.getSource();
   map.toString();
}

map will only contain the fields you've specified.

Note that .setFetchSource("field1", null) (if you need a single field) or .setFetchSource("field*", null) (if you need several wildcarded fields) would work, too.

这篇关于使用Elasticsearch Java API检索特定字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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