筆者在進行Elasticsearch開發過程當中,經過API的不一樣比較,選擇了Rest API:Jest。而後在牆內百度Jest的示例代碼時,發現不少博客的示例都是最簡單的例子,沒法知足真正生產環境的應用。基於此,筆者寫這篇博客,將經過Jest實現的ES交互典型示例總結以下:git
經過百度獲取的Jest API示例中,對於索引建立的代碼,都是不帶settings和mappings的,生產環境開發時,不知足條件,下面是對於帶具體settings和mappings的代碼示例:github
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 = "{\n" + 11 " \"settings\" : {\n" + 12 " \"number_of_shards\" : 8\n" + 13 " },\n" + 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 }
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 }
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 }
筆者上面的代碼採用的jar包時 jest-2.0.0.jar,對於不一樣版本的jar包,上述代碼中具體一些方法可能出現不一樣,這個須要讀者本身注意。api
另外,強烈建議FORK下載Github上的Jest項目,可能不須要關注其源碼,但項目自帶的單元測試代碼,幾乎涵蓋了Jest對於ES的各種操做安全
Github地址:https://github.com/searchbox-io/Jestapp