文档: 一个文档,几相当于mysql重点一行数据,是json格式的
索引:就是相同类型的文档的集合
映射:mapping : 索引中文档的字段约束信息,类似表的结构约束
mysql:擅长事务类型的操作,可以确保数据的安全和一致性
Es :擅长海量数据的搜索,分析,计算
索引操作:
1.创建索引库 put/索引库名 {mapping:{properties:{}}}
2.查询索引库结构 get/索引库名
3.删除索引库 delete/索引库名
4.更新索引库 put/索引库名/_mapping
一旦创建,无法修改mapping,只能对当前索引库进行追加新得字段
文档操作:
新建文档: post/索引库名/_doc/文档id
查询文档: get/{索引库名称}/_doc/{id}
删除文档: delete/{数据库名称}/_doc/id
修改文档
全量更新 先删除,再新增回去
put/索引库名/_doc/文档id
增量更新:
能更新某个字段
post/{索引库名}/_update/文档id
{"doc":{"字段名" :"新的值"}}
#创建hotel索引
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"
},
"seore":{
"type": "integer"
},
"brand":{
"type": "keyword"
, "copy_to": "all"
},
"city":{
"type": "keyword"
, "copy_to": "all"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword"
, "index": false
},
"all":{
"type": "text"
, "analyzer": "ik_max_word"
}
}
}
}
text和keyword都是字符串的类型,text是参与分词的,也就是查询的时候,即使不是全字段符合,也可以别查询到
keyword不参与分词,只有查询到整体符合的字段,才会被查询到
index属性是是否创建索引,就是搜索的时候可不可以按照这个字段进行检索,默认是true
analyzer就是使用的分词器选择
es在代码中的使用
初始化RestClient
在elasticsearch提供的API中,与elasticsearch一切交互都封装在一个名为RestHighLevelClient的类中,必须先完成这个对象的初始化,建立与elasticsearch的连接
1)引入es的RestHighLevelClient依赖:
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
2)因为SpringBoot默认的ES版本是7.6.2,所以我们需要覆盖默认的ES版本:
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.17.7</elasticsearch.version>
</properties>
3)初始化RestHighLevelClient:
public class HotelIndexTest {
private RestHighLevelClient client;
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.150.101:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
索引库
@SpringBootTest
public class HotelIndexText {
private RestHighLevelClient client;
//测试方法执行前执行
@BeforeEach
void beforeAll() {
client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.226.128:9200")));
}
//创建索引库
@Test
void testCreateHotelIndex() throws IOException {
//构建request
CreateIndexRequest request = new CreateIndexRequest("hotel");
//指定mapping结构
request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
//发起请求
client.indices().create(request, RequestOptions.DEFAULT);
}
//删除索引库
@Test
void testDeleteHotelIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
client.indices().delete(request,RequestOptions.DEFAULT);
}
//判断索引库是否存在
@Test
void existIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("hotel");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
// 测试方法后执行
@AfterEach
void afterAll() throws IOException {
client.close();
}
}
文档数据插入
@SpringBootTest
public class HotelDocText {
private RestHighLevelClient client;
//测试方法执行前执行
@BeforeEach
void beforeAll() {
client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.226.128:9200")));
}
@Autowired
private IHotelService hotelService;
//新增文档
@Test
void createDoc() throws IOException {
//酒店数据查询
Hotel hotel = hotelService.getById(36934L);
//转为hotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
//转化为json数据
String jsonString = JSON.toJSONString(hotelDoc);
//构建request
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
//指定数据
request.source(jsonString,XContentType.JSON);
//文档写入es中
client.index(request,RequestOptions.DEFAULT);
}
//根据id查询数据
@Test
void searchDoc() throws IOException {
GetRequest request = new GetRequest("hotel", "36934");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
String sourceAsString = response.getSourceAsString();
System.out.println(sourceAsString);
}
//局部更新
@Test
void updateDoc() throws IOException {
UpdateRequest request = new UpdateRequest("hotel", "36934");
request.doc(
"name","cls412",
"price",88888
);
client.update(request,RequestOptions.DEFAULT);
}
//删除文档
@Test
void deleteDoc() throws IOException {
DeleteRequest request = new DeleteRequest("hotel", "36934");
client.delete(request,RequestOptions.DEFAULT);
}
//批量导入数据
@Test
void batchDoc() throws IOException {
//查询数据
List<Hotel> hotelList = hotelService.list();
//构建bulkRequest
BulkRequest bulkRequest = new BulkRequest();
//循环数据
for (Hotel hotel : hotelList) {
HotelDoc hotelDoc = new HotelDoc(hotel);
String jsonString = JSON.toJSONString(hotelDoc);
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
request.source(jsonString,XContentType.JSON);
bulkRequest.add(request);
}
client.bulk(bulkRequest,RequestOptions.DEFAULT);
}
// 测试方法后执行
@AfterEach
void afterAll() throws IOException {
client.close();
}
}