Search in sources :

Example 41 with IndexResponse

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;
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 42 with IndexResponse

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);
    }
}
Also used : HashMap(java.util.HashMap) IndexResponse(org.elasticsearch.action.index.IndexResponse) IOException(java.io.IOException) File(java.io.File) SpringBaseTest(org.n52.iceland.statistics.basetests.SpringBaseTest) Test(org.junit.Test)

Example 43 with IndexResponse

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();
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest)

Example 44 with IndexResponse

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();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) HashMap(java.util.HashMap) IndexResponse(org.elasticsearch.action.index.IndexResponse) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress) Settings(org.elasticsearch.common.settings.Settings) Date(java.util.Date) Test(org.junit.Test)

Example 45 with IndexResponse

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);
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) DeleteIndexRequestBuilder(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder) IndexException(org.molgenis.data.index.exception.IndexException) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) UnknownIndexException(org.molgenis.data.index.exception.UnknownIndexException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ResourceNotFoundException(org.elasticsearch.ResourceNotFoundException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)108 Test (org.junit.Test)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 IOException (java.io.IOException)17 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)17 IndexRequest (org.elasticsearch.action.index.IndexRequest)16 HashMap (java.util.HashMap)15 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)14 ElasticsearchException (org.elasticsearch.ElasticsearchException)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)10 Settings (org.elasticsearch.common.settings.Settings)9 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)7 GetResponse (org.elasticsearch.action.get.GetResponse)7 ArrayList (java.util.ArrayList)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)6 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)6 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)6 UpdateResponse (org.elasticsearch.action.update.UpdateResponse)6