Elasticsearch JestClient 使用
生活随笔
收集整理的這篇文章主要介紹了
Elasticsearch JestClient 使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、索引操作
//創(chuàng)建索引public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 創(chuàng)建索引CreateIndex createIndex = new CreateIndex.Builder("my_index").build();JestResult result = jestClient.execute(createIndex);// 5. 輸出創(chuàng)建結(jié)果System.out.println(result.getJsonString());} //刪除索引public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 刪除索引DeleteIndex deleteIndex = new DeleteIndex.Builder("my_index").build();JestResult result = jestClient.execute(deleteIndex);// 5. 輸出創(chuàng)建結(jié)果System.out.println(result.getJsonString());} //設(shè)置Mappingpublic static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 創(chuàng)建 json 格式的 mapping/*** {* "mappings":{* "properties":{* "field1":{* "type":"keyword"* },* "field2":{* "type":"byte"* }* }* }* }*/Map<String, Object> map = new HashMap<String, Object>() {{this.put("mappings", new HashMap<String, Object>() {{this.put("properties", new HashMap<String, Object>() {{this.put("name", new HashMap<String, String>() {{//this.put("type", "keyword");}});this.put("age", new HashMap<String, String>() {{//this.put("type", "integer");}});}});}});}};String mapping = JSONObject.toJSONString(map);// 5. 創(chuàng)建索引// PutMapping putMapping = new PutMapping.Builder(indexName, indexType, indexMapping).build();CreateIndex createIndex = new CreateIndex.Builder("my_index").settings(mapping).build();JestResult result = jestClient.execute(createIndex);// 6. 輸出創(chuàng)建結(jié)果System.out.println(result.getJsonString());} //獲取Mappingpublic static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 獲取 mappingGetMapping getMapping = new GetMapping.Builder().addIndex("my_index").build();JestResult result = jestClient.execute(getMapping);// 6. 輸出獲取結(jié)果System.out.println(result.getJsonString());}二、文檔操作
public class Book {@JestId // 自動(dòng)生成 id,private String id;private String name;private String author;private String desc;public Book(String id, String name, String author, String desc) {this.id = id;this.name = name;this.author = author;this.desc = desc;} } //新增/修改文檔public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 準(zhǔn)備數(shù)據(jù)Book book = new Book("001", "斗破蒼穹", "天蠶土豆", "這是斗氣的世界");// 4. 首先會(huì)判斷索引是否存在,不存在則根據(jù)文檔創(chuàng)建索引,然后判斷 id 是否存在,存在就是更新文檔Index index = new Index.Builder(book).index("my_index").type("_doc").build();JestResult result = jestClient.execute(index);// 5. 輸出創(chuàng)建結(jié)果System.out.println(result.getJsonString());} //批量新增public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 批量創(chuàng)建文檔Bulk.Builder bulk = new Bulk.Builder().defaultIndex("my_index").defaultType("_doc");List<Book> list = new ArrayList<Book>() {{this.add(new Book("001", "斗破蒼穹", "天蠶土豆", "這是斗氣的世界"));this.add(new Book("002", "完美世界", "辰東", "遮天前傳"));this.add(new Book("003", "盤(pán)龍", "我吃西紅柿", "神奇的戒指"));this.add(new Book("004", "誅仙", "蕭鼎", "天地不仁,以萬(wàn)物為芻狗"));this.add(new Book("005", "大主宰", "天蠶土豆", "大千世界,位面交匯"));}};for (Book book : list) {// 如未設(shè)置唯一 id 值,則唯一 id 會(huì)默認(rèn)生成,索引操作為添加操作Index index = new Index.Builder(book).build();bulk.addAction(index);}BulkResult result = jestClient.execute(bulk.build());System.out.println(result.getJsonString());} //根據(jù)id查詢(xún)文檔public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 查詢(xún)文檔Get get = new Get.Builder("my_index", "001").type("_doc").build();JestResult result = jestClient.execute(get);// 5. 輸出查詢(xún)結(jié)果System.out.println(result.getJsonString());} //根據(jù)條件查詢(xún)文檔public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 查詢(xún)文檔// 4.1 構(gòu)造查詢(xún)條件// 4.1.1 單 field 不分詞查詢(xún)// TermQueryBuilder termQueryBuilder = new TermQueryBuilder(fieldName, value);// 4.1.2 單 field 多詞不分詞查詢(xún)// TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("", "value1", "value2");// 4.1.3 單 field 分詞查詢(xún)// MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(fieldName, value);// 4.1.4 多 field 分詞查詢(xún)MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("斗氣", "desc");// 轉(zhuǎn)化為 ES 查詢(xún),默認(rèn)分詞后 and 連接,可使用 defaultOperator(Operator.AND) 修改SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);// 4.2 構(gòu)造查詢(xún)Search search = new Search.Builder(query.toString()).build();// 4.3 執(zhí)行查詢(xún)JestResult result = jestClient.execute(search);// 5. 輸出查詢(xún)結(jié)果System.out.println(result.getJsonString());} //區(qū)間搜索public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4.1 構(gòu)建區(qū)間搜索條件RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");// 4.2 解析為 ES 查詢(xún)SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder);// 4.3 構(gòu)建查詢(xún)Search search = new Search.Builder(query.toString()).build();// 4.4 執(zhí)行查詢(xún)JestResult result = jestClient.execute(search);// 5. 輸出查詢(xún)結(jié)果System.out.println(result.getJsonString());} //刪除文檔public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4. 刪除文檔Delete delete = new Delete.Builder("001").index("my_index").type("_doc").build();JestResult result = jestClient.execute(delete);// 5. 輸出刪除結(jié)果System.out.println(result.getJsonString());} //分頁(yè)public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4.1 構(gòu)建區(qū)間搜索條件RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");// 4.2 解析為 ES 查詢(xún)并添加分頁(yè)SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder).from(0).size(1);// 4.3 構(gòu)建查詢(xún)Search search = new Search.Builder(query.toString()).build();// 4.4 執(zhí)行查詢(xún)JestResult result = jestClient.execute(search);// 5. 輸出查詢(xún)結(jié)果System.out.println(result.getJsonString());} //高亮public static void main(String[] args) throws IOException {// 1. 創(chuàng)建 ES 連接池JestClientFactory jestClientFactory = new JestClientFactory();// 2. 配置 ES 信息HttpClientConfig config = new HttpClientConfig.Builder("http://127.0.0.1:9200").build();jestClientFactory.setHttpClientConfig(config);// 3. 獲取 ES 連接JestClient jestClient = jestClientFactory.getObject();// 4.1 構(gòu)建搜索條件MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("天世界", "author", "desc");// 4.2 轉(zhuǎn)化為 ES 搜索SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);// 4.3 配置高亮HighlightBuilder highlightBuilder = new HighlightBuilder();highlightBuilder.field("author");highlightBuilder.field("desc");highlightBuilder.preTags("<em>").postTags("</em>");highlightBuilder.fragmentSize(500);query.highlighter(highlightBuilder);// 4.4 構(gòu)建查詢(xún)Search search = new Search.Builder(query.toString()).build();// 4.5 執(zhí)行查詢(xún), 使用 SearchResult 接收SearchResult result = jestClient.execute(search);// 5.1 遍歷查詢(xún)結(jié)果List<SearchResult.Hit<Book, Void>> hits = result.getHits(Book.class);for (SearchResult.Hit<Book, Void> hit : hits) {// 5.2 獲取高亮集合Map<String, List<String>> highlight = hit.highlight;for (String s : highlight.keySet()) {// 5.3 輸出高亮結(jié)果System.out.println("高亮顯示:" + highlight.get(s).get(0));}}}.三、導(dǎo)入包及pom文件引包
import com.alibaba.fastjson.JSONObject; import com.sdcqjy.cloud.common.util.JsonUtils; import io.searchbox.client.JestClient; import io.searchbox.client.JestClientFactory; import io.searchbox.client.JestResult; import io.searchbox.client.config.HttpClientConfig; import io.searchbox.core.Bulk; import io.searchbox.core.BulkResult; import io.searchbox.core.Delete; import io.searchbox.core.Get; import io.searchbox.core.Index; import io.searchbox.core.Search; import io.searchbox.core.SearchResult; import io.searchbox.indices.CreateIndex; import io.searchbox.indices.DeleteIndex; import io.searchbox.indices.mapping.GetMapping; import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;<dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId><version>6.3.1</version> </dependency>總結(jié)
以上是生活随笔為你收集整理的Elasticsearch JestClient 使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 涨知识了!阿里规定超过三张表禁止join
- 下一篇: uniapp自定义导航栏与手机状态栏重叠