Search in sources :

Example 1 with FetchSourceContext

use of org.opensearch.search.fetch.subphase.FetchSourceContext in project OpenSearch by opensearch-project.

the class RequestConvertersTests method randomizeFetchSourceContextParams.

/**
 * Randomize the {@link FetchSourceContext} request parameters.
 */
private static void randomizeFetchSourceContextParams(Consumer<FetchSourceContext> consumer, Map<String, String> expectedParams) {
    if (randomBoolean()) {
        if (randomBoolean()) {
            boolean fetchSource = randomBoolean();
            consumer.accept(new FetchSourceContext(fetchSource));
            if (fetchSource == false) {
                expectedParams.put("_source", "false");
            }
        } else {
            int numIncludes = randomIntBetween(0, 5);
            String[] includes = new String[numIncludes];
            String includesParam = randomFields(includes);
            if (numIncludes > 0) {
                expectedParams.put("_source_includes", includesParam);
            }
            int numExcludes = randomIntBetween(0, 5);
            String[] excludes = new String[numExcludes];
            String excludesParam = randomFields(excludes);
            if (numExcludes > 0) {
                expectedParams.put("_source_excludes", excludesParam);
            }
            consumer.accept(new FetchSourceContext(true, includes, excludes));
        }
    }
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext)

Example 2 with FetchSourceContext

use of org.opensearch.search.fetch.subphase.FetchSourceContext in project OpenSearch by opensearch-project.

the class CrudIT method testGet.

public void testGet() throws IOException {
    {
        GetRequest getRequest = new GetRequest("index", "id");
        OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage());
        assertEquals("index", exception.getMetadata("opensearch.index").get(0));
    }
    IndexRequest index = new IndexRequest("index").id("id");
    String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
    index.source(document, XContentType.JSON);
    index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    highLevelClient().index(index, RequestOptions.DEFAULT);
    {
        GetRequest getRequest = new GetRequest("index", "id").version(2);
        OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync));
        assertEquals(RestStatus.CONFLICT, exception.status());
        assertEquals("OpenSearch exception [type=version_conflict_engine_exception, " + "reason=[id]: " + "version conflict, current version [1] is different than the one provided [2]]", exception.getMessage());
        assertEquals("index", exception.getMetadata("opensearch.index").get(0));
    }
    {
        GetRequest getRequest = new GetRequest("index", "id");
        if (randomBoolean()) {
            getRequest.version(1L);
        }
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertFalse(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        assertEquals(document, getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "does_not_exist");
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("does_not_exist", getResponse.getId());
        assertFalse(getResponse.isExists());
        assertEquals(-1, getResponse.getVersion());
        assertTrue(getResponse.isSourceEmpty());
        assertNull(getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "id");
        getRequest.fetchSourceContext(new FetchSourceContext(false, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY));
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertTrue(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        assertNull(getResponse.getSourceAsString());
    }
    {
        GetRequest getRequest = new GetRequest("index", "id");
        if (randomBoolean()) {
            getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY));
        } else {
            getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field2" }));
        }
        GetResponse getResponse = execute(getRequest, highLevelClient()::get, highLevelClient()::getAsync);
        assertEquals("index", getResponse.getIndex());
        assertEquals("id", getResponse.getId());
        assertTrue(getResponse.isExists());
        assertFalse(getResponse.isSourceEmpty());
        assertEquals(1L, getResponse.getVersion());
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        assertEquals(1, sourceAsMap.size());
        assertEquals("value1", sourceAsMap.get("field1"));
    }
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) GetRequest(org.opensearch.action.get.GetRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) OpenSearchException(org.opensearch.OpenSearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) GetResponse(org.opensearch.action.get.GetResponse) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 3 with FetchSourceContext

use of org.opensearch.search.fetch.subphase.FetchSourceContext in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testUpdate.

@SuppressWarnings("unused")
public void testUpdate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        IndexRequest indexRequest = new IndexRequest("posts").id("1").source("field", 0);
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        assertSame(RestStatus.CREATED, indexResponse.status());
        Request request = new Request("POST", "/_scripts/increment-field");
        request.setJsonEntity(Strings.toString(JsonXContent.contentBuilder().startObject().startObject("script").field("lang", "painless").field("source", "ctx._source.field += params.count").endObject().endObject()));
        Response response = client().performRequest(request);
        assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode());
    }
    {
        // tag::update-request
        UpdateRequest request = new UpdateRequest(// <1>
        "posts", // <2>
        "1");
        // end::update-request
        request.fetchSource(true);
        // tag::update-request-with-inline-script
        // <1>
        Map<String, Object> parameters = singletonMap("count", 4);
        Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.field += params.count", // <2>
        parameters);
        // <3>
        request.script(inline);
        // end::update-request-with-inline-script
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        assertEquals(4, updateResponse.getGetResult().getSource().get("field"));
        request = new UpdateRequest("posts", "1").fetchSource(true);
        // tag::update-request-with-stored-script
        Script stored = new Script(ScriptType.STORED, null, "increment-field", // <1>
        parameters);
        // <2>
        request.script(stored);
        // end::update-request-with-stored-script
        updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        assertEquals(8, updateResponse.getGetResult().getSource().get("field"));
    }
    {
        // tag::update-request-with-doc-as-map
        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("updated", new Date());
        jsonMap.put("reason", "daily update");
        UpdateRequest request = new UpdateRequest("posts", "1").doc(// <1>
        jsonMap);
        // end::update-request-with-doc-as-map
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    }
    {
        // tag::update-request-with-doc-as-xcontent
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.timeField("updated", new Date());
            builder.field("reason", "daily update");
        }
        builder.endObject();
        UpdateRequest request = new UpdateRequest("posts", "1").doc(// <1>
        builder);
        // end::update-request-with-doc-as-xcontent
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    }
    {
        // tag::update-request-shortcut
        UpdateRequest request = new UpdateRequest("posts", "1").doc("updated", new Date(), "reason", // <1>
        "daily update");
        // end::update-request-shortcut
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
    }
    {
        // tag::update-request-with-doc-as-string
        UpdateRequest request = new UpdateRequest("posts", "1");
        String jsonString = "{" + "\"updated\":\"2017-01-01\"," + "\"reason\":\"daily update\"" + "}";
        // <1>
        request.doc(jsonString, XContentType.JSON);
        // end::update-request-with-doc-as-string
        request.fetchSource(true);
        // tag::update-execute
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        // end::update-execute
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        // tag::update-response
        String index = updateResponse.getIndex();
        String id = updateResponse.getId();
        long version = updateResponse.getVersion();
        if (updateResponse.getResult() == DocWriteResponse.Result.CREATED) {
        // <1>
        } else if (updateResponse.getResult() == DocWriteResponse.Result.UPDATED) {
        // <2>
        } else if (updateResponse.getResult() == DocWriteResponse.Result.DELETED) {
        // <3>
        } else if (updateResponse.getResult() == DocWriteResponse.Result.NOOP) {
        // <4>
        }
        // end::update-response
        // tag::update-getresult
        // <1>
        GetResult result = updateResponse.getGetResult();
        if (result.isExists()) {
            // <2>
            String sourceAsString = result.sourceAsString();
            // <3>
            Map<String, Object> sourceAsMap = result.sourceAsMap();
            // <4>
            byte[] sourceAsBytes = result.source();
        } else {
        // <5>
        }
        // end::update-getresult
        assertNotNull(result);
        assertEquals(3, result.sourceAsMap().size());
        // tag::update-failure
        ReplicationResponse.ShardInfo shardInfo = updateResponse.getShardInfo();
        if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
        // <1>
        }
        if (shardInfo.getFailed() > 0) {
            for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                // <2>
                String reason = failure.reason();
            }
        }
    // end::update-failure
    }
    {
        // tag::update-docnotfound
        UpdateRequest request = new UpdateRequest("posts", "does_not_exist").doc("field", "value");
        try {
            UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        } catch (OpenSearchException e) {
            if (e.status() == RestStatus.NOT_FOUND) {
            // <1>
            }
        }
    // end::update-docnotfound
    }
    {
        // tag::update-conflict
        UpdateRequest request = new UpdateRequest("posts", "1").doc("field", "value").setIfSeqNo(101L).setIfPrimaryTerm(200L);
        try {
            UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        } catch (OpenSearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
            // <1>
            }
        }
    // end::update-conflict
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "1").doc("reason", "no source");
        // tag::update-request-no-source
        // <1>
        request.fetchSource(true);
        // end::update-request-no-source
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        assertNotNull(updateResponse.getGetResult());
        assertEquals(3, updateResponse.getGetResult().sourceAsMap().size());
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "1").doc("reason", "source includes");
        // tag::update-request-source-include
        String[] includes = new String[] { "updated", "r*" };
        String[] excludes = Strings.EMPTY_ARRAY;
        request.fetchSource(// <1>
        new FetchSourceContext(true, includes, excludes));
        // end::update-request-source-include
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        Map<String, Object> sourceAsMap = updateResponse.getGetResult().sourceAsMap();
        assertEquals(2, sourceAsMap.size());
        assertEquals("source includes", sourceAsMap.get("reason"));
        assertTrue(sourceAsMap.containsKey("updated"));
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "1").doc("reason", "source excludes");
        // tag::update-request-source-exclude
        String[] includes = Strings.EMPTY_ARRAY;
        String[] excludes = new String[] { "updated" };
        request.fetchSource(// <1>
        new FetchSourceContext(true, includes, excludes));
        // end::update-request-source-exclude
        UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.UPDATED, updateResponse.getResult());
        Map<String, Object> sourceAsMap = updateResponse.getGetResult().sourceAsMap();
        assertEquals(2, sourceAsMap.size());
        assertEquals("source excludes", sourceAsMap.get("reason"));
        assertTrue(sourceAsMap.containsKey("field"));
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "id");
        // tag::update-request-routing
        // <1>
        request.routing("routing");
        // end::update-request-routing
        // tag::update-request-timeout
        // <1>
        request.timeout(TimeValue.timeValueSeconds(1));
        // <2>
        request.timeout("1s");
        // end::update-request-timeout
        // tag::update-request-retry
        // <1>
        request.retryOnConflict(3);
        // end::update-request-retry
        // tag::update-request-refresh
        // <1>
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        // <2>
        request.setRefreshPolicy("wait_for");
        // end::update-request-refresh
        // tag::update-request-cas
        // <1>
        request.setIfSeqNo(2L);
        // <2>
        request.setIfPrimaryTerm(1L);
        // end::update-request-cas
        // tag::update-request-detect-noop
        // <1>
        request.detectNoop(false);
        // end::update-request-detect-noop
        // tag::update-request-upsert
        String jsonString = "{\"created\":\"2017-01-01\"}";
        // <1>
        request.upsert(jsonString, XContentType.JSON);
        // end::update-request-upsert
        // tag::update-request-scripted-upsert
        // <1>
        request.scriptedUpsert(true);
        // end::update-request-scripted-upsert
        // tag::update-request-doc-upsert
        // <1>
        request.docAsUpsert(true);
        // end::update-request-doc-upsert
        // tag::update-request-active-shards
        // <1>
        request.waitForActiveShards(2);
        // <2>
        request.waitForActiveShards(ActiveShardCount.ALL);
    // end::update-request-active-shards
    }
    {
        UpdateRequest request = new UpdateRequest("posts", "async").doc("reason", "async update").docAsUpsert(true);
        ActionListener<UpdateResponse> listener;
        // tag::update-execute-listener
        listener = new ActionListener<UpdateResponse>() {

            @Override
            public void onResponse(UpdateResponse updateResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::update-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::update-execute-async
        // <1>
        client.updateAsync(request, RequestOptions.DEFAULT, listener);
        // end::update-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : Script(org.opensearch.script.Script) GetResult(org.opensearch.index.get.GetResult) UpdateRequest(org.opensearch.action.update.UpdateRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) OpenSearchException(org.opensearch.OpenSearchException) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) OpenSearchException(org.opensearch.OpenSearchException) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 4 with FetchSourceContext

use of org.opensearch.search.fetch.subphase.FetchSourceContext in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testExplain.

public void testExplain() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    // tag::explain-request
    ExplainRequest request = new ExplainRequest("contributors", "1");
    request.query(QueryBuilders.termQuery("user", "quuz"));
    // end::explain-request
    // tag::explain-request-routing
    // <1>
    request.routing("routing");
    // end::explain-request-routing
    // tag::explain-request-preference
    // <1>
    request.preference("_local");
    // end::explain-request-preference
    // tag::explain-request-source
    // <1>
    request.fetchSourceContext(new FetchSourceContext(true, new String[] { "user" }, null));
    // end::explain-request-source
    // tag::explain-request-stored-field
    // <1>
    request.storedFields(new String[] { "user" });
    // end::explain-request-stored-field
    // tag::explain-execute
    ExplainResponse response = client.explain(request, RequestOptions.DEFAULT);
    // end::explain-execute
    // tag::explain-response
    // <1>
    String index = response.getIndex();
    // <2>
    String id = response.getId();
    // <3>
    boolean exists = response.isExists();
    // <4>
    boolean match = response.isMatch();
    // <5>
    boolean hasExplanation = response.hasExplanation();
    // <6>
    Explanation explanation = response.getExplanation();
    // <7>
    GetResult getResult = response.getGetResult();
    // end::explain-response
    assertThat(index, equalTo("contributors"));
    assertThat(id, equalTo("1"));
    assertTrue(exists);
    assertTrue(match);
    assertTrue(hasExplanation);
    assertNotNull(explanation);
    assertNotNull(getResult);
    // tag::get-result
    // <1>
    Map<String, Object> source = getResult.getSource();
    // <2>
    Map<String, DocumentField> fields = getResult.getFields();
    // end::get-result
    assertThat(source, equalTo(Collections.singletonMap("user", "quuz")));
    assertThat(fields.get("user").getValue(), equalTo("quuz"));
    // tag::explain-execute-listener
    ActionListener<ExplainResponse> listener = new ActionListener<ExplainResponse>() {

        @Override
        public void onResponse(ExplainResponse explainResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::explain-execute-listener
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::explain-execute-async
    // <1>
    client.explainAsync(request, RequestOptions.DEFAULT, listener);
    // end::explain-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : GetResult(org.opensearch.index.get.GetResult) DocumentField(org.opensearch.common.document.DocumentField) Explanation(org.apache.lucene.search.Explanation) ExplainResponse(org.opensearch.action.explain.ExplainResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) ExplainRequest(org.opensearch.action.explain.ExplainRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Example 5 with FetchSourceContext

use of org.opensearch.search.fetch.subphase.FetchSourceContext in project OpenSearch by opensearch-project.

the class MetadataFetchingIT method testInnerHits.

public void testInnerHits() {
    assertAcked(prepareCreate("test").addMapping("_doc", "nested", "type=nested"));
    ensureGreen();
    client().prepareIndex("test").setId("1").setSource("field", "value", "nested", Collections.singletonMap("title", "foo")).get();
    refresh();
    SearchResponse response = client().prepareSearch("test").storedFields("_none_").setFetchSource(false).setQuery(new NestedQueryBuilder("nested", new TermQueryBuilder("nested.title", "foo"), ScoreMode.Total).innerHit(new InnerHitBuilder().setStoredFieldNames(Collections.singletonList("_none_")).setFetchSourceContext(new FetchSourceContext(false)))).get();
    assertThat(response.getHits().getTotalHits().value, equalTo(1L));
    assertThat(response.getHits().getAt(0).getId(), nullValue());
    assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue());
    assertThat(response.getHits().getAt(0).getInnerHits().size(), equalTo(1));
    SearchHits hits = response.getHits().getAt(0).getInnerHits().get("nested");
    assertThat(hits.getTotalHits().value, equalTo(1L));
    assertThat(hits.getAt(0).getId(), nullValue());
    assertThat(hits.getAt(0).getSourceAsString(), nullValue());
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) SearchHits(org.opensearch.search.SearchHits) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)37 Matchers.containsString (org.hamcrest.Matchers.containsString)10 HashMap (java.util.HashMap)8 Map (java.util.Map)7 OpenSearchException (org.opensearch.OpenSearchException)7 IndexRequest (org.opensearch.action.index.IndexRequest)7 IOException (java.io.IOException)6 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 List (java.util.List)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ActionListener (org.opensearch.action.ActionListener)5 DocWriteRequest (org.opensearch.action.DocWriteRequest)5 LatchedActionListener (org.opensearch.action.LatchedActionListener)5 GetRequest (org.opensearch.action.get.GetRequest)5 MultiGetResponse (org.opensearch.action.get.MultiGetResponse)5 UpdateRequest (org.opensearch.action.update.UpdateRequest)5 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)5 XContentParser (org.opensearch.common.xcontent.XContentParser)5 ArrayList (java.util.ArrayList)4