文章目錄
- 一、索引庫
- 1、mapping屬性
- 2、索引庫的crud
- 二、文檔的crud
- 三、RestClient
一、索引庫
es中的索引是指相同類型的文檔集合
,即mysql中表的概念
映射:索引中文檔字段的約束,比如名稱、類型
1、mapping屬性
mapping映射是對索引庫中文檔的約束。類似mysql對表單字段的約束
{"id":[1, 2, 3, 4, 5],"name":{"firstname":"明","lastname":"李"}
}
- type:字段數據類型,常見的類型有:
- 字符串:text(可分詞的文本)、keyword(不可分詞的文本,例如國家、品牌、IP地址)
- 布爾:boolean
- 日期:date
- 數值:long、short、byte、integer、double、float
- Object:對象
es里面沒有數組類型,json格式key、value,允許value有多個值。上圖id字段
- index:是否創建索引,默認為true。就是是否創建倒排索引,不創建之后就不能通過它搜索。
- analyzer:使用那種分詞器
- properties:該字段的子字段,上面name
2、索引庫的crud
# 建立索引庫
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": "新字段類型"}}
}
二、文檔的crud
新增操作
POST /索引庫名/_doc/文檔id
{"字段1": "值1“,"字段2": "值2","字段3": "值3",
}
查詢 GET /索引庫名/_doc/文檔id
刪除 DELETE /索引庫名/_doc/文檔id
# 文檔操作
# 插入
POST /linsy/_doc/1
{"age": "11","email": "linsy@linsy.work","info": "this is a first test 文檔","name": {"firstname": "明","lastName": "李"}
}GET /linsy/_doc/1DELETE /linsy/_doc/1POST /linsy/_update/1
{"doc":{"age":1111}
}
修改文檔:
- 全量修改:刪除舊文檔,添加新文檔。就是將上面新增的 DSL 改為 PUT
PUT /索引庫名/_doc/文檔id
{"字段1": "值1“,"字段2": "值2","字段3": "值3",
}
- 增量修改,修改指定字段
POST /索引庫名/_update/文檔id
{"doc":{"字段名":"新的值"}
}
三、RestClient
springboot 導入elasticsearch依賴需注意,它默認使用的版本和springboot的版本一致,你需要對應到安裝在服務器上的版本。
<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>
<properties><java.version>8</java.version><elasticsearch.version>7.17.11</elasticsearch.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
查詢文檔
@Autowiredprivate IHotelService iHotelService;@BeforeEachpublic void before() {restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.52.150:9200")));}@AfterEachpublic void after() throws IOException {restHighLevelClient.close();}@Testpublic 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);}@Testpublic void queryDocumentTest() throws IOException {GetResponse getResponse = restHighLevelClient.get(new GetRequest("hotel", "61075"), RequestOptions.DEFAULT);String json = getResponse.getSourceAsString();System.out.println(json);}@Testpublic void updateDocumentTest() throws IOException {UpdateRequest updateRequest = new UpdateRequest("hotel", "61075");updateRequest.doc("city", "北京","score", "90");restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);}@Testpublic void deleteDocumentTest() throws IOException {restHighLevelClient.delete(new DeleteRequest("hotel", "61075"), RequestOptions.DEFAULT);}@Testpublic 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);}