use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.
the class ConcurrentDynamicTemplateIT method testConcurrentDynamicMapping.
// see #3544
public void testConcurrentDynamicMapping() throws Exception {
final String fieldName = "field";
final String mapping = "{ \"" + mappingType + "\": {" + "\"dynamic_templates\": [" + "{ \"" + fieldName + "\": {" + "\"path_match\": \"*\"," + "\"mapping\": {" + "\"type\": \"text\"," + "\"store\": true," + "\"analyzer\": \"whitespace\" } } } ] } }";
// The 'fieldNames' array is used to help with retrieval of index terms
// after testing
int iters = scaledRandomIntBetween(5, 15);
for (int i = 0; i < iters; i++) {
cluster().wipeIndices("test");
assertAcked(prepareCreate("test").addMapping(mappingType, mapping, XContentType.JSON));
int numDocs = scaledRandomIntBetween(10, 100);
final CountDownLatch latch = new CountDownLatch(numDocs);
final List<Throwable> throwable = new CopyOnWriteArrayList<>();
int currentID = 0;
for (int j = 0; j < numDocs; j++) {
Map<String, Object> source = new HashMap<>();
source.put(fieldName, "test-user");
client().prepareIndex("test", mappingType, Integer.toString(currentID++)).setSource(source).execute(new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse response) {
latch.countDown();
}
@Override
public void onFailure(Exception e) {
throwable.add(e);
latch.countDown();
}
});
}
latch.await();
assertThat(throwable, emptyIterable());
refresh();
assertHitCount(client().prepareSearch("test").setQuery(QueryBuilders.matchQuery(fieldName, "test-user")).get(), numDocs);
assertHitCount(client().prepareSearch("test").setQuery(QueryBuilders.matchQuery(fieldName, "test user")).get(), 0);
}
}
use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.
the class SearchWithRandomExceptionsIT method testRandomExceptions.
public void testRandomExceptions() throws IOException, InterruptedException, ExecutionException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("test").field("type", "keyword").endObject().endObject().endObject().endObject().string();
final double lowLevelRate;
final double topLevelRate;
if (frequently()) {
if (randomBoolean()) {
if (randomBoolean()) {
lowLevelRate = 1.0 / between(2, 10);
topLevelRate = 0.0d;
} else {
topLevelRate = 1.0 / between(2, 10);
lowLevelRate = 0.0d;
}
} else {
lowLevelRate = 1.0 / between(2, 10);
topLevelRate = 1.0 / between(2, 10);
}
} else {
// rarely no exception
topLevelRate = 0d;
lowLevelRate = 0d;
}
Builder settings = Settings.builder().put(indexSettings()).put(EXCEPTION_TOP_LEVEL_RATIO_KEY, topLevelRate).put(EXCEPTION_LOW_LEVEL_RATIO_KEY, lowLevelRate).put(MockEngineSupport.WRAP_READER_RATIO.getKey(), 1.0d);
logger.info("creating index: [test] using settings: [{}]", settings.build().getAsMap());
assertAcked(prepareCreate("test").setSettings(settings).addMapping("type", mapping, XContentType.JSON));
ensureSearchable();
final int numDocs = between(10, 100);
int numCreated = 0;
boolean[] added = new boolean[numDocs];
for (int i = 0; i < numDocs; i++) {
try {
IndexResponse indexResponse = client().prepareIndex("test", "type", "" + i).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test", English.intToEnglish(i)).get();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
numCreated++;
added[i] = true;
}
} catch (ElasticsearchException ex) {
}
}
logger.info("Start Refresh");
// don't assert on failures here
RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().get();
final boolean refreshFailed = refreshResponse.getShardFailures().length != 0 || refreshResponse.getFailedShards() != 0;
logger.info("Refresh failed [{}] numShardsFailed: [{}], shardFailuresLength: [{}], successfulShards: [{}], totalShards: [{}] ", refreshFailed, refreshResponse.getFailedShards(), refreshResponse.getShardFailures().length, refreshResponse.getSuccessfulShards(), refreshResponse.getTotalShards());
NumShards test = getNumShards("test");
final int numSearches = scaledRandomIntBetween(100, 200);
// we don't check anything here really just making sure we don't leave any open files or a broken index behind.
for (int i = 0; i < numSearches; i++) {
try {
int docToQuery = between(0, numDocs - 1);
int expectedResults = added[docToQuery] ? 1 : 0;
logger.info("Searching for [test:{}]", English.intToEnglish(docToQuery));
SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("test", English.intToEnglish(docToQuery))).setSize(expectedResults).get();
logger.info("Successful shards: [{}] numShards: [{}]", searchResponse.getSuccessfulShards(), test.numPrimaries);
if (searchResponse.getSuccessfulShards() == test.numPrimaries && !refreshFailed) {
assertResultsAndLogOnFailure(expectedResults, searchResponse);
}
// check match all
searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchAllQuery()).setSize(numCreated).addSort("_id", SortOrder.ASC).get();
logger.info("Match all Successful shards: [{}] numShards: [{}]", searchResponse.getSuccessfulShards(), test.numPrimaries);
if (searchResponse.getSuccessfulShards() == test.numPrimaries && !refreshFailed) {
assertResultsAndLogOnFailure(numCreated, searchResponse);
}
} catch (SearchPhaseExecutionException ex) {
logger.info("expected SearchPhaseException: [{}]", ex.getMessage());
}
}
}
use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.
the class TaskResultsService method doStoreResult.
private void doStoreResult(TaskResult taskResult, ActionListener<Void> listener) {
IndexRequestBuilder index = client.prepareIndex(TASK_INDEX, TASK_TYPE, taskResult.getTask().getTaskId().toString());
try (XContentBuilder builder = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE)) {
taskResult.toXContent(builder, ToXContent.EMPTY_PARAMS);
index.setSource(builder);
} catch (IOException e) {
throw new ElasticsearchException("Couldn't convert task result to XContent for [{}]", e, taskResult.getTask());
}
index.execute(new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
listener.onResponse(null);
}
@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
});
}
use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.
the class IndexAliasesIT method testAliases.
public void testAliases() throws Exception {
logger.info("--> creating index [test]");
createIndex("test");
ensureGreen();
logger.info("--> aliasing index [test] with [alias1]");
assertAcked(admin().indices().prepareAliases().addAlias("test", "alias1"));
logger.info("--> indexing against [alias1], should work now");
IndexResponse indexResponse = client().index(indexRequest("alias1").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
assertThat(indexResponse.getIndex(), equalTo("test"));
logger.info("--> creating index [test_x]");
createIndex("test_x");
ensureGreen();
logger.info("--> remove [alias1], Aliasing index [test_x] with [alias1]");
assertAcked(admin().indices().prepareAliases().removeAlias("test", "alias1").addAlias("test_x", "alias1"));
logger.info("--> indexing against [alias1], should work against [test_x]");
indexResponse = client().index(indexRequest("alias1").type("type1").id("1").source(source("1", "test"), XContentType.JSON)).actionGet();
assertThat(indexResponse.getIndex(), equalTo("test_x"));
}
use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.
the class ListenerActionIT method testThreadedListeners.
public void testThreadedListeners() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<Throwable> failure = new AtomicReference<>();
final AtomicReference<String> threadName = new AtomicReference<>();
Client client = client();
IndexRequest request = new IndexRequest("test", "type", "1");
if (randomBoolean()) {
// set the source, without it, we will have a verification failure
request.source(Requests.INDEX_CONTENT_TYPE, "field1", "value1");
}
client.index(request, new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
threadName.set(Thread.currentThread().getName());
latch.countDown();
}
@Override
public void onFailure(Exception e) {
threadName.set(Thread.currentThread().getName());
failure.set(e);
latch.countDown();
}
});
latch.await();
boolean shouldBeThreaded = TransportClient.CLIENT_TYPE.equals(Client.CLIENT_TYPE_SETTING_S.get(client.settings()));
if (shouldBeThreaded) {
assertTrue(threadName.get().contains("listener"));
} else {
assertFalse(threadName.get().contains("listener"));
}
}
Aggregations