笔者在进行Elasticsearch开发过程中,通过API的不同比较,选择了Rest API:Jest。然后在墙内百度Jest的示例代码时,发现很多博客的示例都是最简单的例子,无法满足真正生产环境的应用。基于此,笔者写这篇博客,将通过Jest实现的ES交互典型示例总结如下:
1 创建索引
通过百度获取的Jest API示例中,对于索引创建的代码,都是不带settings和mappings的,生产环境开发时,不满足条件,下面是对于带具体settings和mappings的代码示例:
1 /** 2 * 创建索引 3 * @param client 4 * @throws IOException 5 */ 6 public JestResult createIndex(JestClient client) throws IOException { 7 String index = "stringyone"; 8 String expectedType1Maping = 9 ""_source":{"enabled":false},"properties":{"field1":{"type":"keyword"}}"; 10 String settingsJson = "{ " + 11 " "settings" : { " + 12 " "number_of_shards" : 8 " + 13 " }, " + 14 " "mappings" : {"type1": {" + expectedType1Maping + "}}" + 15 "}"; 16 17 //此处settingsJson字符串就是settings 和 mappings 18 CreateIndex createIndex = new CreateIndex.Builder(index) 19 .settings(settingsJson) 20 .build(); 21 22 JestResult result = client.execute(createIndex); 23 return result; 24 }
2 带安全认证的ES连接创建
1 /** 2 * 获取JestClient对象 3 * @return 4 */ 5 private static JestClient getJestClient() { 6 JestClientFactory factory = new JestClientFactory(); 7 factory.setHttpClientConfig(new HttpClientConfig 8 .Builder("http://127.0.0.1:9200") 9 .defaultCredentials(username,password) 10 .gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss").create()) 11 .multiThreaded(true) 12 .readTimeout(10000) 13 .build()); 14 JestClient client = factory.getObject(); 15 return client; 16 }
3 条件查询
1 /** 2 * 带条件检索 3 * 实现类似 select * from table where filed = queryString AND (... or条件字段 ...) 4 * @param indexName 索引名 5 * @param typeName 类型名 6 * @param queryString 查询内容 7 * @param field 字段对象,包括查询字段及查询字段的值 8 * @param fieldForOrs 查询条件 or区分 9 * @param client 10 * @param pageNumber 页码 11 * @param pageSize 页数 12 * @return 13 */ 14 public JestResult index(String indexName, String typeName, String queryString, String field, List<Field> fieldForOrs, JestClient client, int pageNumber, int pageSize) { 15 //声明一个SearchSourceBuilder对象,构造检索请求体 16 SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 17 18 BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); 19 20 //构造查询哪个字段 21 if(StringUtils.isEmpty(field)) { 22 //没有检索条件,则全字段查询 23 boolQueryBuilder = boolQueryBuilder.must(QueryBuilders.queryStringQuery(queryString)); 24 } else { 25 boolQueryBuilder = boolQueryBuilder.must(QueryBuilders.matchQuery(field,queryString)); 26 } 27 28 BoolQueryBuilder innerQueryBuilder = QueryBuilders.boolQuery(); 29 for(Field fieldValue : fieldForOrs) { 30 innerQueryBuilder = innerQueryBuilder.should(QueryBuilders.termQuery(fieldValue.getFieldName(),fieldValue.getFieldValue())); 31 } 32 boolQueryBuilder = boolQueryBuilder.filter(innerQueryBuilder); 33 34 searchSourceBuilder.query(boolQueryBuilder); 35 36 //设置高亮字段 37 HighlightBuilder highlightBuilder = new HighlightBuilder(); 38 highlightBuilder.field(field); 39 highlightBuilder.preTags("<em>").postTags("</em>"); 40 highlightBuilder.fragmentSize(200); 41 searchSourceBuilder.highlight(highlightBuilder); 42 43 //设置分页 44 searchSourceBuilder.from((pageNumber - 1) * pageSize); 45 searchSourceBuilder.size(pageSize); 46 47 //构建Search对象 48 Search search = new Search.Builder(searchSourceBuilder.toString()) 49 .addIndex(indexName) 50 .addType(typeName) 51 .build(); 52 53 SearchResult searchResult = null; 54 try { 55 searchResult = client.execute(search); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 return searchResult; 60 }
4 总结建议
笔者上面的代码采用的jar包时 jest-2.0.0.jar,对于不同版本的jar包,上述代码中具体一些方法可能出现不同,这个需要读者自己注意。
另外,强烈建议FORK下载Github上的Jest项目,可能不需要关注其源码,但项目自带的单元测试代码,几乎涵盖了Jest对于ES的各类操作
Github地址:https://github.com/searchbox-io/Jest