索引库操作
初始化索引库
①引入es的RestHighLevelClient依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
②因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
③初始化RestHighLevelClient:
@Bean
public RestHighLevelClient client(){
return new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://119.23.248.50:9200")
));
}
创建索引库
①创建一个类,定义mapping映射的JSON字符串常量
package cn.itcast.hotel.constants;
public class HotelConstants {
public static final String MAPPING_TEMPLATE = "{\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"id\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"address\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"price\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"score\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"brand\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"city\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\n" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"location\":{\n" +
" \"type\": \"geo_point\"\n" +
" },\n" +
" \"pic\":{\n" +
" \"type\": \"keyword\",\n" +
" \"index\": false\n" +
" },\n" +
" \"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
②编写代码
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
//新增索引库
@Test
void createIndex() throws IOException {
//准备request请求
CreateIndexRequest request = new CreateIndexRequest("hotel");
//准备参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
}
删除索引库
编写代码
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
//删除索引库
@Test
void deleteIndex() throws IOException {
//准备request请求
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
//准备参数(删除操作不需要参数)
//发送请求
client.indices().delete(request,RequestOptions.DEFAULT);
}
}
判断索引库是否存在
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
//判断索引库是否存在
@Test
void exitsIndex() throws IOException {
//准备request请求
GetIndexRequest request = new GetIndexRequest("hotel");
//准备参数(判断索引库是否存在不需要参数)
//发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//打印一下
System.out.println(exists?"索引库存在":"索引库不存在");
}
}
总结
索引库操作的基本步骤
- 初始化RestHighLevelClient。
- 创建XXXIndexRequest,XXX是Create、Get、Delete。
- 准备DSL参数(只有Create的时候需要,其它都是无参)。
- 发送请求。调用 **indices( ).xxx( )**方法,xxx是create、delete、exists。
文档操作
新增文档
- 根据 id 查询酒店数据 Hotel
- 将 Hotel 封装为 HotelDoc
- 将 HotelDoc 序列化为 JSON
- 创建 IndexRequest,指定索引库名称和id
- 准备请求参数,也就是JSON文档
- 发送请求
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
//新增文档
@Test
void createDocument() throws IOException {
//根据id查询酒店数据
Hotel hotel = hotelService.getById(61083L);
//转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
//序列化为JSON
String json = JSON.toJSONString(hotelDoc);
//准备request请求
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
//准备参数
request.source(json,XContentType.JSON);
//发送请求
client.index(request,RequestOptions.DEFAULT);
}
}
删除文档
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
//删除文档
@Test
void deleteDocument() throws IOException {
//准备request请求
DeleteRequest request = new DeleteRequest("hotel", "61083");
//准备参数(删除文档不需要参数)
//发送请求
client.delete(request,RequestOptions.DEFAULT);
}
}
修改文档
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
//修改文档
@Test
void deleteDocument() throws IOException {
//准备request请求
UpdateRequest request = new UpdateRequest("hotel", "61083");
//准备参数
request.doc(
"age",18,
"name","Tom"
);
//发送请求
client.update(request,RequestOptions.DEFAULT);
}
}
查询文档
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
//查询文档
@Test
void getDocument() throws IOException {
//准备request请求
GetRequest request = new GetRequest("hotel", "61083");
//发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
//解析响应结果
String json = response.getSourceAsString();
//反序列化成对象
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
//打印
System.out.println(hotelDoc);
}
}
批量导入文档
@SpringBootTest
public class Index {
@Autowired
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
//批量插入文档
@Test
void getDocument() throws IOException {
//先把数据库中,酒店的所有信息查出来
List<Hotel> hotels = hotelService.list();
//创建request请求
BulkRequest request = new BulkRequest();
//准备参数,这里的参数就是多个request对象
for (Hotel hotel : hotels) {
//转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
//转换为JSON
String json = JSON.toJSONString(hotelDoc);
//创建新增文档的request对象
request.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())//id
.source(json, XContentType.JSON));
}
//发送请求
client.bulk(request,RequestOptions.DEFAULT);
}
}
文档查询
处理响应结果
编写一个方法,处理响应结果(不含高亮)
//这里编写一个解析响应结果的方法
private void parseResponse(SearchResponse response){
//解析响应
SearchHits searchHits = response.getHits();
//查询到的条数
long total = searchHits.getTotalHits().value;
System.out.println("查询总条数为:"+total);
//获取文档,得到一个数组
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
//获取文档信息(json)
String json = hit.getSourceAsString();
//将JSON反序列化为对象
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
//这里直接打印
System.out.println(hotelDoc);
}
}
编写一个方法,处理响应结果(含高亮)
//这里编写一个解析响应结果的方法(高亮)
private void parseResponseHighlight(SearchResponse response){
//解析响应
SearchHits searchHits = response.getHits();
//查询到的条数
long total = searchHits.getTotalHits().value;
System.out.println("查询总条数为:"+total);
//获取文档,得到一个数组
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
//获取文档信息(json)
String json = hit.getSourceAsString();
//将JSON反序列化为对象
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
//获取高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if (highlightFields!=null){
//不为空,那就覆盖,这里以brand高亮为例
String value = highlightFields.get("brand").getFragments()[0].string();
hotelDoc.setBrand(value);
}
//这里直接打印
System.out.println(hotelDoc);
}
}
match查询
全文检索的 match 和 multi_match 查询与match_all的API基本一致。差别是查询条件,也就是query的部分。
@Autowired
private RestHighLevelClient client;
//match查询
@Test
void match() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备参数
request.source().query(QueryBuilders.matchQuery("all","如家"));
//发送请求,得到响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析响应结果
parseResponse(response);
}
term精确查询
//Term精确查询
@Test
void term() throws IOException {
//准备request请求
SearchRequest request = new SearchRequest("hotel");
//准备参数
request.source().query(QueryBuilders.termQuery("brand","如家"));
//发送请求,得到响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析响应结果
parseResponse(response);
}
bool查询
//bool查询
@Test
void bool() throws IOException {
//准备request请求
SearchRequest request = new SearchRequest("hotel");
//准备参数
//准备BoolQuery
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//添加term
boolQuery.must(QueryBuilders.termQuery("brand","如家"));
//添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(200));
//装进去
request.source().query(boolQuery);
//发送请求,得到响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析响应结果
parseResponse(response);
}
排序
//排序
@Test
void sort() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备参数
request.source().query(QueryBuilders.termQuery("brand","如家"));
//排序
request.source().sort("price", SortOrder.DESC);
//发送请求,得到响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析响应结果
parseResponse(response);
}
分页
//分页
@Test
void page() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备参数
request.source().query(QueryBuilders.termQuery("brand","如家"));
//分页
//当前页码
int pageNum=1;
//每页显示条数
int pageSize=6;
request.source().from((pageNum-1)*pageSize).size(pageSize);
//发送请求,得到响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析响应结果
parseResponse(response);
}
高亮
//高亮
@Test
void highlight() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备参数
request.source().query(QueryBuilders.matchQuery("all","如家"));
//高亮
request.source().highlighter(new HighlightBuilder()
.field("brand")//高亮字段
.preTags("<strong>")//标签前缀
.postTags("</strong>")//标签后缀
.requireFieldMatch(false)//是否需要匹配
);
//发送请求,得到响应
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析响应结果(高亮)
parseResponseHighlight(response);
}