一、什么是Elasticsearch
elasticsearch是一款非常强大的开源搜索引擎,可以帮助我们从海量数据中快速找到需要的内容。
二、倒排索引
1、正向索引
2、倒排索引
3、总结
三、ES和MySQL的区别
四、操作索引库
1、基于Kibana(WEB界面)
以下操作均根据以上数据库表进行:
①、创建索引库
#创建索引库
PUT /music
{
"mappings": {
"properties": {
"title":{
"type": "text",
"analyzer": "ik_smart"
},
"singer":{
"type": "object",
"properties": {
"firstname":{
"type":"keyword"
},
"lastname":{
"type":"keyword"
}
}
},
"time":{
"type": "date",
"index": false
},
"url":{
"type": "keyword",
"index": false
},
"userid":{
"type": "text",
"index": false
}
}
}
}
②、查看索引库
#获取索引库
GET /music
③、修改索引库
索引库和mapping一旦创建则无法修改,但是可以添加新的字段。
#为索引库添加字段(仅支持新增字段,不支持修改字段)
PUT /music/_mapping
{
"properties":{
"id":{
"type":"integer",
"index":false
}
}
}
④、删除索引库
#删除索引库
DELETE /music
2、基于RestClient(代码)
1.Java项目导入es的RestHighLevelClient依赖:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.1</version> </dependency>
2.因为SpringBoot默认的ES版本为7.6.2,所以我们需要覆盖默认的ES版本:
<properties> <java.version>1.8</java.version> <elasticsearch.version>7.12.1</elasticsearch.version> </properties>
3.初始化RestHighLevelClient(单元测试中执行)
private RestHighLevelClient client; @BeforeEach void setClient(){ this.client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://XXXX:9200") )); } @AfterEach void tearDown() throws Exception{ this.client.close(); }
CREATE TABLE `tb_hotel` (
`id` bigint(20) NOT NULL COMMENT '酒店id',
`name` varchar(255) NOT NULL COMMENT '酒店名称;例:7天酒店',
`address` varchar(255) NOT NULL COMMENT '酒店地址;例:航头路',
`price` int(10) NOT NULL COMMENT '酒店价格;例:329',
`score` int(2) NOT NULL COMMENT '酒店评分;例:45,就是4.5分',
`brand` varchar(32) NOT NULL COMMENT '酒店品牌;例:如家',
`city` varchar(32) NOT NULL COMMENT '所在城市;例:上海',
`star_name` varchar(16) DEFAULT NULL COMMENT '酒店星级,从低到高分别是:1星到5星,1钻到5钻',
`business` varchar(255) DEFAULT NULL COMMENT '商圈;例:虹桥',
`latitude` varchar(32) NOT NULL COMMENT '纬度;例:31.2497',
`longitude` varchar(32) NOT NULL COMMENT '经度;例:120.3925',
`pic` varchar(255) DEFAULT NULL COMMENT '酒店图片;例:/img/1.jpg',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
以下操作均根据以上数据库表进行:
①、编写mapping映射
PUT /hotel
{
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik_max_word",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword"
},
"star_name":{
"type": "keyword"
},
"bussiness":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
copy_to属性的作用:支持多条件搜索查询
②、创建索引库
//索引库创建
@Test
void createHotelIndex() throws IOException {
//1.创建Request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
//2.准备请求的参数,DSL语句
//MAPPING_TEMPLATE 就是我们编写的mapping映射中的DSL
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
②、删除索引库
//删除索引库
@Test
void deleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
client.indices().delete(request,RequestOptions.DEFAULT);
}
③、查看索引库是否存在
//判断索引库是否存在
@Test
void testExistHotelIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
boolean ret = client.indices().exists(request,RequestOptions.DEFAULT);
System.err.println(ret);
}
五、操作文档
1、基于Kibana(WEB界面)
※参考四当中基于Kibana生成的索引库进行以下文档操作:
①、插入文档
#插入文档(新增数据)
POST /music/_doc/1
{
"id":1,
"title":"爱在西元前",
"singer":{
"firstname":"周",
"lastname":"杰伦"
},
"time":"2022-01-01T12:00:00.000Z",
"url":"/music/get?path=茜茜 - 爱在西元前",
"userid":"1966998940"
}
②、获取文档
#查询文档
GET /music/_doc/1
③、修改文档
方式一:全量修改,会删除旧文档,添加新文档(如果旧文档不存在则创建)
PUT /music/_doc/1 { "id":1, "title":"爱在西元前", "singer":{ "firstname":"周杰伦", "lastname":"Jay" }, "time":"2022-01-01T12:00:00.000Z", "url":"/music/get?path=茜茜 - 爱在西元前", "userid":"1966998940" }
方式二:局部修改,只修改指定字段
POST /music/_update/1 { "doc": { "title":"爱不在西元前" } }
④、删除文档
#删除文档
DELETE /music/_doc/1
2、基于RestClient(代码)
※参考四当中基于RestClient生成的索引库及其数据库数据进行以下文档操作:
①、新增数据库数据到索引库
//添加数据到索引库
@Test
void addDataToHotelIndex() throws IOException {
//根据id查询酒店数据
Hotel hotel = hotelService.getById(61083L);
//转换为Doc文档类型
HotelDoc doc = new HotelDoc(hotel);
//1.准备request对象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
//2.准备Json文档
request.source(JSON.toJSONString(doc), XContentType.JSON);
//3.发送请求
client.index(request, RequestOptions.DEFAULT);
}
HotelDoc实体类:
@Data @NoArgsConstructor public class HotelDoc { private Long id; private String name; private String address; private Integer price; private Integer score; private String brand; private String city; private String starName; private String business; private String location; private String pic; public HotelDoc(Hotel hotel) { this.id = hotel.getId(); this.name = hotel.getName(); this.address = hotel.getAddress(); this.price = hotel.getPrice(); this.score = hotel.getScore(); this.brand = hotel.getBrand(); this.city = hotel.getCity(); this.starName = hotel.getStarName(); this.business = hotel.getBusiness(); this.location = hotel.getLatitude() + ", " + hotel.getLongitude(); this.pic = hotel.getPic(); } }
②、根据ID查询索引库数据
//根据id查询酒店数据
@Test
void queryDataFromHotelIndex() throws IOException {
//1.创建request对象
GetRequest request = new GetRequest("hotel","61083");
//2.发送请求,获取结果
GetResponse response = client.get(request,RequestOptions.DEFAULT);
//3.解析响应结果
String json = response.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
System.out.println(hotelDoc);
}
③、删除酒店数据
//删除文档数据
@Test
void deleteDocument() throws IOException {
//1.准备Request
DeleteRequest request = new DeleteRequest("hotel","61083");
//2.发送请求
client.delete(request,RequestOptions.DEFAULT);
}
④、修改酒店数据
//修改文档数据
@Test
void updateDocument() throws IOException {
//1.准备Request
UpdateRequest request = new UpdateRequest("hotel","61083");
//2.准备请求参数
request.doc(
"brand","皇冠假日酒店",
"starName","四星"
);
//3.发送请求
client.update(request,RequestOptions.DEFAULT);
}
⑤、批量导入数据库数据到索引库
//批量新增文档数据
@Test
void testBulkRequest() throws IOException {
//1.创建Request
BulkRequest request = new BulkRequest();
//2.准备Json文档
//批量查询酒店数据
List<Hotel> list = hotelService.list();
for (int i = 0; i < list.size(); i++) {
HotelDoc hotelDoc = new HotelDoc(list.get(i));
request.add(new IndexRequest("hotel").
id(hotelDoc.getId().toString()).
source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
//3.发送请求
client.bulk(request,RequestOptions.DEFAULT);
}