Search in sources :

Example 6 with JestResult

use of io.searchbox.client.JestResult in project gerrit by GerritCodeReview.

the class ElasticChangeIndex method replace.

@Override
public void replace(ChangeData cd) throws IOException {
    String deleteIndex;
    String insertIndex;
    try {
        if (cd.change().getStatus().isOpen()) {
            insertIndex = OPEN_CHANGES;
            deleteIndex = CLOSED_CHANGES;
        } else {
            insertIndex = CLOSED_CHANGES;
            deleteIndex = OPEN_CHANGES;
        }
    } catch (OrmException e) {
        throw new IOException(e);
    }
    Bulk bulk = new Bulk.Builder().defaultIndex(indexName).defaultType("changes").addAction(insert(insertIndex, cd)).addAction(delete(deleteIndex, cd.getId())).refresh(true).build();
    JestResult result = client.execute(bulk);
    if (!result.isSucceeded()) {
        throw new IOException(String.format("Failed to replace change %s in index %s: %s", cd.getId(), indexName, result.getErrorMessage()));
    }
}
Also used : OrmException(com.google.gwtorm.server.OrmException) Builder(io.searchbox.core.Bulk.Builder) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) Bulk(io.searchbox.core.Bulk) JestResult(io.searchbox.client.JestResult)

Example 7 with JestResult

use of io.searchbox.client.JestResult in project opennms by OpenNMS.

the class BasicAuthTest method canPerformPreemptiveAuth.

@Test
public void canPerformPreemptiveAuth() throws IOException {
    stubFor(post(urlEqualTo("/_all/_search")).withBasicAuth("mi", "mimimi").willReturn(aResponse().withBody("{\n" + "    \"took\": 1,\n" + "    \"timed_out\": false,\n" + "    \"_shards\":{\n" + "        \"total\" : 1,\n" + "        \"successful\" : 1,\n" + "        \"skipped\" : 0,\n" + "        \"failed\" : 0\n" + "    },\n" + "    \"hits\":{\n" + "        \"total\" : 0,\n" + "        \"max_score\": 0,\n" + "        \"hits\" : []\n" + "    }\n" + "}")));
    // Create the REST client factory in a fashion similar to how it's created in the Blueprint
    // and specify some basic auth credentials
    RestClientFactory restClientFactory = new RestClientFactory(wireMockRule.url(""), "mi", "mimimi");
    restClientFactory.setTimeout(3000);
    restClientFactory.setSocketTimeout(3000);
    restClientFactory.setRetries(0);
    // Execute a request with our client, the contents of the search don't really matter
    // here, we just need some valid JSON
    JestClient client = restClientFactory.getJestClient();
    JestResult result = client.execute(new Search.Builder("{\n" + "    \"query\" : {\n" + "        \"term\" : { \"user\" : \"kimchy\" }\n" + "    }\n" + "}").build());
    // Verify
    assertTrue("The search request using Jest should succeed.", result.isSucceeded());
    // Cleanup
    client.shutdownClient();
}
Also used : RestClientFactory(org.opennms.plugins.elasticsearch.rest.RestClientFactory) JestClient(io.searchbox.client.JestClient) JestResult(io.searchbox.client.JestResult) Test(org.junit.Test)

Example 8 with JestResult

use of io.searchbox.client.JestResult in project nutch by apache.

the class ElasticRestIndexWriter method commit.

@Override
public void commit() throws IOException {
    if (basicFuture != null) {
        // wait for previous to finish
        long beforeWait = System.currentTimeMillis();
        try {
            JestResult result = basicFuture.get();
            if (result == null) {
                throw new RuntimeException();
            }
            long msWaited = System.currentTimeMillis() - beforeWait;
            LOG.info("Previous took in ms {}, including wait {}", millis, msWaited);
        } catch (InterruptedException | ExecutionException e) {
            LOG.error("Error waiting for result ", e);
        }
        basicFuture = null;
    }
    if (bulkBuilder != null) {
        if (bulkDocs > 0) {
            // start a flush, note that this is an asynchronous call
            basicFuture = new BasicFuture<>(null);
            millis = System.currentTimeMillis();
            client.executeAsync(bulkBuilder.build(), new JestResultHandler<BulkResult>() {

                @Override
                public void completed(BulkResult bulkResult) {
                    basicFuture.completed(bulkResult);
                    millis = System.currentTimeMillis() - millis;
                }

                @Override
                public void failed(Exception e) {
                    basicFuture.completed(null);
                    LOG.error("Failed result: ", e);
                }
            });
        }
        bulkBuilder = null;
    }
    if (createNewBulk) {
        // Prepare a new bulk request
        bulkBuilder = new Bulk.Builder().defaultIndex(defaultIndex).defaultType(defaultType);
        bulkDocs = 0;
        bulkLength = 0;
    }
}
Also used : BulkResult(io.searchbox.core.BulkResult) Bulk(io.searchbox.core.Bulk) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) ExecutionException(java.util.concurrent.ExecutionException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ExecutionException(java.util.concurrent.ExecutionException) JestResult(io.searchbox.client.JestResult)

Example 9 with JestResult

use of io.searchbox.client.JestResult in project jmxtrans by jmxtrans.

the class ElasticWriter method internalWrite.

@Override
protected void internalWrite(Server server, Query query, ImmutableList<Result> results) throws Exception {
    for (Result result : results) {
        log.debug("Query result: [{}]", result);
        Map<String, Object> resultValues = result.getValues();
        for (Entry<String, Object> values : resultValues.entrySet()) {
            Object value = values.getValue();
            if (isNumeric(value)) {
                Map<String, Object> map = new HashMap<>();
                map.put("serverAlias", server.getAlias());
                map.put("server", server.getHost());
                map.put("port", server.getPort());
                map.put("objDomain", result.getObjDomain());
                map.put("className", result.getClassName());
                map.put("typeName", result.getTypeName());
                map.put("attributeName", result.getAttributeName());
                map.put("key", values.getKey());
                map.put("keyAlias", result.getKeyAlias());
                map.put("value", Double.parseDouble(value.toString()));
                map.put("timestamp", result.getEpoch());
                log.debug("Insert into Elastic: Index: [{}] Type: [{}] Map: [{}]", indexName, ELASTIC_TYPE_NAME, map);
                Index index = new Index.Builder(map).index(indexName).type(ELASTIC_TYPE_NAME).build();
                JestResult addToIndex = jestClient.execute(index);
                if (!addToIndex.isSucceeded()) {
                    throw new ElasticWriterException(String.format("Unable to write entry to elastic: %s", addToIndex.getErrorMessage()));
                }
            } else {
                log.warn("Unable to submit non-numeric value to Elastic: [{}] from result [{}]", value, result);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) CreateIndex(io.searchbox.indices.CreateIndex) Index(io.searchbox.core.Index) JestResult(io.searchbox.client.JestResult) Result(com.googlecode.jmxtrans.model.Result) JestResult(io.searchbox.client.JestResult)

Example 10 with JestResult

use of io.searchbox.client.JestResult in project jmxtrans by jmxtrans.

the class ElasticWriterTests method sendNonNumericMessageToElastic.

@Test
public void sendNonNumericMessageToElastic() throws Exception {
    Result resultWithNonNumericValue = new Result(1, "attributeName", "className", "objDomain", "classNameAlias", "typeName", ImmutableMap.of("key", (Object) "abc"));
    writer.doWrite(dummyServer(), dummyQuery(), ImmutableList.of(resultWithNonNumericValue));
    // only one call is expected: the index check. No write is being made with non-numeric values.
    Mockito.verify(mockClient, times(0)).execute(Matchers.<Action<JestResult>>any());
}
Also used : JestResult(io.searchbox.client.JestResult) Result(com.googlecode.jmxtrans.model.Result) JestResult(io.searchbox.client.JestResult) DocumentResult(io.searchbox.core.DocumentResult) Test(org.junit.Test)

Aggregations

JestResult (io.searchbox.client.JestResult)14 IOException (java.io.IOException)7 Bulk (io.searchbox.core.Bulk)5 Builder (io.searchbox.core.Bulk.Builder)3 CreateIndex (io.searchbox.indices.CreateIndex)3 Test (org.junit.Test)3 JsonElement (com.google.gson.JsonElement)2 JsonObject (com.google.gson.JsonObject)2 Result (com.googlecode.jmxtrans.model.Result)2 GsonBuilder (com.google.gson.GsonBuilder)1 OrmException (com.google.gwtorm.server.OrmException)1 Action (io.searchbox.action.Action)1 JestClient (io.searchbox.client.JestClient)1 BulkResult (io.searchbox.core.BulkResult)1 DocumentResult (io.searchbox.core.DocumentResult)1 Index (io.searchbox.core.Index)1 DeleteIndex (io.searchbox.indices.DeleteIndex)1 IndicesExists (io.searchbox.indices.IndicesExists)1 GetAliases (io.searchbox.indices.aliases.GetAliases)1 PutMapping (io.searchbox.indices.mapping.PutMapping)1