Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving specific fields using the Elasticsearch Java API

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

like image 715
user1052610 Avatar asked May 27 '15 08:05

user1052610


People also ask

How do I capture a specific field in Elasticsearch?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.

How do I search all fields in Elasticsearch?

Either the query_string query or the match query would be what you're looking for. query_string will use the special _all field if none is specified in default_field , so that would work out well. And with match you can just specify the _all as well.

What are stored fields in Elasticsearch?

Stored fields returned as arrays For consistency, stored fields are always returned as an array because there is no way of knowing if the original field value was a single value, multiple values, or an empty array. If you need the original value, you should retrieve it from the _source field instead.

How do I get Elasticsearch index data?

You can use the search API to search and aggregate data stored in Elasticsearch data streams or indices. The API's query request body parameter accepts queries written in Query DSL. The following request searches my-index-000001 using a match query. This query matches documents with a user.id value of kimchy .


1 Answers

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.

like image 63
Val Avatar answered Sep 28 '22 10:09

Val