Search in sources :

Example 31 with IndexResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project nutch by apache.

the class TestElasticIndexWriter method setup.

@Before
public void setup() {
    conf = NutchConfiguration.create();
    conf.addResource("nutch-site-test.xml");
    bulkRequestSuccessful = false;
    clusterSaturated = false;
    curNumFailures = 0;
    maxNumFailures = 0;
    Settings settings = Settings.builder().build();
    ThreadPool threadPool = new ThreadPool(settings);
    // customize the ES client to simulate responses from an ES cluster
    client = new AbstractClient(settings, threadPool) {

        @Override
        public void close() {
        }

        @Override
        protected <Request extends ActionRequest, Response extends ActionResponse, RequestBuilder extends ActionRequestBuilder<Request, Response, RequestBuilder>> void doExecute(Action<Request, Response, RequestBuilder> action, Request request, ActionListener<Response> listener) {
            BulkResponse response = null;
            if (clusterSaturated) {
                // pretend the cluster is saturated
                curNumFailures++;
                if (curNumFailures >= maxNumFailures) {
                    // pretend the cluster is suddenly no longer saturated
                    clusterSaturated = false;
                }
                // respond with a failure
                BulkItemResponse failed = new BulkItemResponse(0, OpType.INDEX, new BulkItemResponse.Failure("nutch", "index", "failure0", new EsRejectedExecutionException("saturated")));
                response = new BulkResponse(new BulkItemResponse[] { failed }, 0);
            } else {
                // respond successfully
                BulkItemResponse success = new BulkItemResponse(0, OpType.INDEX, new IndexResponse(new ShardId("nutch", UUID.randomUUID().toString(), 0), "index", "index0", 0, true));
                response = new BulkResponse(new BulkItemResponse[] { success }, 0);
            }
            listener.onResponse((Response) response);
        }
    };
    // customize the plugin to signal successful bulk operations
    testIndexWriter = new ElasticIndexWriter() {

        @Override
        protected Client makeClient(Configuration conf) {
            return client;
        }

        @Override
        protected BulkProcessor.Listener bulkProcessorListener() {
            return new BulkProcessor.Listener() {

                @Override
                public void afterBulk(long executionId, BulkRequest request, BulkResponse response) {
                    if (!response.hasFailures()) {
                        bulkRequestSuccessful = true;
                    }
                }

                @Override
                public void afterBulk(long executionId, BulkRequest request, Throwable failure) {
                }

                @Override
                public void beforeBulk(long executionId, BulkRequest request) {
                }
            };
        }
    };
}
Also used : ActionListener(org.elasticsearch.action.ActionListener) ActionRequestBuilder(org.elasticsearch.action.ActionRequestBuilder) Configuration(org.apache.hadoop.conf.Configuration) NutchConfiguration(org.apache.nutch.util.NutchConfiguration) AbstractClient(org.elasticsearch.client.support.AbstractClient) ThreadPool(org.elasticsearch.threadpool.ThreadPool) ActionRequest(org.elasticsearch.action.ActionRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ActionResponse(org.elasticsearch.action.ActionResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ShardId(org.elasticsearch.index.shard.ShardId) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkProcessor(org.elasticsearch.action.bulk.BulkProcessor) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) Client(org.elasticsearch.client.Client) AbstractClient(org.elasticsearch.client.support.AbstractClient) Settings(org.elasticsearch.common.settings.Settings) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) Before(org.junit.Before)

Example 32 with IndexResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project pancm_project by xuwujing.

the class EsHighLevelRestTest1 method insert.

private static void insert() throws IOException {
    String index = "test1";
    String type = "_doc";
    // 唯一编号
    String id = "1";
    IndexRequest request = new IndexRequest(index, type, id);
    /*
		 * 第一种方式,通过jsonString进行创建
		 */
    // json
    String jsonString = "{" + "\"uid\":\"1234\"," + "\"phone\":\"12345678909\"," + "\"msgcode\":\"1\"," + "\"sendtime\":\"2019-03-14 01:57:04\"," + "\"message\":\"xuwujing study Elasticsearch\"" + "}";
    request.source(jsonString, XContentType.JSON);
    /*
		 * 第二种方式,通过map创建,,会自动转换成json的数据
		 */
    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("uid", 1234);
    jsonMap.put("phone", 12345678909L);
    jsonMap.put("msgcode", 1);
    jsonMap.put("sendtime", "2019-03-14 01:57:04");
    jsonMap.put("message", "xuwujing study Elasticsearch");
    request.source(jsonMap);
    /*
		 * 第三种方式 : 通过XContentBuilder对象进行创建
		 */
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.field("uid", 1234);
        builder.field("phone", 12345678909L);
        builder.field("msgcode", 1);
        builder.timeField("sendtime", "2019-03-14 01:57:04");
        builder.field("message", "xuwujing study Elasticsearch");
    }
    builder.endObject();
    request.source(builder);
    IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
    // 如果是200则表示成功,否则就是失败
    if (200 == indexResponse.status().getStatus()) {
    }
    // 对响应结果进行处理
    String index1 = indexResponse.getIndex();
    String type1 = indexResponse.getType();
    String id1 = indexResponse.getId();
    long version = indexResponse.getVersion();
    // 如果是新增/修改的话的话
    if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
    } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
    }
    ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
    if (shardInfo.getTotal() != shardInfo.getSuccessful()) {
    }
    if (shardInfo.getFailed() > 0) {
        for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
            String reason = failure.reason();
        }
    }
}
Also used : HashMap(java.util.HashMap) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) IndexRequest(org.elasticsearch.action.index.IndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse)

Example 33 with IndexResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project uavstack by uavorg.

the class DoTestESClient method main.

public static void main(String[] args) {
    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY);
    client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("127.0.0.1", 9300)));
    IndexRequestBuilder irb = client.prepareIndex("uav_test_db", "uav_test_table");
    Map<String, Object> item = new HashMap<String, Object>();
    item.put("name", "zz");
    item.put("age", 1);
    irb.setSource(item);
    IndexResponse ir = irb.get();
    System.out.println(ir.status());
    client.close();
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) 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) InetSocketAddress(java.net.InetSocketAddress) InetSocketTransportAddress(org.elasticsearch.common.transport.InetSocketTransportAddress)

Example 34 with IndexResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project syncope by apache.

the class ElasticsearchIndexManager method after.

@TransactionalEventListener
public void after(final AnyCreatedUpdatedEvent<Any<?>> event) throws IOException {
    GetResponse getResponse = client.prepareGet(AuthContextUtils.getDomain().toLowerCase(), event.getAny().getType().getKind().name(), event.getAny().getKey()).get();
    if (getResponse.isExists()) {
        LOG.debug("About to update index for {}", event.getAny());
        UpdateResponse response = client.prepareUpdate(AuthContextUtils.getDomain().toLowerCase(), event.getAny().getType().getKind().name(), event.getAny().getKey()).setRetryOnConflict(elasticsearchUtils.getRetryOnConflict()).setDoc(elasticsearchUtils.builder(event.getAny())).get();
        LOG.debug("Index successfully updated for {}: {}", event.getAny(), response);
    } else {
        LOG.debug("About to create index for {}", event.getAny());
        IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), event.getAny().getType().getKind().name(), event.getAny().getKey()).setSource(elasticsearchUtils.builder(event.getAny())).get();
        LOG.debug("Index successfully created for {}: {}", event.getAny(), response);
    }
}
Also used : UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) GetResponse(org.elasticsearch.action.get.GetResponse) TransactionalEventListener(org.springframework.transaction.event.TransactionalEventListener)

Example 35 with IndexResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project elasticsearch-indexing-proxy by codelibs.

the class ProxyActionFilter method getExecutor.

@SuppressWarnings("unchecked")
private <Request extends ActionRequest, Response extends ActionResponse> Supplier<Response> getExecutor(final Task task, final String action, final Request request) {
    if (BulkAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final BulkRequest req = (BulkRequest) request;
        for (final DocWriteRequest<?> subReq : req.requests()) {
            if (indexingProxyService.isTargetIndex(subReq.index())) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.requests().size()) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.requests().size());
        }
        return () -> {
            final List<BulkItemResponse> responseList = new ArrayList<>(req.requests().size());
            for (int i = 0; i < req.requests().size(); i++) {
                final DocWriteRequest<?> dwr = req.requests().get(i);
                if (dwr instanceof IndexRequest) {
                    final IndexRequest r = (IndexRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final IndexResponse response = new IndexResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else if (dwr instanceof UpdateRequest) {
                    final UpdateRequest r = (UpdateRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final UpdateResponse response = new UpdateResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), Result.CREATED);
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else if (dwr instanceof DeleteRequest) {
                    final DeleteRequest r = (DeleteRequest) dwr;
                    final String id = r.id() == null ? INDEX_UUID : r.id();
                    final DeleteResponse response = new DeleteResponse(new ShardId(new Index(r.index(), INDEX_UUID), 0), r.type(), id, r.version(), true);
                    response.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
                    responseList.add(new BulkItemResponse(i, r.opType(), response));
                } else {
                    responseList.add(new BulkItemResponse(i, dwr.opType(), new BulkItemResponse.Failure(dwr.index(), dwr.type(), dwr.id(), new ElasticsearchException("Unknown request: " + dwr))));
                }
            }
            return (Response) new BulkResponse(responseList.toArray(new BulkItemResponse[responseList.size()]), (System.nanoTime() - startTime) / 1000000);
        };
    } else if (DeleteAction.NAME.equals(action)) {
        final DeleteRequest req = (DeleteRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            final DeleteResponse res = new DeleteResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
            res.setShardInfo(new ReplicationResponse.ShardInfo(1, 1, ReplicationResponse.EMPTY));
            return (Response) res;
        };
    } else if (DeleteByQueryAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final DeleteByQueryRequest req = (DeleteByQueryRequest) request;
        for (final String index : req.indices()) {
            if (indexingProxyService.isTargetIndex(index)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.indices().length) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
        }
        return () -> {
            return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
        };
    } else if (IndexAction.NAME.equals(action)) {
        final IndexRequest req = (IndexRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            return (Response) new IndexResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), true);
        };
    } else if (UpdateAction.NAME.equals(action)) {
        final UpdateRequest req = (UpdateRequest) request;
        if (!indexingProxyService.isTargetIndex(req.index())) {
            return null;
        }
        return () -> {
            final String id = req.id() == null ? INDEX_UUID : req.id();
            return (Response) new UpdateResponse(new ShardId(new Index(req.index(), INDEX_UUID), 0), req.type(), id, req.version(), Result.CREATED);
        };
    } else if (UpdateByQueryAction.NAME.equals(action)) {
        final long startTime = System.nanoTime();
        int count = 0;
        final UpdateByQueryRequest req = (UpdateByQueryRequest) request;
        for (final String index : req.indices()) {
            if (indexingProxyService.isTargetIndex(index)) {
                count++;
            }
        }
        if (count == 0) {
            return null;
        } else if (count != req.indices().length) {
            throw new ElasticsearchException("Mixed target requests. ({} != {})", count, req.indices().length);
        }
        return () -> {
            return (Response) new BulkByScrollResponse(TimeValue.timeValueNanos(System.nanoTime() - startTime), new BulkByScrollTask.Status(null, 0, 0, 0, 0, 0, 0, 0, 0, 0, TimeValue.ZERO, 0, null, TimeValue.ZERO), Collections.emptyList(), Collections.emptyList(), false);
        };
    }
    return null;
}
Also used : Index(org.elasticsearch.index.Index) ElasticsearchException(org.elasticsearch.ElasticsearchException) IndexRequest(org.elasticsearch.action.index.IndexRequest) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) ShardId(org.elasticsearch.index.shard.ShardId) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) ArrayList(java.util.ArrayList) List(java.util.List) BulkByScrollTask(org.elasticsearch.index.reindex.BulkByScrollTask) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteByQueryRequest(org.elasticsearch.index.reindex.DeleteByQueryRequest) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) UpdateByQueryRequest(org.elasticsearch.index.reindex.UpdateByQueryRequest) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) BulkByScrollResponse(org.elasticsearch.index.reindex.BulkByScrollResponse) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) ActionResponse(org.elasticsearch.action.ActionResponse) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) ReplicationResponse(org.elasticsearch.action.support.replication.ReplicationResponse) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest)

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