use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project arctic-sea by 52North.
the class ElasticsearchDataHandler method persist.
@Override
public IndexResponse persist(Map<String, Object> dataMap) {
if (!settings.isLoggingEnabled()) {
return null;
}
if (adminHandler.getElasticsearchClient() == null) {
throw new NullPointerException("Client is not initialized. Data will not be persisted.");
}
dataMap.put(ServiceEventDataMapping.TIMESTAMP_FIELD.getName(), DateTime.now(DateTimeZone.UTC));
dataMap.put(ServiceEventDataMapping.UUID_FIELD.getName(), settings.getUuid());
logger.debug("Persisting {}", dataMap);
IndexResponse response = adminHandler.getElasticsearchClient().prepareIndex(settings.getIndexId(), settings.getTypeId()).setSource(dataMap).get();
return response;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project arctic-sea by 52North.
the class EmbeddedServerIT method connectEmbeddedMode.
@Test
public void connectEmbeddedMode() throws Exception {
settings.setNodeConnectionMode(ElasticsearchSettingsKeys.CONNECTION_MODE_EMBEDDED_SERVER);
adminHandler.init();
Map<String, Object> data = new HashMap<>();
data.put("test", "test-string");
IndexResponse idx = dataHandler.persist(data);
Thread.sleep(2000);
String ret = dataHandler.getClient().prepareGet(idx.getIndex(), idx.getType(), idx.getId()).get().getSourceAsString();
Assert.assertNotNull(ret);
adminHandler.destroy();
try {
FileUtils.deleteDirectory(new File("./elasticsearch"));
} catch (IOException e) {
logger.info(e.getMessage(), e);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project Zpider by zeroized.
the class ElasticClient method indexDoc.
public String indexDoc(Map<String, ?> doc) throws IOException {
IndexRequest indexRequest = new IndexRequest(index, type);
indexRequest.source(doc);
IndexResponse indexResponse = highLevelClient.index(indexRequest);
return indexResponse.getId();
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project cheetah by togethwy.
the class TestElasticsearch method test.
@Test
public void test() {
try {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
// IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
// .setSource(jsonBuilder()
// .startObject()
// .field("user", "kimchy")
// .field("postDate", new Date())
// .field("message", "trying out Elasticsearch")
// .endObject()
// )
// .get();
// System.out.println(response);
Map<String, Object> result = new HashMap<>();
result.put("user", "Mary6");
result.put("postDate", new Date());
result.put("massage", "this is Mary6");
IndexResponse response2 = client.prepareIndex("twitter", "tweet").setSource(result).get();
System.out.println(response2.status().name().equals("CREATED"));
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project molgenis by molgenis.
the class ClientFacade method index.
public void index(Index index, Document document) {
if (LOG.isTraceEnabled()) {
LOG.trace("Indexing doc with id '{}' in index '{}' ...", document.getId(), index.getName());
}
String indexName = index.getName();
String documentId = document.getId();
XContentBuilder source = document.getContent();
IndexRequestBuilder indexRequest = client.prepareIndex().setIndex(indexName).setType(indexName).setId(documentId).setSource(source);
IndexResponse indexResponse;
try {
indexResponse = indexRequest.get();
} catch (ResourceNotFoundException e) {
LOG.error("", e);
throw new UnknownIndexException(index.getName());
} catch (ElasticsearchException e) {
LOG.debug("", e);
throw new IndexException(format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName));
}
// TODO: Is it good enough if at least one shard succeeds? Shouldn't we at least log something if failures > 0?
if (indexResponse.getShardInfo().getSuccessful() == 0) {
LOG.error(Arrays.stream(indexResponse.getShardInfo().getFailures()).map(ReplicationResponse.ShardInfo.Failure::toString).collect(joining("\n")));
throw new IndexException(format("Error indexing doc with id '%s' in index '%s'.", documentId, indexName));
}
if (LOG.isDebugEnabled()) {
LOG.debug("Indexed doc with id '{}' in index '{}'.", documentId, indexName);
}
}
Aggregations