es中的索引是指相同类型的文档集合
,即mysql中表的概念
映射:索引中文档字段的约束,比如名称、类型
mapping映射是对索引库中文档的约束。类似mysql对表单字段的约束
{
"id":[1, 2, 3, 4, 5],
"name":{
"firstname":"明",
"lastname":"李"
}
}
# 建立索引库
PUT /linsy
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstname": {
"type": "keyword"
},
"lastName": {
"type": "keyword"
}
}
}
}
}
}
查询索引库 GET /索引库名 GET /linsy
删除索引库 DELETE /索引库名
ES 禁止修改索引库字段类型及属性,会影响整个索引的结构,但是允许在原有索引库中新增字段。
注意不能新增已有字段
PUT /索引库名/_mapping
{
"properties": {
"新字段名": {
"type": "新字段类型"
}
}
}
新增操作
POST /索引库名/_doc/文档id
{
"字段1": "值1“,
"字段2": "值2",
"字段3": "值3",
}
查询 GET /索引库名/_doc/文档id
删除 DELETE /索引库名/_doc/文档id
# 文档操作
# 插入
POST /linsy/_doc/1
{
"age": "11",
"email": "[email protected]",
"info": "this is a first test 文档",
"name": {
"firstname": "明",
"lastName": "李"
}
}
GET /linsy/_doc/1
DELETE /linsy/_doc/1
POST /linsy/_update/1
{
"doc":{
"age":1111
}
}
修改文档:
PUT /索引库名/_doc/文档id
{
"字段1": "值1“,
"字段2": "值2",
"字段3": "值3",
}
POST /索引库名/_update/文档id
{
"doc":{
"字段名":"新的值"
}
}
springboot 导入elasticsearch依赖需注意,它默认使用的版本和springboot的版本一致,你需要对应到安装在服务器上的版本。
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
dependency>
<properties>
<java.version>8java.version>
<elasticsearch.version>7.17.11elasticsearch.version>
properties>
创建索引库的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"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
RestHighLevelClient 的使用
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://http://192.168.52.150:9200")
));
// index的增删查
CreateIndexRequest createIndexRequest = new CreateIndexRequest("linsy");
createIndexRequest.source("建立索引库语句(put)", XContentType.JSON);
restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
restHighLevelClient.indices().delete(new DeleteIndexRequest("要删除的索引库名"), RequestOptions.DEFAULT);
// 判断是否存在
boolean b = restHighLevelClient.indices().exists(new GetIndexRequest("索引库名"), RequestOptions.DEFAULT);
es8.x已经弃用了RestHighLevelClient
官方创建RestClient文档
文档的crud
查询文档
@Autowired
private IHotelService iHotelService;
@BeforeEach
public void before() {
restHighLevelClient = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.52.150:9200")
));
}
@AfterEach
public void after() throws IOException {
restHighLevelClient.close();
}
@Test
public void addDocumentTest() throws IOException {
Hotel hotel = iHotelService.getById(61075);
HotelDoc hotelDoc = new HotelDoc(hotel);
IndexRequest indexRequest = new IndexRequest("hotel").id(hotel.getId().toString());
indexRequest.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}
@Test
public void queryDocumentTest() throws IOException {
GetResponse getResponse = restHighLevelClient.get(new GetRequest("hotel", "61075"), RequestOptions.DEFAULT);
String json = getResponse.getSourceAsString();
System.out.println(json);
}
@Test
public void updateDocumentTest() throws IOException {
UpdateRequest updateRequest = new UpdateRequest("hotel", "61075");
updateRequest.doc(
"city", "北京",
"score", "90"
);
restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
}
@Test
public void deleteDocumentTest() throws IOException {
restHighLevelClient.delete(new DeleteRequest("hotel", "61075"), RequestOptions.DEFAULT);
}
@Test
public void batchAdd() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
List<Hotel> list = iHotelService.list();
for (Hotel hotel : list) {
HotelDoc hotelDoc = new HotelDoc(hotel);
bulkRequest.add(new IndexRequest("hotel")
.id(hotel.getId().toString())
.source(JSON.toJSONString(hotelDoc), XContentType.JSON));
}
restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
}