use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
the class SimpleMgetIT method testThatMgetShouldWorkWithOneIndexMissing.
public void testThatMgetShouldWorkWithOneIndexMissing() throws IOException {
createIndex("test");
client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).setRefreshPolicy(IMMEDIATE).get();
MultiGetResponse mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item("test", "test", "1")).add(new MultiGetRequest.Item("nonExistingIndex", "test", "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"));
assertThat(((ElasticsearchException) mgetResponse.getResponses()[1].getFailure().getFailure()).getIndex().getName(), is("nonExistingIndex"));
mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item("nonExistingIndex", "test", "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"));
assertThat(((ElasticsearchException) mgetResponse.getResponses()[0].getFailure().getFailure()).getIndex().getName(), is("nonExistingIndex"));
}
use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
the class SimpleMgetIT method testThatMgetShouldWorkWithMultiIndexAlias.
public void testThatMgetShouldWorkWithMultiIndexAlias() throws IOException {
assertAcked(prepareCreate("test").addAlias(new Alias("multiIndexAlias")));
assertAcked(prepareCreate("test2").addAlias(new Alias("multiIndexAlias")));
client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().field("foo", "bar").endObject()).setRefreshPolicy(IMMEDIATE).get();
MultiGetResponse mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item("test", "test", "1")).add(new MultiGetRequest.Item("multiIndexAlias", "test", "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("multiIndexAlias"));
assertThat(mgetResponse.getResponses()[1].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[1].getFailure().getMessage(), containsString("more than one indices"));
mgetResponse = client().prepareMultiGet().add(new MultiGetRequest.Item("multiIndexAlias", "test", "1")).get();
assertThat(mgetResponse.getResponses().length, is(1));
assertThat(mgetResponse.getResponses()[0].getIndex(), is("multiIndexAlias"));
assertThat(mgetResponse.getResponses()[0].isFailed(), is(true));
assertThat(mgetResponse.getResponses()[0].getFailure().getMessage(), containsString("more than one indices"));
}
use of org.elasticsearch.action.get.MultiGetResponse in project elasticsearch by elastic.
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, "type", "1").setSource("{}", XContentType.JSON).get();
client().prepareIndex(dateMathExp2, "type", "2").setSource("{}", XContentType.JSON).get();
client().prepareIndex(dateMathExp3, "type", "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, "type", "1").get();
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("1"));
getResponse = client().prepareGet(dateMathExp2, "type", "2").get();
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("2"));
getResponse = client().prepareGet(dateMathExp3, "type", "3").get();
assertThat(getResponse.isExists(), is(true));
assertThat(getResponse.getId(), equalTo("3"));
MultiGetResponse mgetResponse = client().prepareMultiGet().add(dateMathExp1, "type", "1").add(dateMathExp2, "type", "2").add(dateMathExp3, "type", "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, "type", "1").get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
assertThat(deleteResponse.getId(), equalTo("1"));
deleteResponse = client().prepareDelete(dateMathExp2, "type", "2").get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
assertThat(deleteResponse.getId(), equalTo("2"));
deleteResponse = client().prepareDelete(dateMathExp3, "type", "3").get();
assertEquals(DocWriteResponse.Result.DELETED, deleteResponse.getResult());
assertThat(deleteResponse.getId(), equalTo("3"));
}
use of org.elasticsearch.action.get.MultiGetResponse in project camel by apache.
the class ElasticsearchGetSearchDeleteExistsUpdateTest method testMultiGet.
@Test
public void testMultiGet() throws Exception {
//first, INDEX two values
Map<String, String> map = createIndexedData();
Map<String, Object> headers = new HashMap<String, Object>();
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX);
headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "twitter");
headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "tweet");
headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "1");
template.requestBodyAndHeaders("direct:start", map, headers, String.class);
headers.clear();
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_INDEX);
headers.put(ElasticsearchConstants.PARAM_INDEX_NAME, "facebook");
headers.put(ElasticsearchConstants.PARAM_INDEX_TYPE, "status");
headers.put(ElasticsearchConstants.PARAM_INDEX_ID, "2");
template.requestBodyAndHeaders("direct:start", map, headers, String.class);
headers.clear();
//now, verify MULTIGET
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTIGET);
Item item1 = new Item("twitter", "tweet", "1");
Item item2 = new Item("facebook", "status", "2");
Item item3 = new Item("instagram", "latest", "3");
List<Item> list = new ArrayList<Item>();
list.add(item1);
list.add(item2);
list.add(item3);
MultiGetResponse response = template.requestBodyAndHeaders("direct:start", list, headers, MultiGetResponse.class);
MultiGetItemResponse[] responses = response.getResponses();
assertNotNull("response should not be null", response);
assertEquals("response should contains three multiGetResponse object", 3, response.getResponses().length);
assertEquals("response 1 should contains tweet as type", "tweet", responses[0].getResponse().getType().toString());
assertEquals("response 2 should contains status as type", "status", responses[1].getResponse().getType().toString());
assertFalse("response 1 should be ok", responses[0].isFailed());
assertFalse("response 2 should be ok", responses[1].isFailed());
assertTrue("response 3 should be failed", responses[2].isFailed());
}
use of org.elasticsearch.action.get.MultiGetResponse in project incubator-skywalking by apache.
the class MemoryMetricEsUIDAO method getMemoryTrend.
private Trend getMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints, boolean isHeap) {
String tableName = TimePyramidTableNameBuilder.build(step, MemoryMetricTable.TABLE);
MultiGetRequestBuilder prepareMultiGet = getClient().prepareMultiGet(durationPoints, new ElasticSearchClient.MultiGetRowHandler<DurationPoint>() {
@Override
public void accept(DurationPoint durationPoint) {
String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + BooleanUtils.booleanToValue(isHeap);
add(tableName, MemoryMetricTable.TABLE_TYPE, id);
}
});
Trend trend = new Trend();
MultiGetResponse multiGetResponse = prepareMultiGet.get();
for (MultiGetItemResponse response : multiGetResponse.getResponses()) {
if (response.getResponse().isExists()) {
long max = ((Number) response.getResponse().getSource().get(MemoryMetricTable.COLUMN_MAX)).longValue();
long used = ((Number) response.getResponse().getSource().get(MemoryMetricTable.COLUMN_USED)).longValue();
long times = ((Number) response.getResponse().getSource().get(MemoryMetricTable.COLUMN_TIMES)).longValue();
trend.getMetrics().add((int) (used / times));
if (max < 0) {
trend.getMaxMetrics().add((int) (used / times));
} else {
trend.getMaxMetrics().add((int) (max / times));
}
} else {
trend.getMetrics().add(0);
trend.getMaxMetrics().add(0);
}
}
return trend;
}
Aggregations