精华内容
下载资源
问答
  • JestClient

    2016-12-31 06:07:00
    JestService.java [html] view plain copy 在CODE上查看代码片派生到我的代码片 public class JestService { /** * 获取JestClient对象 * @return */ public JestClient getJestClient() { ...
    JestService.java
    
    
    [html] view plain copy 在CODE上查看代码片派生到我的代码片
    public class JestService {  
      
        /**  
         * 获取JestClient对象  
         * @return  
         */  
        public JestClient getJestClient() {    
                  
            JestClientFactory factory = new JestClientFactory();  
            factory.setHttpClientConfig(new HttpClientConfig  
                                   .Builder("http://localhost:9200")  
                                   .gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'hh:mm:ss").create())  
                                   .connTimeout(1500)  
                                   .readTimeout(3000)  
                                   .multiThreaded(true)  
                                   .build());  
            return factory.getObject();  
            }  
          
        /**  
         * 创建索引  
         * @param jestClient  
         * @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();  
        }  
          
        /**  
         * Put映射  
         * @param jestClient  
         * @param indexName  
         * @param typeName  
         * @param source  
         * @return  
         * @throws Exception  
         */  
        public boolean createIndexMapping(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();  
            }  
          
        /**  
         * Get映射  
         * @param jestClient  
         * @param indexName  
         * @param typeName  
         * @return  
         * @throws Exception  
         */  
        public 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();  
            }  
          
        /**  
         * 索引文档  
         * @param jestClient  
         * @param indexName  
         * @param typeName  
         * @param objs  
         * @return  
         * @throws Exception  
         */  
        public boolean index(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();  
            }  
          
        /**  
         * 搜索文档  
         * @param jestClient  
         * @param indexName  
         * @param typeName  
         * @param query  
         * @return  
         * @throws Exception  
         */  
        public SearchResult search(JestClient jestClient, String indexName, String typeName, String query) throws Exception {  
              
            Search search = new Search.Builder(query)  
                .addIndex(indexName)  
                .addType(typeName)  
                .build();  
            return jestClient.execute(search);  
            }  
          
        /**  
         * Count文档  
         * @param jestClient  
         * @param indexName  
         * @param typeName  
         * @param query  
         * @return  
         * @throws Exception  
         */  
        public Double count(JestClient jestClient, String indexName, String typeName, String query) throws Exception {  
      
            Count count = new Count.Builder()  
                .addIndex(indexName)  
                .addType(typeName)  
                .query(query)  
                .build();  
            CountResult results = jestClient.execute(count);   
            return results.getCount();  
        }  
          
        /**  
         * Get文档  
         * @param jestClient  
         * @param indexName  
         * @param typeName  
         * @param id  
         * @return  
         * @throws Exception  
         */  
        public JestResult get(JestClient jestClient, String indexName, String typeName, String id) throws Exception {  
                
            Get get = new Get.Builder(indexName, id).type(typeName).build();  
            return jestClient.execute(get);  
        }  
          
        /**  
         * Delete索引  
         * @param jestClient  
         * @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();  
        }  
          
        /**  
         * Delete文档  
         * @param jestClient  
         * @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();  
        }  
          
        /**  
         * 关闭JestClient客户端  
         * @param jestClient  
         * @throws Exception  
         */  
        public void closeJestClient(JestClient jestClient) throws Exception {  
              
            if (jestClient != null) {  
              jestClient.shutdownClient();  
            }  
            }  
    }  
    (3)UserTest.java
    
    [html] view plain copy 在CODE上查看代码片派生到我的代码片
    public class UserTest {  
      
        private JestService jestService;  
            private JestClient jestClient;  
            private String indexName = "hwd";  
            private String typeName = "user";  
            
        @Before  
        public void setUp() throws Exception {  
              
            jestService = new JestService();  
            jestClient = jestService.getJestClient();  
        }  
      
        @After  
        public void tearDown() throws Exception {  
              
            jestService.closeJestClient(jestClient);  
        }  
      
        @Test  
            public void createIndex() throws Exception {  
          
            boolean result = jestService.createIndex(jestClient, indexName);  
            System.out.println(result);  
            }  
          
        @Test  
            public void createIndexMapping() throws Exception {  
          
            String source = "{\"" + typeName + "\":{\"properties\":{"  
                            + "\"id\":{\"type\":\"integer\"}"  
                            + ",\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"}"  
                            + ",\"birth\":{\"type\":\"date\",\"format\":\"strict_date_optional_time||epoch_millis\"}"  
                            + "}}}";  
                System.out.println(source);  
                boolean result = jestService.createIndexMapping(jestClient, indexName, typeName, source);  
                System.out.println(result);  
            }  
          
        @Test  
        public void getIndexMapping() throws Exception {  
              
            String result = jestService.getIndexMapping(jestClient, indexName, typeName);  
            System.out.println(result);  
        }  
          
        @Test  
        public void index() throws Exception {  
              
            List<Object> objs = new ArrayList<Object>();  
            objs.add(new User(1, "T:o\"m-", new Date()));  
            objs.add(new User(2, "J,e{r}r;y:", new Date()));  
            boolean result = jestService.index(jestClient, indexName, typeName, objs);  
            System.out.println(result);  
        }  
          
        @Test  
        public void termQuery() throws Exception {  
              
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders  
                .termQuery("name", "T:o\"m-");//单值完全匹配查询  
            searchSourceBuilder.query(queryBuilder);  
            searchSourceBuilder.size(10);  
            searchSourceBuilder.from(0);  
            String query = searchSourceBuilder.toString();   
            System.out.println(query);  
            SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
            List<Hit<User, Void>> hits = result.getHits(User.class);  
            System.out.println("Size:" + hits.size());  
            for (Hit<User, Void> hit : hits) {  
              User user = hit.source;  
              System.out.println(user.toString());  
            }  
        }  
            
        @Test  
        public void termsQuery() throws Exception {  
              
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders  
                .termsQuery("name", new String[]{ "T:o\"m-", "J,e{r}r;y:" });//多值完全匹配查询  
            searchSourceBuilder.query(queryBuilder);  
            searchSourceBuilder.size(10);  
            searchSourceBuilder.from(0);  
            String query = searchSourceBuilder.toString();   
            System.out.println(query);  
            SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
            List<Hit<User, Void>> hits = result.getHits(User.class);  
            System.out.println("Size:" + hits.size());  
            for (Hit<User, Void> hit : hits) {  
              User user = hit.source;  
              System.out.println(user.toString());  
            }  
        }  
            
        @Test  
        public void wildcardQuery() throws Exception {  
              
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders  
                .wildcardQuery("name", "*:*");//通配符和正则表达式查询  
            searchSourceBuilder.query(queryBuilder);  
            searchSourceBuilder.size(10);  
            searchSourceBuilder.from(0);  
            String query = searchSourceBuilder.toString();      
            System.out.println(query);  
            SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
            List<Hit<User, Void>> hits = result.getHits(User.class);  
            System.out.println("Size:" + hits.size());  
            for (Hit<User, Void> hit : hits) {  
              User user = hit.source;  
              System.out.println(user.toString());  
            }  
        }  
            
        @Test  
        public void prefixQuery() throws Exception {  
              
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders  
                .prefixQuery("name", "T:o");//前缀查询  
            searchSourceBuilder.query(queryBuilder);  
            searchSourceBuilder.size(10);  
            searchSourceBuilder.from(0);  
            String query = searchSourceBuilder.toString();      
            System.out.println(query);  
            SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
            List<Hit<User, Void>> hits = result.getHits(User.class);  
            System.out.println("Size:" + hits.size());  
            for (Hit<User, Void> hit : hits) {  
              User user = hit.source;  
              System.out.println(user.toString());  
            }  
        }  
            
        @Test  
        public void rangeQuery() throws Exception {  
              
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders  
                .rangeQuery("birth")  
                .gte("2016-09-01T00:00:00")  
                .lte("2016-10-01T00:00:00")  
                .includeLower(true)  
                .includeUpper(true);//区间查询  
            searchSourceBuilder.query(queryBuilder);  
            searchSourceBuilder.size(10);  
            searchSourceBuilder.from(0);  
            String query = searchSourceBuilder.toString();  
            System.out.println(query);  
            SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
            List<Hit<User, Void>> hits = result.getHits(User.class);  
            System.out.println("Size:" + hits.size());  
            for (Hit<User, Void> hit : hits) {  
              User user = hit.source;  
              System.out.println(user.toString());  
            }  
        }  
            
        @Test  
        public void queryString() throws Exception {  
              
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders  
                .queryString(QueryParser.escape("T:o\""));//文本检索,应该是将查询的词先分成词库中存在的词,然后分别去检索,存在任一存在的词即返回,查询词分词后是OR的关系。需要转义特殊字符  
            searchSourceBuilder.query(queryBuilder);  
            searchSourceBuilder.size(10);  
            searchSourceBuilder.from(0);  
            String query = searchSourceBuilder.toString();   
            System.out.println(query);  
            SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
            List<Hit<User, Void>> hits = result.getHits(User.class);  
            System.out.println("Size:" + hits.size());  
            for (Hit<User, Void> hit : hits) {  
              User user = hit.source;  
              System.out.println(user.toString());  
            }  
        }  
            
        @Test  
        public void count() throws Exception {  
              
            String[] name = new String[]{ "T:o\"m-", "Jerry" };  
            String from = "2016-09-01T00:00:00";  
            String to = "2016-10-01T00:00:00";  
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
            QueryBuilder queryBuilder = QueryBuilders.boolQuery()  
                .must(QueryBuilders.termsQuery("name", name))  
                .must(QueryBuilders.rangeQuery("birth").gte(from).lte(to));  
            searchSourceBuilder.query(queryBuilder);  
            String query = searchSourceBuilder.toString();   
            System.out.println(query);  
            Double count = jestService.count(jestClient, indexName, typeName, query);  
            System.out.println("Count:" + count);  
        }  
            
        @Test  
        public void get() throws Exception {  
              
            String id = "2";  
            JestResult result = jestService.get(jestClient, indexName, typeName, id);  
            if (result.isSucceeded()) {  
              User user = result.getSourceAsObject(User.class);  
              System.out.println(user.toString());  
            }  
        }  
              
        @Test  
        public void deleteIndexDocument() throws Exception {  
              
            String id = "2";  
            boolean result = jestService.delete(jestClient, indexName, typeName, id);  
            System.out.println(result);  
        }  
          
        @Test  
        public void deleteIndex() throws Exception {  
              
            boolean result = jestService.delete(jestClient, indexName);  
            System.out.println(result);  
        }  
    }  

    转载于:https://www.cnblogs.com/mayidudu/p/6238710.html

    展开全文
  • Elasticsearch-JestClient

    2019-02-21 14:51:35
    java使用Jest Client操作Elasticsearch。自己修改了一些,测试完美运行。
  • Java实现方式之JestClient 使用示例

    千次阅读 2019-06-06 08:05:56
    @Component public class EsService { // spring方式 ...// private JestClient jestClient ; // JestClientFactory 方便本地开发测试 private static final JestClient jestClient; static { ...

    以下Jest方式实现简单示例,本文大部分实现使用queryString方式,实际开发中通过QueryBuilder构建boolQuery()、rangeQuery、
    matchAllQuery、matchQuery、termsQuery、prefixQuery、wildcardQuery、regexpQuery等方式,queryString适用于业务较复杂场景,构建复杂检索式使用。
    以下代码仅作为示例参考,使用时请自行优化。

    
    @Component
    public class EsService {
    
    //    spring方式
    //    @Autowired
    //    private JestClient jestClient ;
    
    // JestClientFactory 方便本地开发测试
        private static final JestClient jestClient;
        static {
            JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig.Builder("http://127.0.0.1:9200")
                    .multiThreaded(true)
                    .build());
            jestClient = factory.getObject();
        }
    
        /**
         * 创建索引
         * @param indexName 索引名称
         */
        public  void createIndex(String indexName) {
            boolean indexExists = false;
            try {
                indexExists = jestClient.execute(new IndicesExists.Builder(indexName).build()).isSucceeded();
                if (indexExists) {
                    //删除操作可添加索引类型 .type(indexType)
                    jestClient.execute(new DeleteIndex.Builder(indexName).build());
                }
                jestClient.execute(new CreateIndex.Builder(indexName).build());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 删除索引
         * @param indexName 索引名称
         * @return true/false
         */
        public boolean deleteIndex(String indexName){
            JestResult jr = null;
            boolean bool = false;
            try {
                jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());
                bool = jr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 获取索引映射
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @return Mapping映射
         */
        public  String getIndexMapping(String indexName, String indexType){
            GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(indexType).build();
            JestResult jr = null;
            String string = "";
            try {
                jr = jestClient.execute(getMapping);
                string = jr.getJsonString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return string;
        }
    
        /**
         * 创建索引映射
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param source    索引映射
         * @return true/false
         */
        public boolean createIndexMapping(String indexName, String indexType, String source){
            PutMapping putMapping = new PutMapping.Builder(indexName, indexType, source).build();
            JestResult jr = null;
            boolean bool = false;
            try {
                jr = jestClient.execute(putMapping);
                bool = jr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 根据文档id查询文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param id        文档id
         * @return 文档
         */
        public Map getIndexDocById(String indexName, String indexType, String id){
            Get get = new Get.Builder(indexName, id).type(indexType).build();
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                JestResult result = jestClient.execute(get);
                JsonObject jsonObject =  result.getJsonObject().get("_source").getAsJsonObject();
                Gson gson = new Gson();
                map = gson.fromJson(jsonObject,map.getClass());
                //将索引id存入map集合
                map.put("id",id);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return map;
        }
    
        /**
         * 根据文档id查询文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param id        文档id
         * @return 文档
         */
        public JsonObject getIndexDocByIdForJson(String indexName, String indexType, String id){
            Get get = new Get.Builder(indexName, id).type(indexType).build();
            Map<String, Object> map = new HashMap<String, Object>();
            JsonObject jsonObject = null;
            try {
                JestResult result = jestClient.execute(get);
                jsonObject =  result.getJsonObject().get("_source").getAsJsonObject();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonObject;
        }
    
        /**
         * 根据文档id删除索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param id 文档唯一id
         */
        public boolean deleteIndexDoc(String indexName, String indexType, String id){
            DocumentResult dr = null;
            boolean bool = false;
            try {
                dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(indexType).build());
                bool = dr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 根据文档id删除索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param search 检索式
         */
        public boolean deleteIndexDocBySearch(String indexName, String indexType, String search){
            DocumentResult dr = null;
            boolean bool = false;
            try {
                SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
                sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
                Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
                dr = jestClient.execute(new Delete.Builder(sb.toString()).index(indexName).type(indexType).build());
                bool = dr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 批量索引文档,(因为设置了唯一id,所以该方法执行时索引字段必须有id),id存在更新,不存在添加
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param list      List集合对戏
         * @return true/false
         */
        public boolean upsertIndexDocBulk(String indexName, String indexType, List<Map<String, Object>> list) {
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(indexType);
            for (Map<String, Object> map : list) {
                //设置文档唯一id值,id存在执行更新,不存在执行添加
                Index index = new Index.Builder(map).id(map.get("id").toString()).build();
                bulk.addAction(index);
            }
            BulkResult br = null;
            boolean boll = false;
            try {
                br = jestClient.execute(bulk.build());
                boll =  br.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return boll;
        }
    
        /**
         * 批量新增索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param list 集合
         * @return true/false
         */
        public boolean updateIndexDocBulk(String indexName, String indexType, List<Map<String, Object>> list) {
            Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(indexType);
            for (Map<String, Object> map : list) {
                //如未设置索引唯一id值,则唯一id会默认生成,索引操作为添加操作
                Index index = new Index.Builder(map).build();
                bulk.addAction(index);
            }
            BulkResult br = null;
            boolean boll = false;
            try {
                br = jestClient.execute(bulk.build());
                boll =  br.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return boll;
        }
    
        /**
         * 单条更新索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param map map集合
         * @return true/false
         */
        public boolean updateIndexDoc(String indexName, String indexType, Map<String, Object> map){
            Index index = new Index.Builder(map).index(indexName).type(indexType).id(map.get("id").toString()).refresh(true).build();
            JestResult jr = null;
            boolean bool = false;
            try {
                jr = jestClient.execute(index);
                bool = jr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 单条更新索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param id 索引id
         * @param map map集合
         * @return true/false
         */
        public boolean updateIndexDoc(String indexName, String indexType,String id, Map<String, Object> map){
            Index index = new Index.Builder(map).index(indexName).type(indexType).id(id).refresh(true).build();
            JestResult jr = null;
            boolean bool = false;
            try {
                jr = jestClient.execute(index);
                bool = jr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 单条更新索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param jsonObject jsonObject
         * @return true/false
         */
        public boolean updateIndexDoc(String indexName, String indexType, JsonObject jsonObject){
            Index index = new Index.Builder(jsonObject).index(indexName).type(indexType).id(jsonObject.get("id").toString()).build();
            JestResult jr = null;
            boolean bool = false;
            try {
                jr = jestClient.execute(index);
                bool = jr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 单条更新索引文档
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param id 索引id
         * @param jsonObject jsonObject
         * @return true/false
         */
        public boolean updateIndexDoc(String indexName, String indexType,String id, JsonObject jsonObject){
            Index index = new Index.Builder(jsonObject).index(indexName).type(indexType).id(id).build();
            JestResult jr = null;
            boolean bool = false;
            try {
                jr = jestClient.execute(index);
                bool = jr.isSucceeded();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bool;
        }
    
        /**
         * 聚类示例
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param field 操作字段
         * @param facetsField 聚类字段
         */
        public void facetsSearch(String indexName, String indexType, String field, String facetsField){
            SumAggregationBuilder sumBuilder = AggregationBuilders.sum(field).field(facetsField);
            Search sb = new Search.Builder(sumBuilder.toString()).addIndex(indexName).addType(indexType).build();
    
            SearchResult result = null;
            try {
                result = jestClient.execute(sb);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * 统计文档总数
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param search    检索式
         * @return 文档总数
         */
        public Double count(String indexName, String indexType, String search) {
            Count count = new Count.Builder().addIndex(indexName).addType(indexType).query(search).build();
            CountResult cr = null;
            Double db = 0d;
            try {
                cr = jestClient.execute(count);
                db = cr.getCount();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return db;
        }
    
        /**
         * 返回文档唯一id,根据检索式
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param search 检索式
         * @return
         */
        public String getIndexDocIds(String indexName, String indexType, String search){
            //设置默认参检索引
            indexName = StringUtils.isBlank(indexName) ? "*" : indexName;
            //设置默认检索全部
            search = StringUtils.isBlank(search) ? "*" : search;
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            //设置默认分词后AND连接,StringQuery支持通配符
            sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
            sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    
            Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
            SearchResult result = null;
            try {
                result = jestClient.execute(sb);
            } catch (IOException e) {
                e.printStackTrace();
            }
            long totalCount = 0;
            StringBuilder sb1 = new StringBuilder();
            if (result != null && result.isSucceeded()) {
                //获取总记录数
                totalCount = result.getTotal();
                if (totalCount > 0) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        String id = jsonArray.get(i).getAsJsonObject().get("_id").toString();
                        sb1.append(id+",");
                    }
                }
            }
            return StringUtils.isNotBlank(sb1.toString()) ? sb1.toString().substring(0,sb1.toString().length()-1) : "";
        }
    
        /**
         * 查询,根据检索式查询
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param search 检索式
         * @param sortField 排序字段
         * @return 返回List结果集
         */
        public List baseSearch(String indexName, String indexType, String search, String sortField) {
            //设置默认参检索引
            indexName = StringUtils.isBlank(indexName) ? "*" : indexName;
            //设置默认检索全部
            search = StringUtils.isBlank(search) ? "*" : search;
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            //设置默认分词后AND连接,StringQuery支持通配符
            sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
            //设置排序规则
            if (StringUtils.isNotBlank(sortField)) {
                sourceBuilder.sort(new FieldSortBuilder(sortField).order(SortOrder.ASC));
            } else {
                sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
            }
            Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
            SearchResult result = null;
            try {
                result = jestClient.execute(sb);
            } catch (IOException e) {
                e.printStackTrace();
            }
            long totalCount = 0;
            List<Object> list = new ArrayList<Object>();
            if (result != null && result.isSucceeded()) {
                //获取总记录数
                totalCount = result.getTotal();
                if (totalCount > 0) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JsonObject jsonObject = jsonArray.get(i).getAsJsonObject().get("_source").getAsJsonObject();
                        Gson gson = new Gson();
                        map = gson.fromJson(jsonObject,map.getClass());
                        //将索引id存入map集合
                        String id = jsonArray.get(i).getAsJsonObject().get("_id").toString();
                        map.put("id",id);
                        list.add(map);
                    }
                }
            }
            return list;
        }
    
        /**
         * 分页查询,通用方法
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param search    检索式,即检索词
         * @param pageNo    页码
         * @param pageSize  每页记录数
         * @return 返回Page结果集
         */
        public Page baseSearch(String indexName, String indexType, String search, Integer pageNo, Integer pageSize){
            //设置默认参检索引
            indexName = StringUtils.isBlank(indexName) ? "*" : indexName;
            //设置默认检索全部
            search = StringUtils.isBlank(search) ? "*" : search;
            //设置默认页码第1页
            pageNo = pageNo==null || pageNo<1 ? 1: pageNo;
            //设置默认每页记录数20
            pageSize = pageSize==null ? 20: pageSize;
            //此处为自己封装的分页方法,返回每页起始记录序号
            int startIndex = Page.getStartOfPage(pageNo, pageSize);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            //设置默认分词后AND连接,StringQuery支持通配符
            sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
            //es分页从0开始
            sourceBuilder.from(startIndex).size(pageSize);
            sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
    
            Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
            SearchResult result = null;
            try {
                result = jestClient.execute(sb);
            } catch (IOException e) {
                e.printStackTrace();
            }
            long totalCount = 0;
            List list = new ArrayList<>();
    
            if (result !=null && result.isSucceeded()) {
                //获取总记录数
                totalCount = result.getTotal();
                if (totalCount > 0) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    /*JsonArray解析结果集*/
                    JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JsonObject jsonObject =  jsonArray.get(i).getAsJsonObject().get("_source").getAsJsonObject();
                        Gson gson = new Gson();
                        map = gson.fromJson(jsonObject,map.getClass());
                        //将索引id存入map集合
                        String id = jsonArray.get(i).getAsJsonObject().get("_id").toString();
                        map.put("id",id);
                        list.add(map);
                    }
                }
            }
            return new Page(pageNo, totalCount, pageSize, list);
        }
    
        /**
         * 分页查询,通用方法
         * @param indexName 索引名称
         * @param indexType 索引类型
         * @param search    检索式,即检索词
         * @param pageNo    页码
         * @param pageSize  每页记录数
         * @param sortField 排序字段,多字段间逗号分隔
         * @param sortRule 排序规则,多值每个值与sortF字段一一对应,逗号分隔(ASC升序/DESC降序)
         * @return 返回Page结果集
         */
        public Page baseSearch(String indexName, String indexType, String search, Integer pageNo, Integer pageSize, String sortField , String sortRule){
            //设置默认参检索引
            indexName = StringUtils.isBlank(indexName) ? "*" : indexName;
            //设置默认检索全部
            search = StringUtils.isBlank(search) ? "*" : search;
            //设置默认页码第1页
            pageNo = pageNo==null || pageNo<1 ? 1: pageNo;
            //设置默认每页记录数20
            pageSize = pageSize==null ? 20: pageSize;
            //调用分页,返回每页起始记录序号
            int startIndex = Page.getStartOfPage(pageNo, pageSize);
            //创建sourceBuilder
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            //设置默认分词后AND连接,StringQuery支持通配符
            sourceBuilder.query(QueryBuilders.queryStringQuery(search).defaultOperator(Operator.AND));
            //es分页从0开始
            sourceBuilder.from(startIndex).size(pageSize);
            //设置排序规则
            if (StringUtils.isNotBlank(sortField)) {
                String [] sf = sortField.split(",");
                String [] sr = sortRule.split(",");
                StringBuilder sb = new StringBuilder();
                for (int i=0; i<sf.length; i++) {
                    if ("ASC".equals(sr[i])) {
                        //按排序字段进行升序排序
                        sourceBuilder.sort(new FieldSortBuilder(sf[i]).order(SortOrder.ASC));
                    } else {
                        //按排序字段进行降序排序
                        sourceBuilder.sort(new FieldSortBuilder(sf[i]).order(SortOrder.DESC));
                    }
                }
            } else {
                //默认相关度降序排序
                sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
            }
            Search sb = new Search.Builder(sourceBuilder.toString()).addIndex(indexName).addType(indexType).build();
            SearchResult result = null;
            try {
                result = jestClient.execute(sb);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //存储总文档数
            long totalCount = 0;
            //用于存储最后的结果集
            List list = new ArrayList<>();
    
            if (result !=null && result.isSucceeded()) {
                //获取总记录数
                totalCount = result.getTotal();
                if (totalCount > 0) {
                    //用于转储数据
                    Map<String, Object> map = new HashMap<String, Object>();
                    /*JsonArray解析结果集*/
                    JsonArray jsonArray = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JsonObject jsonObject =  jsonArray.get(i).getAsJsonObject().get("_source").getAsJsonObject();
                        Gson gson = new Gson();
                        map = gson.fromJson(jsonObject,map.getClass());
                        //将索引id存入map集合
                        String id = jsonArray.get(i).getAsJsonObject().get("_id").toString();
                        map.put("id",id);
                        list.add(map);
                    }
                }
            }
            return new Page(pageNo, totalCount, pageSize, list);
        }
    
        /**
         * 更新es索引数据,索引中有联合索引,需调用该方法进行数据更新
         * @param map 集合
         * @param indexName 索引名称
         * @param indexType 索引类型
         */
        public boolean updateEsData(String indexName, String indexType, Map<String, Object> map, String[] fields, String[] index ) {
            //存储数据
            Map<String, Object> hmap = new HashMap<String, Object>();
            for (int j = 0; j < fields.length; j++) {
                if (fields[j].contains(":")) {
                    //分割联合字段
                    String[] sa = fields[j].split(":");
                    StringBuffer sb = new StringBuffer();
                    for (String st : sa) {
                        //联合字段,取值拼接
                        sb.append(map.get(st) + ";");
                    }
                    hmap.put(index[j], sb.toString());
                } else {
                    hmap.put(index[j], map.get(fields[j]));
                }
            }
            //针对car_model索引 添加 年款style_year_suffix  默认数据全部符合规范2017格式
            if (indexName.equals("car_model")) {
                if (null != map.get("style_year")) {
                    hmap.put("style_year_suffix", map.get("style_year").toString().substring(2, map.get("style_year").toString().length()));
                }
            }
            //调用es更新方法
            boolean bool = updateIndexDoc(indexName,indexType,hmap);
            return bool;
        }
    
        public static void main(String[] args) {
    //        EsService esService = new EsService();
    //        Boolean bool = esService.deleteIndexDocBySearch("indexName","type","name:张三");
    //        esService.baseSearch("wms_goods","wms_goods","","");
        }
    
    }
    
    
    
    
    展开全文
  • Springboot自动注入jestclient为空,此时需要手动注入 public JestClient getJestClinet(String ip){ JestClientFactory factory = new JestClientFactory(); String url = "http://$s"; factory....

    Springboot 自动注入 Jestclient 为空,此时需要手动注入

    public JestClient getJestClient(String ip){
    		JestClientFactory factory = new JestClientFactory();
    		String url = "http://$s";
    		factory.setHttpClientConfig(new HttpClientConfig
    				.Builder(String.format(url,ip))
    				.multiThreaded(true)
    				.build());
    		return  factory.getObject();
    	}
    

    测试方法

    private static JestClient jestClient;
    // 如果 @Value(“${jedis.ip}”) 的值为 null, 使用此方法读取配置
    private static Final String jedisIp = Constants.jedisIp;
    
    ...
    
    public void test() {
      jestClient = getJestClient(jedisIp);
    }
    

    Constants.java

    @Component
    public class Constants {
        private static String jedisIp;
        
        @Value("${jedis.ip}")
        public void setJedisIp(String jedisIp) {
          Constants.jedisIp= jedisIp;
        }
    }
    

    application.yml

    jedis:
      ip: 127.0.0.1:9200
    
    展开全文
  • 使用jestClient查询ES常见问题

    千次阅读 2019-11-22 16:12:55
    使用jestClient查询ES常见问题 使用JestClient快速查询ES 众所周知,ES提供连接的方法有两种,一种是HTTP请求方式,一种是TCP/IP方式,ES对这两种方式提供了不同的接口,可以在ES的配置文件中查看详细端口号,在此不...

    使用jestClient查询ES常见问题

    使用JestClient快速查询ES

    众所周知,ES提供连接的方法有两种,一种是HTTP请求方式,一种是TCP/IP方式,ES对这两种方式提供了不同的接口,可以在ES的配置文件中查看详细端口号,在此不在赘述。
    现在微服务的全家桶来说,基于spring boot和spring cloud很多情况下需要使用ES中间件进行存储一些数据或者是实现一些功能,比如分词、改造队列等等。所以spring也有相应的spring-data-ElasticSeach组件包连接ES进行相应的操作,其底层是使用了TCP/IP进行连接的,因为ES之后的版本TCP/IP的连接方式将不再支持,所以我们对新版本的ES使用JestClient进行连接。

    开始我们的查询旅途吧~
    首先加入我们的jestClient和ES的依赖

    		<dependency>
                <groupId>io.searchbox</groupId>
                <artifactId>jest</artifactId>
                <version>5.3.4</version>
            </dependency>
    		<dependency>
    			<groupId>org.elasticsearch</groupId>
    			<artifactId>elasticsearch</artifactId>
                <version>6.3.2</version>
    		</dependency>
    

    指定我们的ES服务地址,当然是http连接的端口地址

    spring.elasticsearch.jest.uris=http://127.0.0.1:9200
    

    接下来我们写一个简单精确查询的例子,注意如果是要查询英文,所传入的值应该做小写处理,不然搜不到

    import io.searchbox.client.JestClient;
    import io.searchbox.client.JestResult;
    import io.searchbox.core.Search;
    import org.elasticsearch.index.query.QueryBuilders;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.springframework.beans.factory.annotation.Autowired;
    
    import java.io.IOException;
    
    public class test {
        @Autowired
        JestClient jestClient;
    
        public void seach() throws IOException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.termQuery("字段名","123"));
            Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    }
    

    然后使用jestResult获取各种你想要的查询值
    在这里插入图片描述
    要想改变搜索条件,那么参考下面的方法

    模糊搜索,*为通配符

    public void seach() throws IOException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.wildcardQuery("字段名","*123*"));
            Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    

    模糊搜索+分页请求,使用方法类似于sql里的limit

    public void seach() throws IOException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.wildcardQuery("字段名","*123*"));
            searchSourceBuilder.from(0).size(10);
            Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    

    搜索排序,其中排序方法使用org.elasticsearch.search.sort.SortOrder里的方法

    public void seach() throws IOException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.wildcardQuery("字段名","*123*"));
            searchSourceBuilder.from(0).size(10).sort("排序字段名", SortOrder.DESC);
            Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    

    区间查询,其中rangeQuery也可只传起始值,或者只传终值,就是大于小于的意思

    public void seach() throws IOException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.rangeQuery("字段名").from("起始值").to("终值"));
            Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    

    多重条件查询,在这里就需要注意了,我们往往会遇到多种条件一起查询的场景。由于每一次调用SearchSourceBuilder的query方法都会进行重新覆盖对象,也就是说SearchSourceBuilder只会使用最后一次query方法获取的查询语句,所以当多重条件执行的时候使用boolQueryBuilder的must函数进行条件的拼接可以完美解决这个问题。

     public void Search(String id,String phone, Date startTime, Date endTime, Integer pageNum, Integer pageSize) throws ParseException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            if (id!=null){
                boolQueryBuilder.must(QueryBuilders.termQuery("id", id.toLowerCase()));
            }
            if (phone!=null){
                boolQueryBuilder.must(QueryBuilders.wildcardQuery("phone","*"+phone+"*"));
            }
            if (startTime!=null||endTime!=null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("DateTime").from(startTime).to(endTime));
            }
            if (pageNum!=null&&pageNum!=0){
                searchSourceBuilder.from((pageNum-1)*pageSize).size(pageSize).sort("DateTime", SortOrder.DESC);
            }
            searchSourceBuilder.query(boolQueryBuilder);
         	Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    

    因为ES的特殊性,我们往往查询的时候会查出很多数据,所以ES在不加入size的查询请求时只会默认返回10条数据,所以我们一般查询指定条件的时候都会加上限制个数(size)的条件.

    根据phone字段去重查询

     public void Search(String id,String phone, Date startTime, Date endTime, Integer pageNum, Integer pageSize) throws ParseException {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            if (id!=null){
                boolQueryBuilder.must(QueryBuilders.termQuery("id", id.toLowerCase()));
            }
            if (phone!=null){
                boolQueryBuilder.must(QueryBuilders.wildcardQuery("phone","*"+phone+"*"));
            }
            if (startTime!=null||endTime!=null){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("DateTime").from(startTime).to(endTime));
            }
            if (pageNum!=null&&pageNum!=0){
                searchSourceBuilder.from((pageNum-1)*pageSize).size(pageSize).sort("DateTime", SortOrder.DESC);
            }
            searchSourceBuilder.query(boolQueryBuilder);
            searchSourceBuilder.aggregation(AggregationBuilders.cardinality("phone_aggs").field("phone.keyword"));
        	Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
            JestResult jestResult = jestClient.execute(search);
        }
    

    看到这里你应该对通过jestClient查询ES小有心得了,快去实现吧。

    展开全文
  • JestClient实现对ElasticSearch的操作

    千次阅读 2018-11-21 19:53:59
    JestClient实现对ElasticSearch的操作 JestClient通过写json来实现对ElasticSearch的操作, 使用jestClient比较明显的一个优势,不用因为es的版本升级导致API发生改变而更改代码。 &amp;amp;amp;amp;amp;amp;amp;...
  • 由于springboot 2.3.0以后版本不支持自动注入JestClient,如下图我们在yml文件中配置JestClient时会出现划掉的线提示。我们采取手动配置的方式 采用手动注入的方式: @Test void contextLoads() { Jest...
  • springboot 2.3.0以后版本不支持自动注入JestClient,下图我们在yml文件中配置JestClient时会出现划掉的线提示。我们采取手动配置的方式 在测试类中手动创建JestClient public JestClient getJestCline(){ ...
  • JestClient之HTTP长连接判断

    千次阅读 2018-05-16 18:09:55
    目的: 判断jestclient连接ES集群时,是HTTP短连接,还是HTTP长连接。 为什么要判断: 因为如果是短连接,我们需要连接池控制连接数,防止大量请求,重复创建连接导致系统崩溃...通过jestclient向ES集群里重复连续...
  • 操作Elasticsearch的客户端有很多,SpringBoot也提供了方式去操作,这里介绍另外一种方式去使用Elasticsearch — JestClient JestClient是一款基于HTTP方式操作的Elasticsearch的客户端,支持同步和异步操作,同时也...
  • 使用Jest client pom.xml <!--jest--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.5.4</version&...
  • @springboot 2.3.0以后版本使用JestClient操作es 由于springboot 2.3.0以后版本不支持自动注入JestClient 解决方案:手动创建配置类 pom.xml 配置类 @Configuration public class MyJestClient { @Bean public ...
  • Java使用JestClient操作ElasticSearch

    千次阅读 2019-02-21 14:46:59
    package cn.xgs.JestClient; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; impor....
  • jestClient-ES客户端配置

    2020-12-19 18:29:20
    public JestClient getClientConfig() { JestClientFactory factory = new JestClientFactory(); //集群可以写多个节点,查询的时候不管配置一个还是多个节点,都会到整个集群中去查 //List<String> ...
  • SpringBoot+JestClient操作ElasticSearch

    千次阅读 2019-09-13 23:32:46
    实例使用SpringBoot结合JestClient组件操作ElasticSearch, 实现全文搜索,精确搜索,区间搜索,创建索引,新增数据,查询数据,批量新增数据,全文搜索,删除索引,删除数据等功能, 由于JestClient版本与ElasticSearch之间的...
  • /** * @author LiHaitao * @description JestClientTest: * @date 2019/7/11 15:18 **/ @SuppressWarnings("all") public class JestClientTest { private JestClient jestClient{ Article artic...
  • 在项目开发中,经常会遇到在创建索引的时候,直接把mapping和setting创建到索引中,使用jestClient api时如何创建index,如下代码: import io.searchbox.client.JestClient; import io.searchbox.client.JestResult...
  • JestClient 查询API一、背景二、Elasticsearch-sql插件的形式去操作三、JestClient的API形式去操作 一、背景 公司技术架构中用到的是Elasticsearch5.6.9,写业务时用SpringBoot整合JestClient操作ES挺方便的,并且...
  • 整合完成JestClient,查询出ES中所有的索引。 2.首先我们来看一看什么是JestClient Jest 是ElasticSearch 的java Http Rest 客户端。 他主要是让我们更在使用java执行es语法的时候更方法,他给我们很多的封闭。 3....
  • 使用JestClient操作ElasticSearch

    千次阅读 2018-08-02 23:57:45
    使用JestClient操作ElasticSearch 2017年07月12日 15:51:46 阅读数:9840 可参考: https://www.blog-china.cn/template/documentHtml/1484101683485.html ...
  • 一、背景: 最近在看使用jestClient连接es集群,从网上查询了很多,没有找到解决方法,于是乎扒拉jestClient源码研究了一番,项目中使用了老版本的jest-2.4.0.jar和jest-common-2.4.0.jar 源码: ...
  • 04-Elasticsearch的 JestClient 1. 介绍 任何使用过Elasticsearch的人都知道,使用基于rest的搜索API构建查询可能是单调乏味且容易出错的。 在本教程中,我们将研究Jest,一个用于Elasticsearch的HTTP Java客户端。...
  • 背景:之前java项目是springboot的,使用到es查询一直用jestClient,这次springboot升级到2.3.0后发现jestClient自动注入报错了,发现不支持jestClient自动注入了 解决方法有两个思路: 1、手动创建jestClient,...
  • Spring Boot 整合 StringRedisTemplate + JestClient + JPA 注意:本篇博客风格(不多比比就是撸代码!!!) 一、Spring Boot 整合 redis (StringRedisTemplate) 1.maven依赖 &amp;amp;amp;amp;amp;amp;amp;amp...
  • 通过JestClient实现ES的updateByQuery

    千次阅读 2019-08-27 22:30:28
    一、通过jestClient连接elasticSearch 连接es有很多种方式,大多数使用情况采用封装好的client,里面有很多现成的API能够调用,但是Es的版本更新太快,每个版本都兼容性很差,但是通过jestClient操作es,再写类似于...
  • jestClient获取elasticsearch中所有索引。

    千次阅读 2019-04-17 10:45:34
    如何在java程序中,通过jestClient获取es所有索引 在网上搜了半天,也没搜到结果,很久没写博客了,继续记录自己开发中的一些问题和一些心得吧。 我们都知道,直接获取es的所有索引是可以通过http交互的,例如: ...
  • es结合jestClient完成对数据操作

    千次阅读 2019-11-15 08:57:17
    package cn.xgs.JestClient; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.Map; import org.ela....
  • @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) @ActiveProfiles("offline") public class JestClientTest { ... public JestClient jestClient; /** * 查询文档...
  • 当应用程序空闲(不向elasticsearch发送任何请求)一段时间后,JestClient就会抛出SocketTImeoutException.第二次请求很快就相应. 这是我之前的配置 spring.elasticsearch.jest.uris=http://x.x.x.x:9200 spring....
  • JestClient 是一款基于 HTTP 方式操作的 Elasticsearch 的客户端,支持同步和异步操作,同时也可以结合 ElasticSearch 的依赖进行操作 Elasticsearch。

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 3,081
精华内容 1,232
关键字:

JestClient