Search in sources :

Example 51 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project graylog2-server by Graylog2.

the class MessagesES7IT method indexMessage.

@Override
protected boolean indexMessage(String index, Map<String, Object> source, String id) {
    final IndexRequest indexRequest = new IndexRequest(index).source(source).id(id);
    final IndexResponse result = this.elasticsearch.elasticsearchClient().execute((c, requestOptions) -> c.index(indexRequest, requestOptions));
    return result.status().equals(RestStatus.CREATED);
}
Also used : IndexResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest)

Example 52 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch-opennlp-plugin by spinscale.

the class OpenNlpPluginIntegrationTest method testThatFacetingIsWorking.

@Test
public void testThatFacetingIsWorking() throws Exception {
    putMapping("/test-mapping-analyzers.json");
    String sampleText = copyToStringFromClasspath("/sample-text.txt");
    IndexResponse indexResponse = indexElement(sampleText);
    SearchResponse searchResponse = new SearchRequestBuilder(node.client()).setIndices(index).setTypes(type).setQuery(QueryBuilders.matchAllQuery()).addFacet(new TermsFacetBuilder("names").field("someFieldAnalyzed.name").order(TermsFacet.ComparatorType.TERM)).execute().actionGet();
    assertThat(searchResponse.getHits().totalHits(), is(1L));
    assertThat(searchResponse.getHits().getAt(0).id(), is(indexResponse.getId()));
    TermsFacet termsFacet = searchResponse.getFacets().facet(TermsFacet.class, "names");
    assertThat(termsFacet.getTotalCount(), is(2L));
    assertThat(termsFacet.getEntries().get(0).getTerm().string(), is("Jack Nicholson"));
    assertThat(termsFacet.getEntries().get(1).getTerm().string(), is("Kobe Bryant"));
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) TermsFacetBuilder(org.elasticsearch.search.facet.terms.TermsFacetBuilder) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) TermsFacet(org.elasticsearch.search.facet.terms.TermsFacet) SearchResponse(org.elasticsearch.action.search.SearchResponse) Test(org.junit.Test)

Example 53 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project jstorm by alibaba.

the class TestIndexBolt method execute.

@Override
public void execute(Tuple tuple) {
    String line = tuple.getString(0);
    String[] dims = line.split("\t");
    JsonObject object = new JsonObject();
    object.addProperty("no", dims[1]);
    object.addProperty("date", dims[2]);
    IndexResponse indexResponse = client.prepareIndex("test", "test").setId(dims[0]).setSource(object.toString()).execute().actionGet();
    collector.ack(tuple);
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) JsonObject(com.google.gson.JsonObject)

Example 54 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project xmall by Exrick.

the class ItemESMessageListener method onMessage.

@Override
public void onMessage(Message message) {
    try {
        // 从消息中取商品id
        TextMessage textMessage = (TextMessage) message;
        log.info("得到消息:" + textMessage.getText());
        String[] text = textMessage.getText().split(",");
        Long itemId = new Long(text[1]);
        // 等待事务提交
        Thread.sleep(1000);
        // 更新索引
        Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
        if ("add".equals(text[0])) {
            // 根据商品id查询商品信息
            SearchItem searchItem = itemMapper.getItemById(itemId);
            String image = searchItem.getProductImageBig();
            if (image != null && !"".equals(image)) {
                String[] strings = image.split(",");
                image = strings[0];
            } else {
                image = "";
            }
            searchItem.setProductImageBig(image);
            IndexResponse indexResponse = client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId())).setSource(jsonBuilder().startObject().field("productId", searchItem.getProductId()).field("salePrice", searchItem.getSalePrice()).field("productName", searchItem.getProductName()).field("subTitle", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("categoryName", searchItem.getCategoryName()).field("cid", searchItem.getCid()).endObject()).get();
        } else if ("delete".equals(text[0])) {
            DeleteResponse deleteResponse = client.prepareDelete(ITEM_INDEX, ITEM_TYPE, String.valueOf(itemId)).get();
        }
        log.info("处理消息成功");
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) IndexResponse(org.elasticsearch.action.index.IndexResponse) TransportAddress(org.elasticsearch.common.transport.TransportAddress) TextMessage(javax.jms.TextMessage) Settings(org.elasticsearch.common.settings.Settings) SearchItem(cn.exrick.manager.dto.front.SearchItem)

Example 55 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project elasticsearch by elastic.

the class TransportShardBulkAction method executeUpdateRequest.

/**
     * Executes update request, delegating to a index or delete operation after translation,
     * handles retries on version conflict and constructs update response
     * NOTE: reassigns bulk item request at <code>requestIndex</code> for replicas to
     * execute translated update request (NOOP update is an exception). NOOP updates are
     * indicated by returning a <code>null</code> operation in {@link BulkItemResultHolder}
     * */
private static BulkItemResultHolder executeUpdateRequest(UpdateRequest updateRequest, IndexShard primary, IndexMetaData metaData, BulkShardRequest request, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillis, final MappingUpdatePerformer mappingUpdater) throws Exception {
    Engine.Result updateOperationResult = null;
    UpdateResponse updateResponse = null;
    BulkItemRequest replicaRequest = request.items()[requestIndex];
    int maxAttempts = updateRequest.retryOnConflict();
    for (int attemptCount = 0; attemptCount <= maxAttempts; attemptCount++) {
        final UpdateHelper.Result translate;
        // translate update request
        try {
            translate = updateHelper.prepare(updateRequest, primary, nowInMillis);
        } catch (Exception failure) {
            // we may fail translating a update to index or delete operation
            // we use index result to communicate failure while translating update request
            updateOperationResult = new Engine.IndexResult(failure, updateRequest.version(), SequenceNumbersService.UNASSIGNED_SEQ_NO);
            // out of retry loop
            break;
        }
        // execute translated update request
        switch(translate.getResponseResult()) {
            case CREATED:
            case UPDATED:
                IndexRequest indexRequest = translate.action();
                MappingMetaData mappingMd = metaData.mappingOrDefault(indexRequest.type());
                indexRequest.process(mappingMd, request.index());
                updateOperationResult = executeIndexRequestOnPrimary(indexRequest, primary, mappingUpdater);
                break;
            case DELETED:
                DeleteRequest deleteRequest = translate.action();
                updateOperationResult = executeDeleteRequestOnPrimary(deleteRequest, primary);
                break;
            case NOOP:
                primary.noopUpdate(updateRequest.type());
                break;
            default:
                throw new IllegalStateException("Illegal update operation " + translate.getResponseResult());
        }
        if (updateOperationResult == null) {
            // this is a noop operation
            updateResponse = translate.action();
            // out of retry loop
            break;
        } else if (updateOperationResult.hasFailure() == false) {
            // set translated update (index/delete) request for replica execution in bulk items
            switch(updateOperationResult.getOperationType()) {
                case INDEX:
                    IndexRequest updateIndexRequest = translate.action();
                    final IndexResponse indexResponse = new IndexResponse(primary.shardId(), updateIndexRequest.type(), updateIndexRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.IndexResult) updateOperationResult).isCreated());
                    BytesReference indexSourceAsBytes = updateIndexRequest.source();
                    updateResponse = new UpdateResponse(indexResponse.getShardInfo(), indexResponse.getShardId(), indexResponse.getType(), indexResponse.getId(), indexResponse.getSeqNo(), indexResponse.getVersion(), indexResponse.getResult());
                    if ((updateRequest.fetchSource() != null && updateRequest.fetchSource().fetchSource()) || (updateRequest.fields() != null && updateRequest.fields().length > 0)) {
                        Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(indexSourceAsBytes, true, updateIndexRequest.getContentType());
                        updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), indexResponse.getVersion(), sourceAndContent.v2(), sourceAndContent.v1(), indexSourceAsBytes));
                    }
                    // set translated request as replica request
                    replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateIndexRequest);
                    break;
                case DELETE:
                    DeleteRequest updateDeleteRequest = translate.action();
                    DeleteResponse deleteResponse = new DeleteResponse(primary.shardId(), updateDeleteRequest.type(), updateDeleteRequest.id(), updateOperationResult.getSeqNo(), updateOperationResult.getVersion(), ((Engine.DeleteResult) updateOperationResult).isFound());
                    updateResponse = new UpdateResponse(deleteResponse.getShardInfo(), deleteResponse.getShardId(), deleteResponse.getType(), deleteResponse.getId(), deleteResponse.getSeqNo(), deleteResponse.getVersion(), deleteResponse.getResult());
                    updateResponse.setGetResult(updateHelper.extractGetResult(updateRequest, request.index(), deleteResponse.getVersion(), translate.updatedSourceAsMap(), translate.updateSourceContentType(), null));
                    // set translated request as replica request
                    replicaRequest = new BulkItemRequest(request.items()[requestIndex].id(), updateDeleteRequest);
                    break;
            }
            assert updateOperationResult.getSeqNo() != SequenceNumbersService.UNASSIGNED_SEQ_NO;
            // out of retry loop
            break;
        } else if (updateOperationResult.getFailure() instanceof VersionConflictEngineException == false) {
            // out of retry loop
            break;
        }
    }
    return new BulkItemResultHolder(updateResponse, updateOperationResult, replicaRequest);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexRequest(org.elasticsearch.action.index.IndexRequest) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IOException(java.io.IOException) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateHelper(org.elasticsearch.action.update.UpdateHelper) DeleteResponse(org.elasticsearch.action.delete.DeleteResponse) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) IndexResponse(org.elasticsearch.action.index.IndexResponse) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Engine(org.elasticsearch.index.engine.Engine) Tuple(org.elasticsearch.common.collect.Tuple)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)103 Test (org.junit.Test)26 SearchResponse (org.elasticsearch.action.search.SearchResponse)18 IOException (java.io.IOException)15 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)15 HashMap (java.util.HashMap)13 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)13 IndexRequest (org.elasticsearch.action.index.IndexRequest)13 ElasticsearchException (org.elasticsearch.ElasticsearchException)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)8 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)7 Settings (org.elasticsearch.common.settings.Settings)7 ArrayList (java.util.ArrayList)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)6 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)5