本文共 5192 字,大约阅读时间需要 17 分钟。
一、RestClient操作索引库
RestClient 实际上是 Elasticsearch 官方为不同语言提供的客户端之一。作为 Java 开发人员,在进行 Elasticsearch 操作时,RestHighLevelClient 是一个非常重要的工具。
RestClient 的主要职责是帮助我们组装 Elasticsearch 的 DSL 语句,并将请求发送到 Elasticsearch 服务器。我们只需要在 Java 代码中配置正确的参数,RestClient 将自动处理请求的发送和数据的格式化。
在项目中引入 Elasticsearch 的 RestHighLevelClient ,需要添加以下依赖:
org.elasticsearch.client elasticsearch-rest-high-level-client
在项目中进行以下步骤即可初始化 RestHighLevelClient:
1.8 7.12.1
@SpringBootTestpublic class HotelIndexTest { private RestHighLevelClient client; @BeforeEach public void setUp() { client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://云服务器ip:9200")) ); } @AfterEach public void tearDown() throws IOException { client.close(); }}
根据酒店表结构,创建索引库时需要注意以下几点:
keyword
,不需要分词。geo_point
类型。keyword
。索引库的创建 DSL 语句如下:
{ "mappings": { "properties": { "id": { "type": "keyword" }, "name": { "type": "text", "analyzer": "ik_max_word", "copy_to": "all" }, "address": { "type": "text", "analyzer": "ik_max_word", "copy_to": "all" }, "price": { "type": "integer" }, "score": { "type": "integer" }, "brand": { "type": "keyword", "copy_to": "all" }, "city": { "type": "keyword", "copy_to": "all" }, "star_name": { "type": "keyword" }, "business": { "type": "keyword", "copy_to": "all" }, "location": { "type": "geo_point" }, "all": { "type": "text", "analyzer": "ik_max_word" } } }}
创建索引库的代码示例:
@Testpublic void testCreateHotelIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("hotel"); request.source(MAPPING_TEMPLATE, XContentType.JSON); client.indices().create(request, RequestOptions.DEFAULT);}
检查索引库是否存在的代码如下:
@Testpublic void testExistsHotelIndex() throws IOException { GetIndexRequest request = new GetIndexRequest("hotel"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println(exists);}
删除索引库的代码:
@Testpublic void testDeleteHotelIndex() throws IOException { DeleteIndexRequest request = new DeleteIndexRequest("hotel"); client.indices().delete(request, RequestOptions.DEFAULT);}
与创建索引库的初始化步骤相同,代码如下:
@SpringBootTestpublic class HotelDocumentTest { private RestHighLevelClient client; @BeforeEach public void setUp() { client = new RestHighLevelClient(RestClient.builder( HttpHost.create("http://云服务器ip:9200")) ); } @AfterEach public void tearDown() throws IOException { client.close(); }}
将数据库中的酒店数据转化为 Elasticsearch 文档,并添加到索引库中。代码示例如下:
@Testpublic void testAddDocument() throws IOException { Hotel hotel = hotelService.getById(5865979L); HotelDoc hotelDoc = new HotelDoc(hotel); String hotelJson = objectMapper.writeValueAsString(hotelDoc); IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString()); request.source(hotelJson, XContentType.JSON); client.index(request, RequestOptions.DEFAULT);}
查询文档的代码:
@Testpublic void testGetDocument() throws IOException { GetRequest request = new GetRequest("hotel").id("5865979"); GetResponse response = client.get(request, RequestOptions.DEFAULT); String json = response.getSourceAsString(); System.out.println(json);}
全量更新文档的代码:
@Testpublic void testUpdateDocument() throws IOException { UpdateRequest request = new UpdateRequest("hotel", "5865979"); request.doc( "name", "地表最强酒店", "price", "99999" ); client.update(request, RequestOptions.DEFAULT);}
删除文档的代码:
@Testpublic void testDeleteDocument() throws IOException { DeleteRequest request = new DeleteRequest("hotel", "5865979"); client.delete(request, RequestOptions.DEFAULT);}
批量导入酒店数据的代码:
@Testpublic void testBulkDocument() throws IOException { ListhotelList = hotelService.list(); BulkRequest request = new BulkRequest(); for (Hotel hotel : hotelList) { HotelDoc hotelDoc = new HotelDoc(hotel); String json = objectMapper.writeValueAsString(hotelDoc); request.add(new IndexRequest("hotel").id(hotel.getId().toString()).source(json, XContentType.JSON)); } client.bulk(request, RequestOptions.DEFAULT);}
转载地址:http://rheyk.baihongyu.com/