Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - get source field data with java api

I'm using elastic search with jest (as java client). I need some fields that is in nested document and since cannot get nested fields as pair, I need '_source' to get them.

Here is previous question to get them in ES query[ Link ], and It works well.

BUT cannot convert its query as jest code. Below is my try.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query( 
            query
        )
        .fields(      // need _source but no method.
          "oid", 
          "_source.events.activityoid", 
          "_source.events.worktime");
like image 378
J.Done Avatar asked Jul 29 '16 08:07

J.Done


People also ask

How do I select 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 get Elasticsearch 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 .

What is source field in Elasticsearch?

The _source field contains the original JSON document body that was passed at index time. The _source field itself is not indexed (and thus is not searchable), but it is stored so that it can be returned when executing fetch requests, like get or search.

Is Elasticsearch Java based?

Elasticsearch is developed in Java and is dual-licensed under the source-available Server Side Public License and the Elastic license, while other parts fall under the proprietary (source-available) Elastic License.


1 Answers

Try using fetchSource() like this:

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
    .query(query)
    .fetchSource(new String[] {
      "oid", 
      "events.activityoid", 
      "events.worktime"
    }, null);
like image 161
Val Avatar answered Oct 14 '22 06:10

Val