spring-data-elasticsearchspring-data-opensearch

How to retrieve stored fields using spring-data-opensearch with _source disabled?


I have an index template like below

{
  "index_patterns": [
    "request_response-*"
  ],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "index": {
        "codec": "best_compression"
      }
    },
    "mappings": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "value": {
          "type": "binary",
          "store": true
        }
      }
    },
    "aliases": {
      "request_response": { }
    }
  },
  "priority": 500
}

Can someone let me know how can I execute the below query using spring-data-opensearch?

curl -X GET "localhost:9200/request_response-*/_search?stored_fields=value&pretty" -H 'Content-Type: application/json' -d'
{
  "query": {
    "term": {
      "_id": {
        "value": "092d882489ea6dc4a6d2dc16d97fb72192b17ca97eaaca59961e97597b6ba80c"
      }
    }
  }
}
'

It returns the below result

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "request_response-2024-05-09",
        "_type" : "_doc",
        "_id" : "092d882489ea6dc4a6d2dc16d97fb72192b17ca97eaaca59961e97597b6ba80c",
        "_score" : 1.0,
        "fields" : {
          "value" : [
            "U29tZSBiaW5hcnkgYmxvYg=="
          ]
        }
      },
      {
        "_index" : "request_response-2024-05-10",
        "_type" : "_doc",
        "_id" : "092d882489ea6dc4a6d2dc16d97fb72192b17ca97eaaca59961e97597b6ba80c",
        "_score" : 1.0,
        "fields" : {
          "value" : [
            "U29tZSBiaW5hcnkgYmxvYg=="
          ]
        }
      }
    ]
  }
}

I tried the below using spring-data-opensearch

String documentId = "092d882489ea6dc4a6d2dc16d97fb72192b17ca97eaaca59961e97597b6ba80c";
    String[] storedFields = new String[]{"value"};
    IndexCoordinates indexCoordinates = IndexCoordinates.of("request_response-*");

    NativeSearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withQuery(QueryBuilders.termQuery("_id", documentId))
        .withFields(storedFields)
        .build();

    SearchHits<KeyValueDoc> searchHits = elasticsearchOperations.search(searchQuery, KeyValueDoc.class, indexCoordinates);
    KeyValueDoc document = searchHits.getSearchHit(0).getContent();

But it is throwing the below error

Caused by: OpenSearchException[OpenSearch exception [type=illegal_argument_exception, reason=Unable to retrieve the requested [fields] since _source is disabled in the mappings for index [request_response-2024-05-09]]]; nested: OpenSearchException[OpenSearch exception [type=illegal_argument_exception, reason=Unable to retrieve the requested [fields] since _source is disabled in the mappings for index [request_response-2024-05-09]]];
    at org.opensearch.OpenSearchException.innerFromXContent(OpenSearchException.java:550)
    at org.opensearch.OpenSearchException.fromXContent(OpenSearchException.java:461)
    at org.opensearch.OpenSearchException.innerFromXContent(OpenSearchException.java:491)
    at org.opensearch.OpenSearchException.failureFromXContent(OpenSearchException.java:655)
    at org.opensearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:199)
    ... 80 more
Caused by: OpenSearchException[OpenSearch exception [type=illegal_argument_exception, reason=Unable to retrieve the requested [fields] since _source is disabled in the mappings for index [request_response-2024-05-09]]]
    at org.opensearch.OpenSearchException.innerFromXContent(OpenSearchException.java:550)
    at org.opensearch.OpenSearchException.fromXContent(OpenSearchException.java:461)
    at org.opensearch.OpenSearchException.innerFromXContent(OpenSearchException.java:491)
    ... 84 more

Solution

  • This is answered in OpenSearch Forum.

        NativeSearchQuery searchQuery =
            new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.termQuery("_id", doc.getKey()))
                .withStoredFields(new String[] {"value"})
                .build();
        elasticsearchOperations.search(
            searchQuery, KeyValueDoc.class, IndexCoordinates.of("request_response-*"));