Elasticsearch的Java HTTP Rest客户端。

1、是什么

它是Elasticsearch的Java HTTP Rest客户端。

2、实例

(1)User.java

[html]  view plain  copy
  1. public class User {  
  2.   
  3.     @JestId  
  4.     private Integer id;  
  5.     private String name;  
  6.     private Date birth;  
  7.     public Integer getId() {  
  8.         return id;  
  9.     }  
  10.     public void setId(Integer id) {  
  11.         this.id = id;  
  12.     }  
  13.     public String getName() {  
  14.         return name;  
  15.     }  
  16.     public void setName(String name) {  
  17.         this.name = name;  
  18.     }  
  19.     public Date getBirth() {  
  20.         return birth;  
  21.     }  
  22.     public void setBirth(Date birth) {  
  23.         this.birth = birth;  
  24.     }  
  25.     public User() {  
  26.         super();  
  27.         // TODO Auto-generated constructor stub  
  28.     }  
  29.     public User(Integer id, String name, Date birth) {  
  30.         super();  
  31.         this.id = id;  
  32.         this.name = name;  
  33.         this.birth = birth;  
  34.     }  
  35.     @Override  
  36.     public String toString() {  
  37.         return "User [id=" + id + "name=" + name + "birth=" + birth + "]";  
  38.     }  
  39. }  

(2)JestService.java

[html]  view plain  copy
  1. public class JestService {  
  2.   
  3.     /**  
  4.      * 获取JestClient对象  
  5.      * @return  
  6.      */  
  7.     public JestClient getJestClient() {    
  8.               
  9.         JestClientFactory factory = new JestClientFactory();  
  10.         factory.setHttpClientConfig(new HttpClientConfig  
  11.                                .Builder("http://localhost:9200")  
  12.                                .gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'hh:mm:ss").create())  
  13.                                .connTimeout(1500)  
  14.                                .readTimeout(3000)  
  15.                                .multiThreaded(true)  
  16.                                .build());  
  17.         return factory.getObject();  
  18.         }  
  19.       
  20.     /**  
  21.      * 创建索引  
  22.      * @param jestClient  
  23.      * @param indexName  
  24.      * @return  
  25.      * @throws Exception  
  26.      */  
  27.     public boolean createIndex(JestClient jestClient, String indexName) throws Exception {  
  28.           
  29.         JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());  
  30.         return jr.isSucceeded();  
  31.     }  
  32.       
  33.     /**  
  34.      * Put映射  
  35.      * @param jestClient  
  36.      * @param indexName  
  37.      * @param typeName  
  38.      * @param source  
  39.      * @return  
  40.      * @throws Exception  
  41.      */  
  42.     public boolean createIndexMapping(JestClient jestClient, String indexName, String typeName, String source) throws Exception {  
  43.   
  44.         PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();  
  45.         JestResult jr = jestClient.execute(putMapping);  
  46.         return jr.isSucceeded();  
  47.         }  
  48.       
  49.     /**  
  50.      * Get映射  
  51.      * @param jestClient  
  52.      * @param indexName  
  53.      * @param typeName  
  54.      * @return  
  55.      * @throws Exception  
  56.      */  
  57.     public String getIndexMapping(JestClient jestClient, String indexName, String typeName) throws Exception {  
  58.   
  59.         GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();  
  60.         JestResult jr = jestClient.execute(getMapping);  
  61.         return jr.getJsonString();  
  62.         }  
  63.       
  64.     /**  
  65.      * 索引文档  
  66.      * @param jestClient  
  67.      * @param indexName  
  68.      * @param typeName  
  69.      * @param objs  
  70.      * @return  
  71.      * @throws Exception  
  72.      */  
  73.     public boolean index(JestClient jestClient, String indexName, String typeName, List<Object> objs) throws Exception {  
  74.           
  75.         Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);  
  76.         for (Object obj : objs) {  
  77.           Index index = new Index.Builder(obj).build();  
  78.           bulk.addAction(index);  
  79.         }  
  80.         BulkResult br = jestClient.execute(bulk.build());  
  81.         return br.isSucceeded();  
  82.         }  
  83.       
  84.     /**  
  85.      * 搜索文档  
  86.      * @param jestClient  
  87.      * @param indexName  
  88.      * @param typeName  
  89.      * @param query  
  90.      * @return  
  91.      * @throws Exception  
  92.      */  
  93.     public SearchResult search(JestClient jestClient, String indexName, String typeName, String query) throws Exception {  
  94.           
  95.         Search search = new Search.Builder(query)  
  96.             .addIndex(indexName)  
  97.             .addType(typeName)  
  98.             .build();  
  99.         return jestClient.execute(search);  
  100.         }  
  101.       
  102.     /**  
  103.      * Count文档  
  104.      * @param jestClient  
  105.      * @param indexName  
  106.      * @param typeName  
  107.      * @param query  
  108.      * @return  
  109.      * @throws Exception  
  110.      */  
  111.     public Double count(JestClient jestClient, String indexName, String typeName, String query) throws Exception {  
  112.   
  113.         Count count = new Count.Builder()  
  114.             .addIndex(indexName)  
  115.             .addType(typeName)  
  116.             .query(query)  
  117.             .build();  
  118.         CountResult results = jestClient.execute(count);   
  119.         return results.getCount();  
  120.     }  
  121.       
  122.     /**  
  123.      * Get文档  
  124.      * @param jestClient  
  125.      * @param indexName  
  126.      * @param typeName  
  127.      * @param id  
  128.      * @return  
  129.      * @throws Exception  
  130.      */  
  131.     public JestResult get(JestClient jestClient, String indexName, String typeName, String id) throws Exception {  
  132.             
  133.         Get get = new Get.Builder(indexName, id).type(typeName).build();  
  134.         return jestClient.execute(get);  
  135.     }  
  136.       
  137.     /**  
  138.      * Delete索引  
  139.      * @param jestClient  
  140.      * @param indexName  
  141.      * @return  
  142.      * @throws Exception  
  143.      */  
  144.     public boolean delete(JestClient jestClient, String indexName) throws Exception {  
  145.   
  146.         JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());  
  147.         return jr.isSucceeded();  
  148.     }  
  149.       
  150.     /**  
  151.      * Delete文档  
  152.      * @param jestClient  
  153.      * @param indexName  
  154.      * @param typeName  
  155.      * @param id  
  156.      * @return  
  157.      * @throws Exception  
  158.      */  
  159.     public boolean delete(JestClient jestClient, String indexName, String typeName, String id) throws Exception {  
  160.           
  161.         DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());  
  162.         return dr.isSucceeded();  
  163.     }  
  164.       
  165.     /**  
  166.      * 关闭JestClient客户端  
  167.      * @param jestClient  
  168.      * @throws Exception  
  169.      */  
  170.     public void closeJestClient(JestClient jestClient) throws Exception {  
  171.           
  172.         if (jestClient != null) {  
  173.           jestClient.shutdownClient();  
  174.         }  
  175.         }  
  176. }  
(3)UserTest.java

[html]  view plain  copy
  1. public class UserTest {  
  2.   
  3.     private JestService jestService;  
  4.         private JestClient jestClient;  
  5.         private String indexName = "hwd";  
  6.         private String typeName = "user";  
  7.         
  8.     @Before  
  9.     public void setUp() throws Exception {  
  10.           
  11.         jestService = new JestService();  
  12.         jestClient = jestService.getJestClient();  
  13.     }  
  14.   
  15.     @After  
  16.     public void tearDown() throws Exception {  
  17.           
  18.         jestService.closeJestClient(jestClient);  
  19.     }  
  20.   
  21.     @Test  
  22.         public void createIndex() throws Exception {  
  23.       
  24.         boolean result = jestService.createIndex(jestClient, indexName);  
  25.         System.out.println(result);  
  26.         }  
  27.       
  28.     @Test  
  29.         public void createIndexMapping() throws Exception {  
  30.       
  31.         String source = "{\"" + typeName + "\":{\"properties\":{"  
  32.                         + "\"id\":{\"type\":\"integer\"}"  
  33.                         + ",\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"}"  
  34.                         + ",\"birth\":{\"type\":\"date\",\"format\":\"strict_date_optional_time||epoch_millis\"}"  
  35.                         + "}}}";  
  36.             System.out.println(source);  
  37.             boolean result = jestService.createIndexMapping(jestClient, indexName, typeName, source);  
  38.             System.out.println(result);  
  39.         }  
  40.       
  41.     @Test  
  42.     public void getIndexMapping() throws Exception {  
  43.           
  44.         String result = jestService.getIndexMapping(jestClient, indexName, typeName);  
  45.         System.out.println(result);  
  46.     }  
  47.       
  48.     @Test  
  49.     public void index() throws Exception {  
  50.           
  51.         List<Object> objs = new ArrayList<Object>();  
  52.         objs.add(new User(1, "T:o\"m-", new Date()));  
  53.         objs.add(new User(2, "J,e{r}r;y:", new Date()));  
  54.         boolean result = jestService.index(jestClient, indexName, typeName, objs);  
  55.         System.out.println(result);  
  56.     }  
  57.       
  58.     @Test  
  59.     public void termQuery() throws Exception {  
  60.           
  61.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  62.         QueryBuilder queryBuilder = QueryBuilders  
  63.             .termQuery("name", "T:o\"m-");//单值完全匹配查询  
  64.         searchSourceBuilder.query(queryBuilder);  
  65.         searchSourceBuilder.size(10);  
  66.         searchSourceBuilder.from(0);  
  67.         String query = searchSourceBuilder.toString();   
  68.         System.out.println(query);  
  69.         SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
  70.         List<Hit<User, Void>> hits = result.getHits(User.class);  
  71.         System.out.println("Size:" + hits.size());  
  72.         for (Hit<User, Void> hit : hits) {  
  73.           User user = hit.source;  
  74.           System.out.println(user.toString());  
  75.         }  
  76.     }  
  77.         
  78.     @Test  
  79.     public void termsQuery() throws Exception {  
  80.           
  81.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  82.         QueryBuilder queryBuilder = QueryBuilders  
  83.             .termsQuery("name", new String[]{ "T:o\"m-", "J,e{r}r;y:" });//多值完全匹配查询  
  84.         searchSourceBuilder.query(queryBuilder);  
  85.         searchSourceBuilder.size(10);  
  86.         searchSourceBuilder.from(0);  
  87.         String query = searchSourceBuilder.toString();   
  88.         System.out.println(query);  
  89.         SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
  90.         List<Hit<User, Void>> hits = result.getHits(User.class);  
  91.         System.out.println("Size:" + hits.size());  
  92.         for (Hit<User, Void> hit : hits) {  
  93.           User user = hit.source;  
  94.           System.out.println(user.toString());  
  95.         }  
  96.     }  
  97.         
  98.     @Test  
  99.     public void wildcardQuery() throws Exception {  
  100.           
  101.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  102.         QueryBuilder queryBuilder = QueryBuilders  
  103.             .wildcardQuery("name", "*:*");//通配符和正则表达式查询  
  104.         searchSourceBuilder.query(queryBuilder);  
  105.         searchSourceBuilder.size(10);  
  106.         searchSourceBuilder.from(0);  
  107.         String query = searchSourceBuilder.toString();      
  108.         System.out.println(query);  
  109.         SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
  110.         List<Hit<User, Void>> hits = result.getHits(User.class);  
  111.         System.out.println("Size:" + hits.size());  
  112.         for (Hit<User, Void> hit : hits) {  
  113.           User user = hit.source;  
  114.           System.out.println(user.toString());  
  115.         }  
  116.     }  
  117.         
  118.     @Test  
  119.     public void prefixQuery() throws Exception {  
  120.           
  121.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  122.         QueryBuilder queryBuilder = QueryBuilders  
  123.             .prefixQuery("name", "T:o");//前缀查询  
  124.         searchSourceBuilder.query(queryBuilder);  
  125.         searchSourceBuilder.size(10);  
  126.         searchSourceBuilder.from(0);  
  127.         String query = searchSourceBuilder.toString();      
  128.         System.out.println(query);  
  129.         SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
  130.         List<Hit<User, Void>> hits = result.getHits(User.class);  
  131.         System.out.println("Size:" + hits.size());  
  132.         for (Hit<User, Void> hit : hits) {  
  133.           User user = hit.source;  
  134.           System.out.println(user.toString());  
  135.         }  
  136.     }  
  137.         
  138.     @Test  
  139.     public void rangeQuery() throws Exception {  
  140.           
  141.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  142.         QueryBuilder queryBuilder = QueryBuilders  
  143.             .rangeQuery("birth")  
  144.             .gte("2016-09-01T00:00:00")  
  145.             .lte("2016-10-01T00:00:00")  
  146.             .includeLower(true)  
  147.             .includeUpper(true);//区间查询  
  148.         searchSourceBuilder.query(queryBuilder);  
  149.         searchSourceBuilder.size(10);  
  150.         searchSourceBuilder.from(0);  
  151.         String query = searchSourceBuilder.toString();  
  152.         System.out.println(query);  
  153.         SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
  154.         List<Hit<User, Void>> hits = result.getHits(User.class);  
  155.         System.out.println("Size:" + hits.size());  
  156.         for (Hit<User, Void> hit : hits) {  
  157.           User user = hit.source;  
  158.           System.out.println(user.toString());  
  159.         }  
  160.     }  
  161.         
  162.     @Test  
  163.     public void queryString() throws Exception {  
  164.           
  165.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  166.         QueryBuilder queryBuilder = QueryBuilders  
  167.             .queryString(QueryParser.escape("T:o\""));//文本检索,应该是将查询的词先分成词库中存在的词,然后分别去检索,存在任一存在的词即返回,查询词分词后是OR的关系。需要转义特殊字符  
  168.         searchSourceBuilder.query(queryBuilder);  
  169.         searchSourceBuilder.size(10);  
  170.         searchSourceBuilder.from(0);  
  171.         String query = searchSourceBuilder.toString();   
  172.         System.out.println(query);  
  173.         SearchResult result = jestService.search(jestClient, indexName, typeName, query);  
  174.         List<Hit<User, Void>> hits = result.getHits(User.class);  
  175.         System.out.println("Size:" + hits.size());  
  176.         for (Hit<User, Void> hit : hits) {  
  177.           User user = hit.source;  
  178.           System.out.println(user.toString());  
  179.         }  
  180.     }  
  181.         
  182.     @Test  
  183.     public void count() throws Exception {  
  184.           
  185.         String[] name = new String[]{ "T:o\"m-", "Jerry" };  
  186.         String from = "2016-09-01T00:00:00";  
  187.         String to = "2016-10-01T00:00:00";  
  188.         SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();  
  189.         QueryBuilder queryBuilder = QueryBuilders.boolQuery()  
  190.             .must(QueryBuilders.termsQuery("name", name))  
  191.             .must(QueryBuilders.rangeQuery("birth").gte(from).lte(to));  
  192.         searchSourceBuilder.query(queryBuilder);  
  193.         String query = searchSourceBuilder.toString();   
  194.         System.out.println(query);  
  195.         Double count = jestService.count(jestClient, indexName, typeName, query);  
  196.         System.out.println("Count:" + count);  
  197.     }  
  198.         
  199.     @Test  
  200.     public void get() throws Exception {  
  201.           
  202.         String id = "2";  
  203.         JestResult result = jestService.get(jestClient, indexName, typeName, id);  
  204.         if (result.isSucceeded()) {  
  205.           User user = result.getSourceAsObject(User.class);  
  206.           System.out.println(user.toString());  
  207.         }  
  208.     }  
  209.           
  210.     @Test  
  211.     public void deleteIndexDocument() throws Exception {  
  212.           
  213.         String id = "2";  
  214.         boolean result = jestService.delete(jestClient, indexName, typeName, id);  
  215.         System.out.println(result);  
  216.     }  
  217.       
  218.     @Test  
  219.     public void deleteIndex() throws Exception {  
  220.           
  221.         boolean result = jestService.delete(jestClient, indexName);  
  222.         System.out.println(result);  
  223.     }  
  224. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值