io.searchbox.client.JestResult Java Examples

The following examples show how to use io.searchbox.client.JestResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractClientFactory.java    From apiman with Apache License 2.0 7 votes vote down vote up
/**
 * Called to initialize the storage.
 * @param client the jest client
 * @param indexName the name of the ES index to initialize
 * @param defaultIndexName the default ES index - used to determine which -settings.json file to use
 */
protected void initializeClient(JestClient client, String indexName, String defaultIndexName) {
    try {
        client.execute(new Health.Builder().build());
        Action<JestResult> action = new IndicesExists.Builder(indexName).build();
        // There was occasions where a race occurred here when multiple threads try to
        // create the index simultaneously. This caused a non-fatal, but annoying, exception.
        synchronized(AbstractClientFactory.class) {
            JestResult result = client.execute(action);
            if (!result.isSucceeded()) {
                createIndex(client, indexName, defaultIndexName + "-settings.json"); //$NON-NLS-1$
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #2
Source File: JestServiceImpl.java    From EserKnife with Apache License 2.0 6 votes vote down vote up
@Override
public JestResult deleteBulk(String clustName, String indexName, String Type, List<SearchResultDetailVO> results) {
    JestResult result = null ;
    try {
        Bulk.Builder bulkBulder = new Bulk.Builder().defaultIndex(indexName).defaultType(Type);
        if(CollectionUtils.isNotEmpty(results)){
            for (SearchResultDetailVO resultDetailVO:results){
                bulkBulder.addAction(new Delete.Builder(resultDetailVO.getId()).index(indexName).type(Type).build());
            }
        }
        result = JestManager.getJestClient(clustName).execute(bulkBulder.build());
    } catch (Exception e) {
        e.printStackTrace();
        LOGGER.error("deleteBulk失败:",e);
    }
    return result ;

}
 
Example #3
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) {
    return new JestResultHandler<JestResult>() {
        @Override
        public void completed(JestResult result) {

            backoffPolicy.deregister(bulk);

            if (!result.isSucceeded()) {
                LOG.warn(result.getErrorMessage());
                failureHandler.apply(bulk);
            }
        }
        @Override
        public void failed(Exception ex) {
            LOG.warn(ex.getMessage(), ex);
            backoffPolicy.deregister(bulk);
            failureHandler.apply(bulk);
        }
    };
}
 
Example #4
Source File: IndexMsgController.java    From EserKnife with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/reindex")
@ResponseBody
public Object reIndex(
        @RequestParam("clusterName") String clusterName,
        @RequestParam("newColsJson") String newColsJson) {
    String execute =null;
    try {
        JestResult result = (JestResult) jestService.httpProxy(clusterName,"/_reindex","POST",newColsJson);
        if(result != null){
           execute = result.getJsonString();
        }
    } catch (Exception e) {
        LOGGER.error("添加字段失败:",e);
    }
    return JSON.parse(execute);
}
 
Example #5
Source File: PollCachingESRegistry.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Checks the ES store to see if the 'dataVersion' entry has been updated with a newer
 * version #.  If it has, then we need to invalidate our cache.
 */
protected void checkCacheVersion() {
    // Be very aggressive in invalidating the cache.
    boolean invalidate = true;
    try {
        Get get = new Get.Builder(getDefaultIndexName(), "instance").type("dataVersion").build(); //$NON-NLS-1$ //$NON-NLS-2$
        JestResult result = getClient().execute(get);
        if (result.isSucceeded()) {
            String latestDV = result.getJsonObject().get("_version").getAsString(); //$NON-NLS-1$
            if (latestDV != null && dataVersion != null && latestDV.equals(dataVersion)) {
                invalidate = false;
            } else {
                dataVersion = latestDV;
            }
        }
    } catch (IOException e) {
        logger.warn("Elasticsearch is not available, using cache");
        invalidate = false;
    }
    if (invalidate) {
        invalidateCache();
    }
}
 
Example #6
Source File: IndexMsgController.java    From EserKnife with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/indexManager")
@ResponseBody
public Object indexManager(@RequestParam("clusterName") String clusterName,
                       @RequestParam(value="data",required=false) String data,
                       @RequestParam("url") String url,
                       @RequestParam("method") String method) {
    String json =null;
    try {
        JestResult result = (JestResult) jestService.httpProxy(clusterName,url,method,data);
        if(result != null){
            json = result.getJsonString();
        }
    } catch (Exception e) {
        LOGGER.error("index manager error:",e);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg:",e);
        return jsonObject;
    }
    return (Map<String, Map<String, String>>) JSON.parse(json);
}
 
Example #7
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 6 votes vote down vote up
private synchronized JestResult scrollResults(String scrollId, int retry, JestClient client) throws InterruptedException {
    if (client != null) {
        final SearchScroll scroll = new SearchScroll.Builder(scrollId, SCROLL_TIME_SESSION).build();
        try {
            final JestResult result = client.execute(scroll);
            log.debug("Completed scroll query. Succeeded: {}", result.isSucceeded());
            return result;
        } catch (IOException e) {
            log.warn("Error in scroll request query: {}", e.getMessage(), e);
            if(retry > ES_MAX_TRIES) {
                log.error("Error in scroll request: reached maximum number of scroll tries [{}].", retry);
                throw new RuntimeException("Error in scroll request query: " + e.getMessage(), e);

            } else {
                Thread.sleep((retry + 1) * ES_WAIT_TIME);
                return scrollResults(scrollId, retry + 1, client);
            }
        }
    }
    log.error("Error in scroll request query: ES client has not been initialized, client is null.");
    throw new RuntimeException("Error in scroll request query: ES client has not been initialized, client is null.");
}
 
Example #8
Source File: EsStorage.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * This method will check if a migration of the index is needed
 * @return true or false
 * @throws IOException
 */
private boolean isMigrationNeeded() throws IOException {
    // Check if migration is needed for too large swagger files - "keyword" to "text"
    // See https://github.com/apiman/apiman/issues/736
    JestResult result = esClient.execute(new GetMapping.Builder().addIndex(getIndexName()).build());
    if (result.isSucceeded()) {
        boolean migrationNeeded = result.getJsonObject()
                .getAsJsonObject(getIndexName())
                .getAsJsonObject("mappings")
                .getAsJsonObject("auditEntry")
                .getAsJsonObject("properties")
                .getAsJsonObject("data")
                .getAsJsonPrimitive("type")
                .getAsString()
                .equals("keyword");
        if (migrationNeeded) {
            logger.info("Migration of Elasticsearch index is needed.");
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
 
Example #9
Source File: IndexFunctionsDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testIdsInIndexFunction()
{
    JestResult jestResult = mock(JestResult.class);
    SearchResult searchResult = mock(SearchResult.class);
    List<String> idList = Arrays.asList("{id:1}");
    List<String> emptyList = new ArrayList<>();
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("_scroll_id", "100");
    when(jestClientHelper.execute(any(Search.class))).thenReturn(searchResult);
    when(searchResult.getSourceAsStringList()).thenReturn(idList);
    when(searchResult.getJsonObject()).thenReturn(jsonObject);
    when(jestClientHelper.execute(any(SearchScroll.class))).thenReturn(jestResult);
    when(jestResult.getSourceAsStringList()).thenReturn(emptyList);
    indexFunctionsDao.getIdsInIndex("INDEX_NAME", "DOCUMENT_TYPE");
    verify(jestClientHelper).execute(any(Search.class));
    verify(searchResult, times(2)).getSourceAsStringList();
    verify(searchResult).getJsonObject();
    verify(jestClientHelper).execute(any(SearchScroll.class));
    verify(jestResult).getSourceAsStringList();
}
 
Example #10
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncCallbackHandlesBufferedJestResult() throws IOException {

    // given
    JestResultHandler<JestResult> jestResultHandler = createMockTestResultHandler();

    BufferedJestHttpClient.BufferedResultCallback asyncCallback = mockHttpResponseCallback(jestResultHandler);

    String reasonPhrase = UUID.randomUUID().toString();
    HttpResponse httpResponse = createDefaultTestHttpResponse(200, reasonPhrase);

    // when
    asyncCallback.completed(httpResponse);

    // then
    verify(jestResultHandler, never()).failed(any());
    verify(jestResultHandler).completed(captor.capture());
    JestResult jestResult = captor.getValue();

    assertTrue(jestResult.isSucceeded());
    assertEquals(200, jestResult.getResponseCode());
    assertEquals(reasonPhrase, jestResult.getErrorMessage());
    assertNull(((BufferedJestResult)jestResult).getItems());

}
 
Example #11
Source File: AliasManagerController.java    From EserKnife with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value = "/createAll")
@ResponseBody
public Object cretaAll(@RequestParam("clusterName") String clusterName,
                              @RequestParam(value="data",required=false) String data,
                              @RequestParam("url") String url,
                              @RequestParam("method") String method) {
    String json =null;
    try {
        JestResult result = (JestResult) jestService.httpProxy(clusterName,url,method,data);
        if(result != null){
            json = result.getJsonString();
        }
    } catch (Exception e) {
        LOG.error("alias error:{}",e.getMessage());
        return null;
    }
    return JSON.parse(json);
}
 
Example #12
Source File: JestClientHelper.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Method to use the JEST client to execute an elastic action
 *
 * @param <T> The expected class of the action
 * @param action action
 *
 * @return a jest action result
 */
@Retryable(maxAttempts = 3, value = RestClientException.class, backoff = @Backoff(delay = 5000, multiplier = 2))
public <T extends JestResult> T execute(Action<T> action)
{
    T actionResult = null;
    try
    {
        actionResult = jestClientFactory.getJestClient().execute(action);

        // log the error if the action failed but no exception is thrown
        if (actionResult == null || !actionResult.isSucceeded())
        {
            LOGGER.error("Failed to execute JEST client action. action={}, actionResult={}", action, actionResult);
        }
    }
    catch (final IOException ioException)
    {
        LOGGER.error("Failed to execute JEST client action.", ioException);
        //throw a runtime exception so that the client needs not catch
        throw new RestClientException(ioException.getMessage()); //NOPMD
    }

    return actionResult;
}
 
Example #13
Source File: EsStorage.java    From apiman with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
private void fetch() throws StorageException {
    try {
        Builder builder = new SearchScroll.Builder(scrollId, "1m");
        SearchScroll scroll = new SearchScroll(builder) {
            @Override
            public JestResult createNewElasticSearchResult(String responseBody, int statusCode,
                    String reasonPhrase, Gson gson) {
                return createNewElasticSearchResult(new SearchResult(gson), responseBody, statusCode, reasonPhrase, gson);
            }
        };
        SearchResult response = (SearchResult) esClient.execute(scroll);
        if (!response.isSucceeded()) {
            throw new StorageException("Scrolled fetch failed " + response.getErrorMessage());
        }
        this.hits = (List) response.getHits(Map.class);
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
 
Example #14
Source File: BufferedJestHttpClientTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void executeAsyncDelegatesToFailureHandlerOnPrepareRequestIOException() throws IOException {

    // given
    BufferedJestHttpClient client = spy(createDefaultTestHttpClient());
    CloseableHttpAsyncClient asyncClient = mockAsyncClient(client);

    String expectedMesage = UUID.randomUUID().toString();
    BufferedBulk bulk = createDefaultTestBufferedBulk();
    when(client.prepareRequest(bulk)).thenThrow(new IOException(expectedMesage));

    JestResultHandler<JestResult> jestResultHandler = createMockTestResultHandler();

    // when
    client.executeAsync(bulk, jestResultHandler);

    // then
    verify(jestResultHandler).failed(exceptionCaptor.capture());
    assertEquals(expectedMesage, exceptionCaptor.getValue().getMessage());

    verify(client, never()).getAsyncClient();
    verify(asyncClient, never()).execute(any(HttpUriRequest.class), any());

}
 
Example #15
Source File: IndexFunctionsDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateIndexDocumentsFunction()
{
    SearchResult jestResult = mock(SearchResult.class);
    JestResult jestResultAliases = mock(JestResult.class);
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("INDEX_NAME_1", "INDEX_NAME");
    jsonObject.addProperty("INDEX_NAME_2", "INDEX_NAME");

    // Build mocks
    when(jestClientHelper.execute(any())).thenReturn(jestResult);
    when(jestResult.isSucceeded()).thenReturn(true);
    when(jestClientHelper.execute(any())).thenReturn(jestResultAliases);
    when(jestResultAliases.isSucceeded()).thenReturn(true);
    when(jestResultAliases.getJsonObject()).thenReturn(jsonObject);

    Map<String, String> documentMap = new HashMap<>();
    documentMap.put("1", "JSON");
    indexFunctionsDao.createIndexDocuments("INDEX_NAME", "DOCUMENT_TYPE", documentMap);

    verify(jestClientHelper, times(3)).execute(any());
    verify(jestResultAliases).getJsonObject();
    verifyNoMoreInteractions(jestClientHelper);
}
 
Example #16
Source File: IndexerManagerBeanTest.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void pingTest() throws IOException {
    Health health = new Health.Builder().build();
    JestResult result = new JestResult(new Gson());
    Mockito.when(esClient.execute(health))
            .thenReturn(result);

    result.setSucceeded(false);
    Assert.assertFalse(indexerManagerBean.ping());

    result.setSucceeded(true);
    Assert.assertTrue(indexerManagerBean.ping());

    Mockito.when(esClient.execute(health)).thenThrow(new IOException());
    Assert.assertFalse(indexerManagerBean.ping());
}
 
Example #17
Source File: ESRegistry.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * @see io.apiman.gateway.engine.IRegistry#publishApi(io.apiman.gateway.engine.beans.Api, io.apiman.gateway.engine.async.IAsyncResultHandler)
 */
@Override
public void publishApi(final Api api, final IAsyncResultHandler<Void> handler) {
    try {
        String id = getApiId(api);
        Index index = new Index.Builder(api).refresh(false)
                .index(getIndexName()).setParameter(Parameters.OP_TYPE, "index") //$NON-NLS-1$
                .type("api").id(id).build(); //$NON-NLS-1$
        JestResult result = getClient().execute(index);
        if (!result.isSucceeded()) {
            throw new IOException(result.getErrorMessage());
        } else {
            handler.handle(AsyncResultImpl.create((Void) null));
        }
    } catch (Exception e) {
        handler.handle(AsyncResultImpl.create(
                new PublishingException(Messages.i18n.format("ESRegistry.ErrorPublishingApi"), e),  //$NON-NLS-1$
                Void.class));
    }
}
 
Example #18
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsExecutedAfterFailedRequest() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2");

    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    // when
    resultHandler.failed(new IOException());

    // then
    ArgumentCaptor<Bulk> captor = ArgumentCaptor.forClass(Bulk.class);
    verify(failoverHandler, times(1)).apply(captor.capture());
    verify((BufferedBulk)bulk, times(1)).completed();

    assertEquals(bulk, captor.getValue());

}
 
Example #19
Source File: AdminOperationsTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void exceptionMessageIsRetrievedOnIndexTemplateIOException() {

    //given
    JestHttpObjectFactory factory = spy(createTestObjectFactoryBuilder().build());

    final String expectedMessage = "test-exception";

    TestException testException = spy(new TestException(expectedMessage));
    when(factory.createClient()).thenAnswer((Answer<JestResult>) invocation -> {
        throw testException;
    });

    IndexTemplate indexTemplate = spy(IndexTemplate.newBuilder()
            .withPath("classpath:indexTemplate-6.json")
            .withName("testName")
            .build());

    // when
    factory.execute(indexTemplate);

    // then
    verify(testException).getMessage();

}
 
Example #20
Source File: IndexFunctionsDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateIndexDocumentsFunction()
{
    SearchResult jestResult = mock(SearchResult.class);
    JestResult jestResultAliases = mock(JestResult.class);
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("INDEX_NAME_1", "INDEX_NAME");
    jsonObject.addProperty("INDEX_NAME_2", "INDEX_NAME");
    // Build mocks
    when(jestClientHelper.execute(any())).thenReturn(jestResult);
    when(jestResult.isSucceeded()).thenReturn(true);
    when(jestClientHelper.execute(any())).thenReturn(jestResultAliases);
    when(jestResultAliases.isSucceeded()).thenReturn(true);
    when(jestResultAliases.getJsonObject()).thenReturn(jsonObject);

    // Call the method under test
    Map<String, String> documentMap = new HashMap<>();
    documentMap.put("1", "JSON");
    indexFunctionsDao.updateIndexDocuments("INDEX_NAME", "DOCUMENT_TYPE", documentMap);

    // Verify the calls to external methods
    verify(jestClientHelper, times(3)).execute(any());
    verifyNoMoreInteractions(jestClientHelper);
}
 
Example #21
Source File: PollCachingESRegistry.java    From apiman with Apache License 2.0 6 votes vote down vote up
/**
 * Stores a "dataversion" record in the ES store.  There is only a single one of these.  The
 * return value of the add will include the version number of the entity.  This version
 * number is what we use to determine whether our cache is stale.
 */
protected void updateDataVersion() {
    DataVersionBean dv = new DataVersionBean();
    dv.setUpdatedOn(System.currentTimeMillis());
    Index index = new Index.Builder(dv).refresh(false)
            .index(getDefaultIndexName())
            .type("dataVersion").id("instance").build(); //$NON-NLS-1$ //$NON-NLS-2$
    getClient().executeAsync(index, new JestResultHandler<JestResult>() {
        @Override
        public void completed(JestResult result) {
            dataVersion = null;
        }
        @Override
        public void failed(Exception e) {
            dataVersion = null;
        }
    });
}
 
Example #22
Source File: BufferedJestHttpObjectFactoryTest.java    From log4j2-elasticsearch with Apache License 2.0 6 votes vote down vote up
@Test
public void failoverIsNotExecutedAfterSuccessfulRequest() {

    // given
    BufferedJestHttpObjectFactory.Builder builder = createTestObjectFactoryBuilder();
    BufferedJestHttpObjectFactory config = spy(builder.build());

    ItemSource<ByteBuf> payload1 = createDefaultTestBuffereItemSource("test1");
    ItemSource<ByteBuf> payload2 = createDefaultTestBuffereItemSource("test2");
    Bulk bulk = createTestBatch(payload1, payload2);

    Function<Bulk, Boolean> failoverHandler = mock(Function.class);
    JestResultHandler<JestResult> resultHandler = config.createResultHandler(bulk, failoverHandler);

    JestResult result = mock(JestResult.class);
    when(result.isSucceeded()).thenReturn(true);

    // when
    resultHandler.completed(result);

    // then
    verify(failoverHandler, never()).apply(Mockito.any(Bulk.class));
}
 
Example #23
Source File: IndexFunctionsDaoTest.java    From herd with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateIndexDocumentsFunctionWithFailures()
{
    SearchResult jestResult = mock(SearchResult.class);
    JestResult jestResultAliases = mock(JestResult.class);
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("INDEX_NAME_1", "INDEX_NAME");
    jsonObject.addProperty("INDEX_NAME_2", "INDEX_NAME");

    // Build mocks
    when(jestClientHelper.execute(any())).thenReturn(jestResult);
    when(jestClientHelper.execute(any())).thenReturn(jestResultAliases);
    when(jestResultAliases.isSucceeded()).thenReturn(true);
    when(jestResultAliases.getJsonObject()).thenReturn(jsonObject);
    when(jestResult.isSucceeded()).thenReturn(false);
    Map<String, String> documentMap = new HashMap<>();
    documentMap.put("1", "JSON");
    indexFunctionsDao.createIndexDocuments("INDEX_NAME", "DOCUMENT_TYPE", documentMap);

    verify(jestClientHelper, times(3)).execute(any());
    verify(jestResultAliases).getJsonObject();
    verifyNoMoreInteractions(jestClientHelper);
}
 
Example #24
Source File: JestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Function<Bulk, Boolean> createBatchListener(FailoverPolicy failoverPolicy) {
    return new Function<Bulk, Boolean>() {

        private Function<Bulk, Boolean> failureHandler = createFailureHandler(failoverPolicy);

        @Override
        public Boolean apply(Bulk bulk) {

            while (!operations.isEmpty()) {
                try {
                    operations.remove().execute();
                } catch (Exception e) {
                    // TODO: redirect to failover (?) retry with exp. backoff (?) multiple options here
                    LOG.error("Deferred operation failed: {}", e.getMessage());
                }
            }

            if (backoffPolicy.shouldApply(bulk)) {
                LOG.warn("Backoff applied. Request rejected.");
                failureHandler.apply(bulk);
                return false;
            } else {
                backoffPolicy.register(bulk);
            }

            JestResultHandler<JestResult> jestResultHandler = createResultHandler(bulk, failureHandler);
            createClient().executeAsync(bulk, jestResultHandler);
            return true;
        }

    };
}
 
Example #25
Source File: ElasticsearchService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * getStatisticsCountByDay(按天统计时间内的数据)
 *
 * @param EsModel es
 *                startTime
 *                endTime
 * @return
 */
@RequestMapping("getStatisticsCountByDay")
public JSONObject getStatisticsCountByDay(@RequestBody EsModel es) {
    JSONObject resJson = null;
    try {
        Search search = elasticsearchHelperService.statisticsDayCondition(es);
        if (search == null) {
            return resJson;
        }
        EsPage esPage = es.getEsPage();
        JestResult result = jestClient.execute(search);
        logger.info("getStatisticsCountByDay :" + result.getJsonString());
        int responseCode = -1;
        if (result != null) {
            responseCode = result.getResponseCode();
        }
        if (responseCode != 200) {
            logger.error("ES 搜索错误信息:" + result.getErrorMessage());
            return resJson;
        }
        resJson = elasticsearchHelperService.getDayStatisticsResult(result);
    } catch (Exception e) {

        String msg = StringUtil.getExceptionMsg(e);
        StringBuilder builder = new StringBuilder();
        builder.append("getStatisticsCountByDay(按天统计时间内的数据) 参数 es:").append(JSON.toJSONString(es)).append(" \n\n错误消息:").append(msg);
        logger.error(builder.toString());
    }
    return resJson;
}
 
Example #26
Source File: IndexManagerBean.java    From eplmp with Eclipse Public License 1.0 5 votes vote down vote up
private void createIndex(String indexName) throws IndexerNotAvailableException, IndexerRequestException {
    Settings settings = Settings.builder().build();
    JestResult result;

    try {
        result = esClient.execute(new CreateIndex.Builder(indexName).settings(settings.toString()).build());
    } catch (IOException e) {
        throw new IndexerNotAvailableException();
    }

    if (!result.isSucceeded()) {
        LOGGER.log(Level.WARNING, "Cannot create index" + indexName + ": " + result.getErrorMessage());
        throw new IndexerRequestException(result.getErrorMessage());
    }
}
 
Example #27
Source File: ElasticsearchService.java    From xmfcn-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 保存单条数据到ES
 *
 * @param message
 * @return
 */
@RequestMapping("save")
public RetData save(@RequestBody EsModel es) {
    RetData retData = new RetData();
    RetData aReturn = elasticsearchHelperService.validateParms(es);
    if (aReturn.getCode() == ResultCodeMessage.FAILURE) {
        return aReturn;
    }
    String message = es.getMessage();
    String indexName = es.getIndex();
    String type = es.getType();
    try {
        JestResult jestResult = elasticsearchHelperService.createIndex(es);
        if (jestResult == null) {
            return aReturn;
        }
        int responseCode = jestResult.getResponseCode();
        if (responseCode != 200) {
            return aReturn;
        }
        Index index = new Index.Builder(message).index(indexName).type(type).build();
        DocumentResult result = jestClient.execute(index);
        responseCode = result.getResponseCode();
        if (responseCode == 200) {
            retData.setMessage(ResultCodeMessage.SUCCESS_MESSAGE);
            retData.setCode(ResultCodeMessage.SUCCESS);
        } else {
            retData.setMessage(result.getErrorMessage());
        }
    } catch (Exception e) {

        String msg = StringUtil.getExceptionMsg(e);
        StringBuilder builder = new StringBuilder();
        builder.append("saveBatch(保存集合数据到ES) 参数 es:").append(JSON.toJSONString(es)).append(" \n\n错误消息:").append(msg);
        logger.error(builder.toString());
        retData.setMessage(msg);
    }
    return retData;
}
 
Example #28
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public void closeScroll(String scrollId) {
    final ClearScroll clearScroll = new ClearScroll.Builder().addScrollId(scrollId).build();
    try {
        final JestResult result = getElasticSearchClient().execute(clearScroll);
        log.debug("Closed scroll query {}. Succeeded: {}", scrollId, result.isSucceeded());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #29
Source File: BufferedJestHttpObjectFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
protected JestResultHandler<JestResult> createResultHandler(Bulk bulk, Function<Bulk, Boolean> failureHandler) {
    return new JestResultHandler<JestResult>() {

        @Override
        public void completed(JestResult result) {

            backoffPolicy.deregister(bulk);

            if (!result.isSucceeded()) {
                LOG.warn(result.getErrorMessage());
                // TODO: filter only failed items when retry is ready.
                // failing whole bulk for now
                failureHandler.apply(bulk);
            }
            ((BufferedBulk)bulk).completed();
        }

        @Override
        public void failed(Exception ex) {
            LOG.warn(ex.getMessage(), ex);
            backoffPolicy.deregister(bulk);
            failureHandler.apply(bulk);
            ((BufferedBulk)bulk).completed();
        }

    };
}
 
Example #30
Source File: JestExample.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
 * 清缓存
 * @throws Exception
 */
private static void clearCache() throws Exception {
	JestClient jestClient = JestExample.getJestClient();
	ClearCache closeIndex = new ClearCache.Builder().build();
	JestResult result = jestClient.execute(closeIndex);
	System.out.println(result.getJsonString());
}