Search in sources :

Example 36 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project fess by codelibs.

the class EsAbstractBehavior method delegateUpdate.

@Override
protected int delegateUpdate(final Entity entity, final UpdateOption<? extends ConditionBean> option) {
    final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
    final IndexRequestBuilder builder = createUpdateRequest(esEntity);
    final IndexResponse response = builder.execute().actionGet(indexTimeout);
    long version = response.getVersion();
    if (version != -1) {
        esEntity.asDocMeta().version(version);
    }
    return 1;
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 37 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project fess by codelibs.

the class EsAbstractBehavior method delegateInsert.

// ===================================================================================
//                                                                              Update
//                                                                              ======
@Override
protected int delegateInsert(final Entity entity, final InsertOption<? extends ConditionBean> option) {
    final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
    IndexRequestBuilder builder = createInsertRequest(esEntity);
    final IndexResponse response = builder.execute().actionGet(indexTimeout);
    esEntity.asDocMeta().id(response.getId());
    return response.getResult() == Result.CREATED ? 1 : 0;
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 38 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project fess by codelibs.

the class EsAbstractBehavior method delegateUpdate.

@Override
protected int delegateUpdate(final Entity entity, final UpdateOption<? extends ConditionBean> option) {
    final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
    final IndexRequestBuilder builder = createUpdateRequest(esEntity);
    final IndexResponse response = builder.execute().actionGet(indexTimeout);
    long version = response.getVersion();
    if (version != -1) {
        esEntity.asDocMeta().version(version);
    }
    return 1;
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 39 with IndexResponse

use of org.elasticsearch.action.index.IndexResponse in project fess by codelibs.

the class EsAbstractBehavior method delegateInsert.

// ===================================================================================
//                                                                              Update
//                                                                              ======
@Override
protected int delegateInsert(final Entity entity, final InsertOption<? extends ConditionBean> option) {
    final EsAbstractEntity esEntity = (EsAbstractEntity) entity;
    IndexRequestBuilder builder = createInsertRequest(esEntity);
    final IndexResponse response = builder.execute().actionGet(indexTimeout);
    esEntity.asDocMeta().id(response.getId());
    return response.getResult() == Result.CREATED ? 1 : 0;
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) IndexResponse(org.elasticsearch.action.index.IndexResponse)

Example 40 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("sub_title", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("category_name", searchItem.getCategory_name()).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)

Aggregations

IndexResponse (org.elasticsearch.action.index.IndexResponse)79 Test (org.junit.Test)15 SearchResponse (org.elasticsearch.action.search.SearchResponse)13 IOException (java.io.IOException)11 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)11 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)10 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)6 Settings (org.elasticsearch.common.settings.Settings)6 ArrayList (java.util.ArrayList)5 ExecutionException (java.util.concurrent.ExecutionException)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)4 GetResponse (org.elasticsearch.action.get.GetResponse)4