Java Code Examples for io.searchbox.client.JestClient#execute()

The following examples show how to use io.searchbox.client.JestClient#execute() . 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: ElasticSearchClient.java    From vind with Apache License 2.0 6 votes vote down vote up
private void bulkUpdate(Bulk.Builder bulkProcessor, int retries, JestClient client) throws InterruptedException {
    if (Objects.nonNull(client)) {
        try {
            BulkResult result = client.execute(bulkProcessor.build());
            if (result.getFailedItems().size() > 0) {
                final String errorIds = result.getFailedItems().stream()
                            .map( fi -> fi.id)
                            .collect(Collectors.joining(", "));
                log.error("Error executing bulk update: {} items where no updated [{}].", result.getFailedItems().size(), errorIds);

            }
        } catch (IOException e) {
            log.warn("Error executing bulk update: {}", e.getMessage(), e);
            if (retries > ES_MAX_TRIES) {
                log.error("Error executing bulk update: reached maximum number of retries [{}].", retries);
                throw new RuntimeException("Error executing bulk update: " + e.getMessage(), e);
            } else {
                Thread.sleep((retries + 1) * ES_WAIT_TIME);
                bulkUpdate(bulkProcessor, retries + 1, client);
            }
        }
    } else {
        log.error("Error in bulk update request: ES client has not been initialized, client is null.");
        throw new RuntimeException("Error in bulk update request: ES client has not been initialized, client is null.");
    }
}
 
Example 2
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 3
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
/**
 * Cache a result
 *
 * @param content
 */
private void cacheResult(final String content) {
    final JestClient client = getElasticSearchClient();
    if (client != null) {
        final Index contentIndex = new Index.Builder(content).index(elasticIndex).type("logEntry").build();
        try {
            final DocumentResult result = client.execute(contentIndex);
            log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded());
        } catch (IOException e) {
            log.error("Error indexing content {}: {}", content, e.getMessage(), e);
        }
        //TODO: move to async at some point
        /*client.executeAsync(contentIndex, new JestResultHandler<JestResult>() {
            @Override
            public void completed(JestResult result) {
                log.debug("Completed indexation of content {} with succeeded={}", content, result.isSucceeded());
            }

            @Override
            public void failed(Exception e) {
                log.error("Error indexing content {}: {}", content, e.getMessage(), e);
            }
        });*/

    } else {
        log.warn("Content {} won't be cached, there is not target bucket", content);
    }
}
 
Example 4
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
private synchronized SearchResult  getScrollQuery(String query, int retry, JestClient client) throws InterruptedException {

        if (client != null) {
            final Search.Builder searchBuilder = new Search.Builder(query)
                    .addIndex(elasticIndex)
                    .setParameter(Parameters.SCROLL, SCROLL_TIME_SESSION);

            final Search search = searchBuilder.build();

            try {
                final SearchResult result = client.execute(search);
                log.debug("Completed query scroll query in {} tries. Succeeded: {}", retry + 1, result.isSucceeded());
                return result;
            } catch (IOException e) {
                log.warn("Try {} - Error in query scroll request query: {}", retry, e.getMessage(), e);
                if(retry > ES_MAX_TRIES) {
                    log.error("Error in query scroll request: reached maximum number of scroll tries [{}].", retry);
                    throw new RuntimeException("Error in query scroll request query: " + e.getMessage(), e);
                } else {
                    Thread.sleep((retry + 1) * ES_WAIT_TIME);
                    return getScrollQuery(query, retry + 1, client);
                }
            }
            //TODO: move to async at some point
            /*client.executeAsync(search,new JestResultHandler<JestResult>() {
                @Override
                public void completed(JestResult result) {
                    log.debug("Completed total requests query. Succeeded: {}", result.isSucceeded());
                }

                @Override
                public void failed(Exception e) {
                    log.error("Error indexing content : {}", e.getMessage(), e);
                }
            });*/
        }
        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 5
Source File: JestTest.java    From springBoot-study with Apache License 2.0 5 votes vote down vote up
/**
* 批量新增数据
* @param indexName
* @param typeName
* @param objs
* @return
* @throws Exception
*/
public static boolean insertBatch(JestClient jestClient,String indexName, String typeName, List<Object> objs) throws Exception {  
    Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);  
    for (Object obj : objs) {  
        Index index = new Index.Builder(obj).build();  
         bulk.addAction(index);  
    }  
    BulkResult br = jestClient.execute(bulk.build());  
    return br.isSucceeded();  
   }
 
Example 6
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public synchronized SearchResult  getQuery(String query) {
    final JestClient client = getElasticSearchClient();
    if (client != null) {

        final Search.Builder searchBuilder = new Search.Builder(query)
                .addIndex(elasticIndex);

        if (StringUtils.isNotEmpty(this.logType)) {
            searchBuilder.addType(this.logType);
        }

        final Search search = searchBuilder.build();

        try {
            final SearchResult result = client.execute(search);
            log.debug("Completed total requests query. Succeeded: {}", result.isSucceeded());
            return result;
        } catch (IOException e) {
            log.error("Error in total requests query: {}", e.getMessage(), e);
            return null;
        }
        //TODO: move to async at some point
        /*client.executeAsync(search,new JestResultHandler<JestResult>() {
            @Override
            public void completed(JestResult result) {
                log.debug("Completed total requests query. Succeeded: {}", result.isSucceeded());
            }

            @Override
            public void failed(Exception e) {
                log.error("Error indexing content : {}", e.getMessage(), e);
            }
        });*/
    }
    return null;
}
 
Example 7
Source File: IndexMsgController.java    From EserKnife with Apache License 2.0 5 votes vote down vote up
@RequestMapping(value = "/addNewIndex")
@ResponseBody
public Object addNewIndex(@RequestParam("clusterName") String clusterName,
                         @RequestParam("indexName") String indexName,
                          @RequestParam("settings") String settings) {
    Map<String, Object> map = Maps.newHashMap();
    try {
        JestClient jestClient = JestManager.getJestClient(clusterName);
        /*IndicesExists indicesExists = new IndicesExists.Builder(indexName).build();
        JestResult jestResult = jestClient.execute(indicesExists);
        if(!jestResult.isSucceeded()){*/
            CreateIndex createIndex = new CreateIndex.Builder(indexName).settings(settings).build();
            JestResult result = jestClient.execute(createIndex);
            if(result.isSucceeded()){
                map.put("success",true);
            }else{
                map.put("errMsg", result.getErrorMessage());
            }
       /* }else{
            map.put("exist",true);
        }*/
    } catch (Exception e) {
        LOGGER.error("添加索引失败:",e);
        e.printStackTrace();
        map.put("errMsg", "添加失败,请联系管理员");
    }
    return map;
}
 
Example 8
Source File: ElasticSearchClient.java    From vind with Apache License 2.0 5 votes vote down vote up
public boolean init(String elasticHost, String elasticPort, String elasticIndex, String logType, Path processResultMappingFile) {
    this.elasticHost = elasticHost;
    this.elasticPort = elasticPort;
    this.elasticIndex = elasticIndex;
    this.logType = logType;

    try {
        final JestClient client = getElasticSearchClient();
        boolean indexExists = client.execute(new IndicesExists.Builder(elasticIndex).build()).isSucceeded();
        if (!indexExists){
            log.info("Creating elasticsearch index.");
            client.execute(new CreateIndex.Builder(elasticIndex).build());
        }

        if (StringUtils.isNotBlank(logType) && (processResultMappingFile != null)) {
            log.info("Updating type mapping.");
            final String mappingJson = new String(ByteStreams.toByteArray(Files.newInputStream(processResultMappingFile)));
            client.execute(new PutMapping.Builder(elasticIndex, logType, mappingJson).build());
        }

        log.info("Established elasticsearch connection to host '{}:{}', index '{}'.", elasticHost, elasticPort, elasticIndex);
        elasticClient = client;
    } catch (IOException e) {
        log.error("Error creating base index on ElasticSearch: {}", e.getMessage(), e);
        elasticClient = null;
    }
    return true;
}
 
Example 9
Source File: JestClientDemo.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    // Construct a new Jest client according to configuration via factory
    JestClientFactory factory = new JestClientFactory();
    factory.setHttpClientConfig(new HttpClientConfig
        .Builder("http://172.22.6.9:9200")
        .multiThreaded(true)
        //Per default this implementation will create no more than 2 concurrent connections per given route
        .defaultMaxTotalConnectionPerRoute(2)
        // and no more 20 connections in total
        .maxTotalConnection(100)
        .build());
    JestClient client = factory.getObject();
    JestResult result = client.execute(new CreateIndex.Builder("user").build());
    System.out.println("result = " + result.getJsonString());
}
 
Example 10
Source File: JestTest.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
* 批量新增数据
* @param indexName
* @param typeName
* @param objs
* @return
* @throws Exception
*/
public static boolean insertBatch(JestClient jestClient,String indexName, String typeName, List<Object> objs) throws Exception {  
    Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);  
    for (Object obj : objs) {  
        Index index = new Index.Builder(obj).build();  
         bulk.addAction(index);  
    }  
    BulkResult br = jestClient.execute(bulk.build());  
    return br.isSucceeded();  
   }
 
Example 11
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
  * 查询数据
  * @param indexName
  * @param typeName
  * @return
  * @throws Exception
  */
public static String getIndexMapping(JestClient jestClient,String indexName, String typeName) throws Exception {  
    GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();  
    JestResult jr =jestClient.execute(getMapping);  
    return jr.getJsonString();  
 }
 
Example 12
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
* 删除数据
* @param indexName
* @param typeName
* @param id
* @return
* @throws Exception
*/
public boolean delete(JestClient jestClient,String indexName, String typeName, String id) throws Exception {  
    DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());  
    return dr.isSucceeded();  
}
 
Example 13
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
* 删除索引
* @param indexName
* @return
* @throws Exception
*/
public boolean delete(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}
 
Example 14
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 新增数据
 * @param indexName
 * @param typeName
 * @param source
 * @return
 * @throws Exception
 */
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {  
    PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
    JestResult jr = jestClient.execute(putMapping);  
    return jr.isSucceeded();  
}
 
Example 15
Source File: JestTest.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建索引
 * @param indexName
 * @return
 * @throws Exception
 */
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}
 
Example 16
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
* 删除数据
* @param indexName
* @param typeName
* @param id
* @return
* @throws Exception
*/
public boolean delete(JestClient jestClient,String indexName, String typeName, String id) throws Exception {  
    DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());  
    return dr.isSucceeded();  
}
 
Example 17
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
* 删除索引
* @param indexName
* @return
* @throws Exception
*/
public boolean delete(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}
 
Example 18
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
  * 查询数据
  * @param indexName
  * @param typeName
  * @return
  * @throws Exception
  */
public static String getIndexMapping(JestClient jestClient,String indexName, String typeName) throws Exception {  
    GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();  
    JestResult jr =jestClient.execute(getMapping);  
    return jr.getJsonString();  
 }
 
Example 19
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
 * 新增数据
 * @param indexName
 * @param typeName
 * @param source
 * @return
 * @throws Exception
 */
public boolean insert(JestClient jestClient,String indexName, String typeName, String source) throws Exception {  
    PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
    JestResult jr = jestClient.execute(putMapping);  
    return jr.isSucceeded();  
}
 
Example 20
Source File: JestTest.java    From springBoot-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建索引
 * @param indexName
 * @return
 * @throws Exception
 */
public boolean createIndex(JestClient jestClient,String indexName) throws Exception {  
    JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
    return jr.isSucceeded();  
}