索引庫操作
mapping屬性
mapping是對文檔的約束,常見約束屬性包括:
創(chuàng)建索引庫
#創(chuàng)建索引庫
PUT /heima
{
"mappings": {
"properties": {
"info":{
"type": "text",
"analyzer": "ik_smart"
},
"email":{
"type": "keyword",
"index": false
},
"name":{
"type": "object",
"properties": {
"firstName":{
"type":"keyword"
},
"lastName":{
"type":"keyword"
}
}
}
}
}
}
?如下所示,成功創(chuàng)建一個索引庫
查詢,刪除,修改索引庫
?在es中不允許修改索引庫,但可以添加新的字段
#添加新字段
PUT /heima/_mapping
{
"properties":{
"age":{
"type":"integer"
}
}
}
然后重新獲取就有新的字段出現(xiàn)了
文檔操作
新增文檔
在索引庫插入一個規(guī)定格式的數(shù)據就像在一個數(shù)據庫表里面插入一條數(shù)據。?
POST /heima/_doc/1
{
"info":"北嶺山腳鼠鼠",
"email":"yhy@beiling.cn",
"name":{
"firstName":"云",
"lastName":"趙"
}
}
查詢和刪除
?
修改文檔
有兩種修改方式——全量修改
?全量修改與新增文檔幾乎一模一樣就是方法名不同?
#全量修改文檔
PUT /heima/_doc/1
{
"info":"北嶺鼠鼠",
"email":"yhy@beiling.cn",
"name":{
"firstName":"云",
"lastName":"趙"
}
}
#局部修改文檔字段
POST /heima/_doc/1
{
"doc":{
"email":"123@123.cn"
}
}
RestClient操作索引庫(java)
入門案例
導入Demo
創(chuàng)建一個名為heima的數(shù)據庫運行給好的SQL文件就可以得到如下。
?然后導入準備好的項目
?分析數(shù)據結構
?id屬性較為特殊,在索引庫里面是字符串,并且不分詞,所以用的是keyword類型
address不參與搜索,選擇index置為false.
starName使用駝峰命名法。
地理坐標較為特殊,由兩個字段組成,使用一個location字段表示
?有多個字段同時參與搜索,但是通常一個字段的查詢效率比較高,解決方法如下,用一個all字段包含多個字段就可以根據其中一個字段搜索到多個字段的內容了。并且all字段不參與倒排索引
定義對應的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"
}
}
}
}
初始化JavaRestClient
<!--elasticsearch-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.12.1</version>
</dependency>
不指定版本的話里面有子依賴會用默認7.6版本。
創(chuàng)建新的測試類
使用Before注解在最開始創(chuàng)建,之后可以直接使用。
public class HotelIndexTest {
private RestHighLevelClient client;
@Test
void testInit(){
System.out.println(client);
}
//執(zhí)行前初始化
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
//HttpHost.create("http://xxx.xxx.xxx.xxx:9200") 集群時添加更多的地址
));
}
//銷毀
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
創(chuàng)建索引庫
?準備一個實體對象靜態(tài)屬性
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" +
" },\n" +
" \"starName\":{\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"business\":{\n" +
" \"type\": \"keyword\",\n" +
" \"copy_to\": \"all\"\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" +
"}";
}
@Test
void createHotelIndex() throws IOException {
//1.創(chuàng)建Request對象
CreateIndexRequest request = new CreateIndexRequest("hotel");
//2.準備請求的參數(shù)
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//3.發(fā)送請求
client.indices().create(request, RequestOptions.DEFAULT);
}
運行后可以查詢到對應的索引庫
?刪除和判斷索引庫
@Test
void testDeleteHotelIndex() throws IOException {
//1.創(chuàng)建Request對象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
//2.發(fā)送請求
client.indices().delete(request, RequestOptions.DEFAULT);
}
@Test
void testExistsHotelIndex() throws IOException {
//1.創(chuàng)建Request對象
GetIndexRequest request = new GetIndexRequest("hotel");
//2.發(fā)送請求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
//3.輸出
System.out.println(exists?"索引庫已存在":"索引庫不存在");
}
總結:
?RestClient操作文檔
初始化和上面一樣
?添加文檔
?這里將查詢數(shù)據庫的數(shù)據插入到索引庫當中,但是索引庫中的location是將兩個數(shù)據拼到一起的,所以這里有兩個實體類,一個對應數(shù)據庫,一個對應索引庫,從數(shù)據庫查出來之后還要轉到索引庫數(shù)據類型。
然后再序列化層JSON風格的數(shù)據。
@SpringBootTest
public class HotelDocumentTest {
private RestHighLevelClient client;
@Autowired
private IHotelService hotelService;
@Test
void testAddDocument() throws IOException {
//根據id查詢酒店數(shù)據
Hotel hotel = hotelService.getById(61083L);
//轉換為文檔類型
HotelDoc hotelDoc = new HotelDoc(hotel);
//1.準備Request對象
IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
//2.準備JSON文檔
request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
//3.發(fā)送請求
client.index(request,RequestOptions.DEFAULT);
}
//執(zhí)行前初始化
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://xxx.xxx.xxx.xxx:9200")
//HttpHost.create("http://xxx.xxx.xxx.xxx:9200") 集群時添加更多的地址
));
}
//銷毀
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
這里使用給定的項目代碼在數(shù)據庫配置中有錯誤
改成如下
url: jdbc:mysql://localhost:3306/heima?allowPublicKeyRetrieval=true&useSSL=false
成功弄執(zhí)行后便可查詢到?
?查詢文檔
@Test
void testGetDocumentById() throws IOException {
//1.準備request
GetRequest request = new GetRequest("hotel", "61083");
//2.發(fā)送請求,得到響應
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 testUpdate() throws IOException {
//1.準備request
UpdateRequest request = new UpdateRequest("hotel", "61083");
//2.準備請求參數(shù)
request.doc(
"price","961",
"starName","三轉"
);
//3.發(fā)送請求
client.update(request,RequestOptions.DEFAULT);
}
刪除文檔?
@Test
void testDeleteDocument() throws IOException {
//1.準備request
DeleteRequest request = new DeleteRequest("hotel", "61083");
//2.發(fā)送請求
client.delete(request,RequestOptions.DEFAULT);
}
?總結
批量導入文檔
@Test
void testBulkRequest() throws IOException {
//批量查詢酒店數(shù)據
List<Hotel> hotels = hotelService.list();
//1.創(chuàng)建request
BulkRequest request = new BulkRequest();
//2.準備參數(shù),添加多個新增的request
for (Hotel hotel : hotels) {
//轉換為文檔類型
HotelDoc hotelDoc = new HotelDoc(hotel);
//創(chuàng)建新增文檔的Request對象
request.add(new IndexRequest("hotel")
.id(hotel.getId().toString())
.source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
//3.發(fā)送請求
client.bulk(request,RequestOptions.DEFAULT);
}
?然后可以查詢得到201條數(shù)據文章來源:http://www.zghlxwxcb.cn/news/detail-637504.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-637504.html
到了這里,關于微服務——操作索引庫+文檔操作+RestClient操作索引庫和文檔(java程序)的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!