Search code examples
elasticsearchelasticsearch-jest

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

Solution

  • Try using fetchSource() like this:

    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
        .query(query)
        .fetchSource(new String[] {
          "oid", 
          "events.activityoid", 
          "events.worktime"
        }, null);