利用JavaRestClient实现创建、删除索引库,判断索引库是否存在
创建索引
#酒店的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"
}
}
}
}
ES中支持两种地理坐标数据类型:
geo_point:由纬度和经度确定的一个点
geo_shape:有多个geo_point组成的复杂几何图形,例如一条直线
加入elasticsearch的依赖坐标
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.12.1
2、因为SpringBoot默认版本ES是7.6.2
修改properties保证elasticsearch版本一致

1.8
7.12.1
3.初始化RestHighLevelClient
@SpringBootTest
public class HotelIndexTest {
private RestHighLevelClient client;
@Test
void testInit(){
System.out.println(client);
}
@BeforeEach
void setUP(){
this.client=new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.205.128:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
@Test
void createHotelIndex() throws IOException {
//1.创建Request对象
CreateIndexRequest request=new CreateIndexRequest("hotel");
//2准备请求的参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
//3.发送请求
client.indices().create(request, RequestOptions.DEFAULT);
}
其中的MAPPING_TEMPLATE是定义的常量用来存放
public class HotelConstans {
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" +
" \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" +
" }\n" +
" }\n" +
"}";
}
//删除索引库
@Test
void testDeleteHodelIndex() throws IOException {
//创建request对象
DeleteIndexRequest request=new DeleteIndexRequest("hotel");
//发送请求
client.indices().delete(request,RequestOptions.DEFAULT);
}
//判断是否存在
@Test
void testExistsIndexHotelIndex() throws IOException {
//创建resquest对象
GetIndexRequest request=new GetIndexRequest("hotel");
//发送请求
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists?"索引库存在":"索引库不存在");
}
去数据库中查询酒店数据,导入到hotel索引库,实现酒店数据的CRUD
1.初始化JavaRestClient
2.利用JavaRestClient新增酒店数据
//添加文档数据
@Test
void testAddDoucment() throws IOException {
//调用数据库查询
Hotel hotel = hotelService.getById(36934);
//准备Request对象
IndexRequest request=new IndexRequest("hotel").id(hotel.getId().toString());
//准备JSON文档
HotelDoc hotelDoc = new HotelDoc(hotel);
//准备请求的参数
request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
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();
}
}
3.利用JavaRestClient根据id查询酒店
//根据id查询数据
@Test
void testGetDocumentById() throws IOException {
//准备Request对象
GetRequest request=new GetRequest("hotel","36934");
GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
String json = documentFields.getSourceAsString();
//反序列化
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
System.out.println(hotelDoc);
}
4.利用JavaRestClient删除酒店数据
@Test
void testDeleteDocument() throws IOException {
DeleteRequest request=new DeleteRequest("hotel","36934");
client.delete(request,RequestOptions.DEFAULT);
}
5.利用JavaRestClient修改酒店数据
局部更新:
//修改数据
@Test
void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("hotel", "36934");
request.doc(
"price","666"
);
client.update(request,RequestOptions.DEFAULT);
}
@Test
void testBulkRequest() throws IOException {
//批量查询酒店数据
List<Hotel> list = hotelService.list();
//创建Request
BulkRequest bulkRequest = new BulkRequest();
//转换为文档类型
for (Hotel hotel : list) {
HotelDoc hotelDoc = new HotelDoc(hotel);
bulkRequest.add(new IndexRequest("hotel")
.id(hotelDoc.getId().toString())
.source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
//添加请求
client.bulk(bulkRequest, RequestOptions.DEFAULT);
}
查询:
GET /hotel/_search
