1.1 简介
1.1.1 概述
Elasticsearch Java API 有四类 client 连接方式:TransportClient、 RestClient 、Jest、 Spring Data Elasticsearch。其中 TransportClient、RestClient 是 Elasticsearch 原生的 api,TransportClient 会在 8.0 版本中删除,替代的是 HighLevelRestClient,它使用 HTTP 请求而不是 Java 序列化请求。Spring Data Elasticsearch 是 Spring 集成的 Elasticsearch 开发包。本章介绍如何使用 JestClient 操作 Elasticsearch。
1.1.2 相关依赖
<!-- Elasticsearch HTTP 客户端 JestClient -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
<!-- 与使用版本保持一致 -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.2</version>
</dependency>
1.2 索引操作
1.2.1 创建索引
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 创建索引
*/
@SpringBootTest
public class ES {
@Test
public void create() throws IOException {
// 1. 创建 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. 创建索引
CreateIndex createIndex = new CreateIndex.Builder("my_index").build();
JestResult result = jestClient.execute(createIndex);
// 5. 输出创建结果
System.out.println(result.getJsonString());
}
}
1.2.2 删除索引
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 删除索引
*/
@SpringBootTest
public class ES {
@Test
public void delete() throws IOException {
// 1. 创建 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. 输出删除结果
System.out.println(result.getJsonString());
}
}
1.2.3 设置 Mapping
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 设置 mapping
*/
@SpringBootTest
public class ES {
@Test
public void setMapping() throws IOException {
// 1. 创建 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. 创建 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. 创建索引
// 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. 输出创建结果
System.out.println(result.getJsonString());
}
}
1.2.4 获取 Mapping
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 获取 mapping
*/
@SpringBootTest
public class ES {
@Test
public void getMapping() throws IOException {
// 1. 创建 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. 获取 mapping
GetMapping getMapping = new GetMapping.Builder().addIndex("my_index").build();
JestResult result = jestClient.execute(getMapping);
// 6. 输出获取结果
System.out.println(formatJson(result.getJsonString()));
}
}
1.3 文档操作
1.3.1 实体类
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc //TODO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BookDocument {
@JestId // 自动生成 id,
private String id;
private String name;
private String author;
private String desc;
}
1.3.2 新增/修改文档
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 添加文档
*/
@SpringBootTest
public class ES {
@Test
public void create() throws IOException {
// 1. 创建 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. 准备数据
BookDocument bookDocument = new BookDocument("001", "斗破苍穹", "天蚕土豆", "这是斗气的世界");
// 4. 首先会判断索引是否存在,不存在则根据文档创建索引,然后判断 id 是否存在,存在就是更新文档
Index index = new Index.Builder(bookDocument).index("my_index").type("_doc").build();
JestResult result = jestClient.execute(index);
// 5. 输出创建结果
System.out.println(formatJson(result.getJsonString()));
}
}
1.3.3 批量新增
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc //TODO
*/
@SpringBootTest
public class ES {
@Test
public void create() throws IOException {
// 1. 创建 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. 批量创建文档
Bulk.Builder bulk = new Bulk.Builder().defaultIndex("my_index").defaultType("_doc");
List<BookDocument> list = new ArrayList<BookDocument>() {{
this.add(new BookDocument("001", "斗破苍穹", "天蚕土豆", "这是斗气的世界"));
this.add(new BookDocument("002", "完美世界", "辰东", "遮天前传"));
this.add(new BookDocument("003", "盘龙", "我吃西红柿", "神奇的戒指"));
this.add(new BookDocument("004", "诛仙", "萧鼎", "天地不仁,以万物为刍狗"));
this.add(new BookDocument("005", "大主宰", "天蚕土豆", "大千世界,位面交汇"));
}};
for (BookDocument bookDocument : list) {
// 如未设置唯一 id 值,则唯一 id 会默认生成,索引操作为添加操作
Index index = new Index.Builder(bookDocument).build();
bulk.addAction(index);
}
BulkResult result = jestClient.execute(bulk.build());
System.out.println(formatJson(result.getJsonString()));
}
}
1.3.4 查询文档
☞ 根据 id 查询
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 根据 id 查询文档
*/
@SpringBootTest
public class ES {
@Test
public void search() throws IOException {
// 1. 创建 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. 查询文档
Get get = new Get.Builder("my_index", "001").type("_doc").build();
JestResult result = jestClient.execute(get);
// 5. 输出查询结果
System.out.println(formatJson(result.getJsonString()));
}
}
☞ 根据条件查询
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 条件查询
*/
@SpringBootTest
public class ES {
@Test
public void search() throws IOException {
// 1. 创建 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. 查询文档
// 4.1 构造查询条件
// 4.1.1 单 field 不分词查询
// TermQueryBuilder termQueryBuilder = new TermQueryBuilder(fieldName, value);
// 4.1.2 单 field 多词不分词查询
// TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder("", "value1", "value2");
// 4.1.3 单 field 分词查询
// MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(fieldName, value);
// 4.1.4 多 field 分词查询
MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("斗气", "desc");
// 转化为 ES 查询,默认分词后 and 连接,可使用 defaultOperator(Operator.AND) 修改
SearchSourceBuilder query = new SearchSourceBuilder().query(multiMatchQueryBuilder);
// 4.2 构造查询
Search search = new Search.Builder(query.toString()).build();
// 4.3 执行查询
JestResult result = jestClient.execute(search);
// 5. 输出查询结果
System.out.println(formatJson(result.getJsonString()));
}
}
☞ 区间搜索
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc //TODO
*/
@SpringBootTest
public class ES {
@Test
public void search() throws IOException {
// 1. 创建 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 构建区间搜索条件
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");
// 4.2 解析为 ES 查询
SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder);
// 4.3 构建查询
Search search = new Search.Builder(query.toString()).build();
// 4.4 执行查询
JestResult result = jestClient.execute(search);
// 5. 输出查询结果
System.out.println(formatJson(result.getJsonString()));
}
}
1.3.5 删除文档
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 根据 id 删除文档
*/
@SpringBootTest
public class ES {
@Test
public void delete() throws IOException {
// 1. 创建 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. 输出删除结果
System.out.println(result.getJsonString());
}
}
1.4 其他操作
1.4.1 分页
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc 分页
*/
@SpringBootTest
public class ES {
@Test
public void search() throws IOException {
// 1. 创建 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 构建区间搜索条件
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("id").gt("002").lte("003");
// 4.2 解析为 ES 查询并添加分页
SearchSourceBuilder query = new SearchSourceBuilder().query(rangeQueryBuilder).from(0).size(1);
// 4.3 构建查询
Search search = new Search.Builder(query.toString()).build();
// 4.4 执行查询
JestResult result = jestClient.execute(search);
// 5. 输出查询结果
System.out.println(formatJson(result.getJsonString()));
}
}
1.4.2 高亮
/**
* @author Demo_Null
* @version 1.0
* @date 2021/2/1
* @desc //TODO
*/
@SpringBootTest
public class ES {
@Test
public void search() throws IOException {
// 1. 创建 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 构建搜索条件
MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("天世界", "author", "desc");
// 4.2 转化为 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 构建查询
Search search = new Search.Builder(query.toString()).build();
// 4.5 执行查询, 使用 SearchResult 接收
SearchResult result = jestClient.execute(search);
// 5.1 遍历查询结果
List<SearchResult.Hit<BookDocument, Void>> hits = result.getHits(BookDocument.class);
for (SearchResult.Hit<BookDocument, Void> hit : hits) {
// 5.2 获取高亮集合
Map<String, List<String>> highlight = hit.highlight;
for (String s : highlight.keySet()) {
// 5.3 输出高亮结果
System.out.println("高亮显示:" + highlight.get(s).get(0));
}
}
}
}