导入数据库
编写创建索引库的语句
使用copy_to可以将当前的字段拷贝到其他之中实现同时根据多个字段搜索
DSL# 酒店的mappings 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" } } } }
初始化项目中的RestClient
pom<!-- elasticsearch--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.1</version> </dependency>
springboot管理了elasticsearch的版本需要添加覆盖版本
覆盖版本
版本全部变为7.12.1(与搭建的elasticsearch一致)
在测试类中编写初始化和销毁代码
javaublic class HotelIndexTest {
private RestHighLevelClient client;
@BeforeEach
void setUp(){
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.42.10:9200")))
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
编写创建索引库代码
java @Test
void testCreateHotelIndex() throws IOException {
// 创建request对象
CreateIndexRequest request = new CreateIndexRequest("hotel");
// 请求参数
request.source(MAPPING_TEMPLATE, XContentType.JSON);
// 发起请求
client.indices().create(request, RequestOptions.DEFAULT);
}
编写一个类存放创建索引库的语句
javapackage cn.itcast.hotel.constants;
public class HotelConstants {
public final static 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" +
" },\"city\":{\n" +
" \"type\": \"keyword\"\n" +
" },\"starName\":{\n" +
" \"type\": \"keyword\"\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" +
" }\n" +
" ,\"all\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}
运行代码查询平台创建成功
java{
"hotel" : {
"aliases" : { },
"mappings" : {
"properties" : {
"address" : {
"type" : "keyword",
"index" : false
},
"brand" : {
"type" : "keyword"
},
"business" : {
"type" : "keyword"
},
"city" : {
"type" : "keyword"
},
"id" : {
"type" : "keyword"
},
"location" : {
"type" : "geo_point"
},
"name" : {
"type" : "text",
"analyzer" : "ik_max_word"
},
"pic" : {
"type" : "keyword",
"index" : false
},
"price" : {
"type" : "integer"
},
"score" : {
"type" : "integer"
},
"starName" : {
"type" : "keyword"
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "hotel",
"creation_date" : "1759399676974",
"number_of_replicas" : "1",
"uuid" : "aRnUh_l1Thi3hABSvrbm_w",
"version" : {
"created" : "7120199"
}
}
}
}
}
删除索引库判断索引库是否存在
java @Test
void testGetHotelIndex() throws IOException {
// 创建request对象
GetIndexRequest request = new GetIndexRequest("hotel");
// 发起判断索引库是否存在
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
// 输出结果
System.out.println(exists);
}
java @Test
void testDeleteHotelIndex() throws IOException {
// 创建request对象
DeleteIndexRequest request = new DeleteIndexRequest("hotel");
// 发起删除请求
client.indices().delete(request, RequestOptions.DEFAULT);
}
编写添加文档代码
java @Test
void testInsertIndex() throws IOException {
// 根据id查询hotel数据
Hotel hotel= hotelService.getById(36934L);
// 将hotel转成hotelDoc
HotelDoc hotelDoc = new HotelDoc(hotel);
// 创建request对象
IndexRequest request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
client.index(request, RequestOptions.DEFAULT);
}
添加成功在可视化平台上查询 添加成功
根据id查询文档
java @Test
void testGetDocument() throws IOException {
// 创建request对象
GetRequest request = new GetRequest("hotel","36934");
// 获取response
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 解析返回数据
String json = response.getSourceAsString();
// 打印结果
HotelDoc hotelDoc = JSON.parseObject(json,HotelDoc.class);
System.out.println(hotelDoc.toString());
}
本文作者:钱小杰
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!