java代码操作es,要用到RestClient
RestClient是什么?
实际上他就是我们es官方提供的各种不同的语言的客户端(java,c++等)
用来操作es 这些客户端的本质就是组装DSL语句 通过http请求发送给
es,也就是我们不用手写DSL语句客户端帮我写我只发请求就好了
客户端:


分析数据:
mapping要考虑的问题:
字段名,数据模型,是否参与搜素,是否是分词,如果是分词,分词器是什么?

编写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"}}}}
想根据多个字段去搜索:
RestClient操作索引库—-初始化RestClient:
- 引入es的RestClinentHighleveClient依赖
- 因为SpringBoot默认版本是7.6.2所以我们需要覆盖默认es版本
- 初始化RestHighLevelClient

<dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency>
<properties><java.version>1.8</java.version><elasticsearch.version>7.12.1</elasticsearch.version></properties>
@Testvoid testIndex(){System.out.println(client);}@BeforeEachvoid setUp() {//创建客户端client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.150.101:9200")));}@AfterEachvoid tearDown() throws IOException {//销毁客户端client.close();}

RestClient:操作索引库—-创建索引库:


@Testvoid createHotelIndex() throws IOException {//创建Request对象 索引库的名称CreateIndexRequest request = new CreateIndexRequest("hotel");//准备请求的参数DSL语句 传递其实就是DSL json风格的语句request.source(MAPPING_TEMPLATE,XContentType.JSON);//发送请求包含了索引库里面的所有的操作方法 走默认client.indices().create(request,RequestOptions.DEFAULT);}
删除和判断索引库是否存在


/*** 删除索引库*/@Testvoid testDeletIndex() throws IOException {//创建请求对象DeleteIndexRequest request = new DeleteIndexRequest("hotel");//发送请求client.indices().delete(request,RequestOptions.DEFAULT);}
/*** 判断索引库是否存在* @throws IOException*/@Testvoid testExisHotleIndex() throws IOException {//创建请求对象GetIndexRequest request = new GetIndexRequest("hotel");//发送请求:final boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists ? "存在":"不存在");}
总结:
初始化:
RestHighLevelClient
new 出它指定IP地址和端口号
创建Requet对象根据你直接的业务需求来决定(增删查,判断是否存在)都会有自己独立的Requets对象
准备DSL(只要创建的时候才会需要参数)
发送请求 调用 RestHighLevelClient #indices().xxx()方法
xxx:create exists delete
import static cn.itcast.hotel.constants.HotelIndexConstants.MAPPING_TEMPLATE;@SpringBootTestclass HotelIndexTest {private RestHighLevelClient client;@Testvoid testCreateIndex() throws IOException {// 1.准备Request PUT /hotelCreateIndexRequest request = new CreateIndexRequest("hotel");// 2.准备请求参数request.source(MAPPING_TEMPLATE, XContentType.JSON);// 3.发送请求client.indices().create(request, RequestOptions.DEFAULT);}/*** 判断索引库是否存在* @throws IOException*/@Testvoid testExisHotleIndex() throws IOException {//创建请求对象GetIndexRequest request = new GetIndexRequest("hotel");//发送请求:final boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists ? "存在":"不存在");}/*** 删除索引库*/@Testvoid testDeletIndex() throws IOException {//创建请求对象DeleteIndexRequest request = new DeleteIndexRequest("hotel");//发送请求client.indices().delete(request,RequestOptions.DEFAULT);}@Testvoid testIndex(){System.out.println(client);}@BeforeEachvoid setUp() {//创建客户端client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://114.55.210.174:9200")));}@AfterEachvoid tearDown() throws IOException {//销毁客户端client.close();}}
