Search in sources :

Example 46 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project titan by thinkaurelius.

the class ElasticSearchIndex method restore.

public void restore(Map<String, Map<String, List<IndexEntry>>> documents, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
    BulkRequestBuilder bulk = client.prepareBulk();
    int requests = 0;
    try {
        for (Map.Entry<String, Map<String, List<IndexEntry>>> stores : documents.entrySet()) {
            String store = stores.getKey();
            for (Map.Entry<String, List<IndexEntry>> entry : stores.getValue().entrySet()) {
                String docID = entry.getKey();
                List<IndexEntry> content = entry.getValue();
                if (content == null || content.size() == 0) {
                    // delete
                    if (log.isTraceEnabled())
                        log.trace("Deleting entire document {}", docID);
                    bulk.add(new DeleteRequest(indexName, store, docID));
                    requests++;
                } else {
                    // Add
                    if (log.isTraceEnabled())
                        log.trace("Adding entire document {}", docID);
                    bulk.add(new IndexRequest(indexName, store, docID).source(getNewDocument(content, informations.get(store), IndexMutation.determineTTL(content))));
                    requests++;
                }
            }
        }
        if (requests > 0)
            bulk.execute().actionGet();
    } catch (Exception e) {
        throw convert(e);
    }
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) FileNotFoundException(java.io.FileNotFoundException) TitanException(com.thinkaurelius.titan.core.TitanException) IndexMissingException(org.elasticsearch.indices.IndexMissingException) IOException(java.io.IOException)

Example 47 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project camel by apache.

the class ElasticsearchBulkTest method bulkRequestBody.

@Test
public void bulkRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    BulkRequest request = new BulkRequest();
    request.add(new IndexRequest(prefix + "foo", prefix + "bar", prefix + "baz").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"));
    // when
    BulkResponse response = template.requestBody("direct:bulk", request, BulkResponse.class);
    // then
    assertThat(response, notNullValue());
    assertEquals(prefix + "baz", response.getItems()[0].getId());
}
Also used : BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) Test(org.junit.Test)

Example 48 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method deleteRequestBody.

@Test
public void deleteRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    DeleteRequest request = new DeleteRequest(prefix + "foo").type(prefix + "bar");
    // when
    String documentId = template.requestBody("direct:index", new IndexRequest("" + prefix + "foo", "" + prefix + "bar", "" + prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
    DeleteResponse response = template.requestBody("direct:delete", request.id(documentId), DeleteResponse.class);
    // then
    assertThat(response, notNullValue());
    assertThat(documentId, equalTo(response.getId()));
}
Also used : DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Test(org.junit.Test)

Example 49 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project camel by apache.

the class ElasticsearchGetSearchDeleteExistsUpdateTest method getRequestBody.

@Test
public void getRequestBody() throws Exception {
    String prefix = createPrefix();
    // given
    GetRequest request = new GetRequest(prefix + "foo").type(prefix + "bar");
    // when
    String documentId = template.requestBody("direct:index", new IndexRequest(prefix + "foo", prefix + "bar", prefix + "testId").source("{\"" + prefix + "content\": \"" + prefix + "hello\"}"), String.class);
    GetResponse response = template.requestBody("direct:get", request.id(documentId), GetResponse.class);
    // then
    assertThat(response, notNullValue());
    assertThat(prefix + "hello", equalTo(response.getSourceAsMap().get(prefix + "content")));
}
Also used : GetRequest(org.elasticsearch.action.get.GetRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) Test(org.junit.Test)

Example 50 with IndexRequest

use of org.elasticsearch.action.index.IndexRequest in project metron by apache.

the class ElasticsearchDao method buildIndexRequest.

protected IndexRequest buildIndexRequest(Document update, String sensorType, String indexName) {
    String type = sensorType + "_doc";
    Object ts = update.getTimestamp();
    IndexRequest indexRequest = new IndexRequest(indexName, type, update.getGuid()).source(update.getDocument());
    if (ts != null) {
        indexRequest = indexRequest.timestamp(ts.toString());
    }
    return indexRequest;
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)177 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)36 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)34 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)33 IOException (java.io.IOException)28 Test (org.junit.Test)27 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)26 ElasticsearchException (org.elasticsearch.ElasticsearchException)20 IndexResponse (org.elasticsearch.action.index.IndexResponse)17 HashMap (java.util.HashMap)16 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)15 Map (java.util.Map)14 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)13 GetRequest (org.elasticsearch.action.get.GetRequest)13 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)10 Matchers.anyBoolean (org.mockito.Matchers.anyBoolean)9 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)8 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)8