Search in sources :

Example 51 with IndexResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexResponse in project YCSB by brianfrankcooper.

the class ElasticsearchClient method insert.

@Override
public Status insert(String table, String key, Map<String, ByteIterator> values) {
    try (XContentBuilder doc = jsonBuilder()) {
        doc.startObject();
        for (final Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
            doc.field(entry.getKey(), entry.getValue());
        }
        doc.field(KEY, key);
        doc.endObject();
        final IndexResponse indexResponse = client.prepareIndex(indexKey, table).setSource(doc).get();
        if (indexResponse.getResult() != DocWriteResponse.Result.CREATED) {
            return Status.ERROR;
        }
        if (!isRefreshNeeded) {
            synchronized (this) {
                isRefreshNeeded = true;
            }
        }
        return Status.OK;
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : IndexResponse(org.elasticsearch.action.index.IndexResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) UnknownHostException(java.net.UnknownHostException) DBException(site.ycsb.DBException)

Example 52 with IndexResponse

use of org.graylog.shaded.elasticsearch7.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 53 with IndexResponse

use of org.graylog.shaded.elasticsearch7.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 54 with IndexResponse

use of org.graylog.shaded.elasticsearch7.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 55 with IndexResponse

use of org.graylog.shaded.elasticsearch7.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)

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