use of org.opensearch.action.get.MultiGetResponse in project OpenSearch by opensearch-project.
the class CrudIT method testMultiGet.
public void testMultiGet() throws IOException {
{
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add("index", "id1");
multiGetRequest.add("index", "id2");
MultiGetResponse response = execute(multiGetRequest, highLevelClient()::mget, highLevelClient()::mgetAsync);
assertEquals(2, response.getResponses().length);
assertTrue(response.getResponses()[0].isFailed());
assertNull(response.getResponses()[0].getResponse());
assertEquals("id1", response.getResponses()[0].getFailure().getId());
assertEquals("index", response.getResponses()[0].getFailure().getIndex());
assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", response.getResponses()[0].getFailure().getFailure().getMessage());
assertTrue(response.getResponses()[1].isFailed());
assertNull(response.getResponses()[1].getResponse());
assertEquals("id2", response.getResponses()[1].getId());
assertEquals("index", response.getResponses()[1].getIndex());
assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", response.getResponses()[1].getFailure().getFailure().getMessage());
}
BulkRequest bulk = new BulkRequest();
bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
IndexRequest index = new IndexRequest("index").id("id1");
index.source("{\"field\":\"value1\"}", XContentType.JSON);
bulk.add(index);
index = new IndexRequest("index").id("id2");
index.source("{\"field\":\"value2\"}", XContentType.JSON);
bulk.add(index);
highLevelClient().bulk(bulk, RequestOptions.DEFAULT);
{
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add("index", "id1");
multiGetRequest.add("index", "id2");
MultiGetResponse response = execute(multiGetRequest, highLevelClient()::mget, highLevelClient()::mgetAsync);
assertEquals(2, response.getResponses().length);
assertFalse(response.getResponses()[0].isFailed());
assertNull(response.getResponses()[0].getFailure());
assertEquals("id1", response.getResponses()[0].getId());
assertEquals("index", response.getResponses()[0].getIndex());
assertEquals(Collections.singletonMap("field", "value1"), response.getResponses()[0].getResponse().getSource());
assertFalse(response.getResponses()[1].isFailed());
assertNull(response.getResponses()[1].getFailure());
assertEquals("id2", response.getResponses()[1].getId());
assertEquals("index", response.getResponses()[1].getIndex());
assertEquals(Collections.singletonMap("field", "value2"), response.getResponses()[1].getResponse().getSource());
}
}
use of org.opensearch.action.get.MultiGetResponse in project OpenSearch by opensearch-project.
the class GetActionIT method testMultiGetWithVersion.
public void testMultiGetWithVersion() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
ensureGreen();
MultiGetResponse response = client().prepareMultiGet().add(indexOrAlias(), "1").get();
assertThat(response.getResponses().length, equalTo(1));
assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(false));
for (int i = 0; i < 3; i++) {
client().prepareIndex("test").setId(Integer.toString(i)).setSource("field", "value" + i).get();
}
// Version from translog
response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "1").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "1").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "1").version(2)).get();
assertThat(response.getResponses().length, equalTo(3));
// [0] version doesn't matter, which is the default
assertThat(response.getResponses()[0].getFailure(), nullValue());
assertThat(response.getResponses()[0].getId(), equalTo("1"));
assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
assertThat(response.getResponses()[1].getId(), equalTo("1"));
assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
assertThat(response.getResponses()[1].getFailure(), nullValue());
assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[1].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
assertThat(response.getResponses()[2].getFailure(), notNullValue());
assertThat(response.getResponses()[2].getFailure().getId(), equalTo("1"));
assertThat(response.getResponses()[2].getFailure().getMessage(), startsWith("[1]: version conflict"));
assertThat(response.getResponses()[2].getFailure().getFailure(), instanceOf(VersionConflictEngineException.class));
// Version from Lucene index
refresh();
response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "1").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "1").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "1").version(2)).setRealtime(false).get();
assertThat(response.getResponses().length, equalTo(3));
// [0] version doesn't matter, which is the default
assertThat(response.getResponses()[0].getFailure(), nullValue());
assertThat(response.getResponses()[0].getId(), equalTo("1"));
assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
assertThat(response.getResponses()[1].getId(), equalTo("1"));
assertThat(response.getResponses()[1].getFailure(), nullValue());
assertThat(response.getResponses()[1].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[1].getResponse().getSourceAsMap().get("field").toString(), equalTo("value1"));
assertThat(response.getResponses()[2].getFailure(), notNullValue());
assertThat(response.getResponses()[2].getFailure().getId(), equalTo("1"));
assertThat(response.getResponses()[2].getFailure().getMessage(), startsWith("[1]: version conflict"));
assertThat(response.getResponses()[2].getFailure().getFailure(), instanceOf(VersionConflictEngineException.class));
for (int i = 0; i < 3; i++) {
client().prepareIndex("test").setId(Integer.toString(i)).setSource("field", "value" + i).get();
}
// Version from translog
response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "2").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "2").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "2").version(2)).get();
assertThat(response.getResponses().length, equalTo(3));
// [0] version doesn't matter, which is the default
assertThat(response.getResponses()[0].getFailure(), nullValue());
assertThat(response.getResponses()[0].getId(), equalTo("2"));
assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
assertThat(response.getResponses()[1].getFailure(), notNullValue());
assertThat(response.getResponses()[1].getFailure().getId(), equalTo("2"));
assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
assertThat(response.getResponses()[1].getFailure().getMessage(), startsWith("[2]: version conflict"));
assertThat(response.getResponses()[2].getId(), equalTo("2"));
assertThat(response.getResponses()[2].getIndex(), equalTo("test"));
assertThat(response.getResponses()[2].getFailure(), nullValue());
assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
// Version from Lucene index
refresh();
response = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), "2").version(Versions.MATCH_ANY)).add(new MultiGetRequest.Item(indexOrAlias(), "2").version(1)).add(new MultiGetRequest.Item(indexOrAlias(), "2").version(2)).setRealtime(false).get();
assertThat(response.getResponses().length, equalTo(3));
// [0] version doesn't matter, which is the default
assertThat(response.getResponses()[0].getFailure(), nullValue());
assertThat(response.getResponses()[0].getId(), equalTo("2"));
assertThat(response.getResponses()[0].getIndex(), equalTo("test"));
assertThat(response.getResponses()[0].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[0].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
assertThat(response.getResponses()[1].getFailure(), notNullValue());
assertThat(response.getResponses()[1].getFailure().getId(), equalTo("2"));
assertThat(response.getResponses()[1].getIndex(), equalTo("test"));
assertThat(response.getResponses()[1].getFailure().getMessage(), startsWith("[2]: version conflict"));
assertThat(response.getResponses()[2].getId(), equalTo("2"));
assertThat(response.getResponses()[2].getIndex(), equalTo("test"));
assertThat(response.getResponses()[2].getFailure(), nullValue());
assertThat(response.getResponses()[2].getResponse().isExists(), equalTo(true));
assertThat(response.getResponses()[2].getResponse().getSourceAsMap().get("field").toString(), equalTo("value2"));
}
use of org.opensearch.action.get.MultiGetResponse in project OpenSearch by opensearch-project.
the class DateMathIndexExpressionsIntegrationIT method testIndexNameDateMathExpressions.
public void testIndexNameDateMathExpressions() {
DateTime now = new DateTime(DateTimeZone.UTC);
String index1 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now);
String index2 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(1));
String index3 = ".marvel-" + DateTimeFormat.forPattern("YYYY.MM.dd").print(now.minusDays(2));
createIndex(index1, index2, index3);
GetSettingsResponse getSettingsResponse = client().admin().indices().prepareGetSettings(index1, index2, index3).get();
assertEquals(index1, getSettingsResponse.getSetting(index1, IndexMetadata.SETTING_INDEX_PROVIDED_NAME));
assertEquals(index2, getSettingsResponse.getSetting(index2, IndexMetadata.SETTING_INDEX_PROVIDED_NAME));
assertEquals(index3, getSettingsResponse.getSetting(index3, IndexMetadata.SETTING_INDEX_PROVIDED_NAME));
String dateMathExp1 = "<.marvel-{now/d}>";
String dateMathExp2 = "<.marvel-{now/d-1d}>";
String dateMathExp3 = "<.marvel-{now/d-2d}>";
client().prepareIndex(dateMathExp1).setId("1").setSource("{}", XContentType.JSON).get();
client().prepareIndex(dateMathExp2).setId("2").setSource("{}", XContentType.JSON).get();
client().prepareIndex(dateMathExp3).setId("3").setSource("{}", XContentType.JSON).get();
refresh();
SearchResponse searchResponse = client().prepareSearch(dateMathExp1, dateMathExp2, dateMathExp3).get();
assertHitCount(searchResponse, 3);
assertSearchHits(searchResponse, "1", "2", "3");
GetResponse getResponse = client().prepareGet(dateMathExp1, "1").get();
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("1"));
getResponse = client().prepareGet(dateMathExp2, "2").get();
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("2"));
getResponse = client().prepareGet(dateMathExp3, "3").get();
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("3"));
MultiGetResponse mgetResponse = client().prepareMultiGet().add(dateMathExp1, "1").add(dateMathExp2, "2").add(dateMathExp3, "3").get();
assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[0].getResponse().getId(), equalTo("1"));
assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[1].getResponse().getId(), equalTo("2"));
assertThat(mgetResponse.getResponses()[2].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[2].getResponse().getId(), equalTo("3"));
IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(dateMathExp1, dateMathExp2, dateMathExp3).get();
assertThat(indicesStatsResponse.getIndex(index1), notNullValue());
assertThat(indicesStatsResponse.getIndex(index2), notNullValue());
assertThat(indicesStatsResponse.getIndex(index3), notNullValue());
DeleteResponse deleteResponse = client().prepareDelete(dateMathExp1, "1").get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
assertThat(deleteResponse.getId(), equalTo("1"));
deleteResponse = client().prepareDelete(dateMathExp2, "2").get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
assertThat(deleteResponse.getId(), equalTo("2"));
deleteResponse = client().prepareDelete(dateMathExp3, "3").get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
assertThat(deleteResponse.getId(), equalTo("3"));
}
use of org.opensearch.action.get.MultiGetResponse in project OpenSearch by opensearch-project.
the class SimpleMgetIT method testThatMgetShouldWorkWithOneIndexMissing.
public void testThatMgetShouldWorkWithOneIndexMissing() throws IOException {
createIndex("test");
client().prepareIndex("test").setId("1").setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).setRefreshPolicy(IMMEDIATE).get();
MultiGetResponse mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item("test", "1")).add(new MultiGetRequest.Item("nonExistingIndex", "1")).get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("test"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[1].getIndex(), is("nonExistingIndex"));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[1].getFailure().getMessage(), is("no such index [nonExistingIndex]"));
assertThat(((OpenSearchException) mgetResponse.getResponses()[1].getFailure().getFailure()).getIndex().getName(), is("nonExistingIndex"));
mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item("nonExistingIndex", "1")).get();
assertThat(mgetResponse.getResponses().length, is(1));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("nonExistingIndex"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[0].getFailure().getMessage(), is("no such index [nonExistingIndex]"));
assertThat(((OpenSearchException) mgetResponse.getResponses()[0].getFailure().getFailure()).getIndex().getName(), is("nonExistingIndex"));
}
use of org.opensearch.action.get.MultiGetResponse in project OpenSearch by opensearch-project.
the class SimpleMgetIT method testThatRoutingPerDocumentIsSupported.
public void testThatRoutingPerDocumentIsSupported() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put(indexSettings()).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(2, DEFAULT_MAX_NUM_SHARDS))));
final String id = routingKeyForShard("test", 0);
final String routingOtherShard = routingKeyForShard("test", 1);
client().prepareIndex("test").setId(id).setRefreshPolicy(IMMEDIATE).setRouting(routingOtherShard).setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).get();
MultiGetResponse mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item(indexOrAlias(), id).routing(routingOtherShard)).add(new MultiGetRequest.Item(indexOrAlias(), id)).get();
assertThat(mgetResponse.getResponses().length, is(2));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[0].getResponse().isExists(), is(true));
assertThat(mgetResponse.getResponses()[0].getResponse().getIndex(), is("test"));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(false));
assertThat(mgetResponse.getResponses()[1].getResponse().isExists(), is(false));
assertThat(mgetResponse.getResponses()[1].getResponse().getIndex(), is("test"));
}
Aggregations