原生依賴
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.1</version>
</dependency>
初始化對象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
client.close();
然后分析這個對象中的方法即可(api)
創建項目
這里以https://start.spring.io/的方式進行創建
🐤環境規定
- SpringBoot:2.2.5.RELEASE
- JDK:1.8
🐸修改elasticsearch.version
<properties>
<elasticsearch.version>7.8.1</elasticsearch.version>
</properties>
🐬創建配置類ElasticSearchClientConfig.java
@Configuration
public class ElasticSearchClientConfig {
@Bean
public RestHighLevelClient restHighLevelClient() {
return new RestHighLevelClient(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
}
}
索引API
創建索引
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testCreateIndex() throws IOException {
// 1.創建索引請求
CreateIndexRequest request = new CreateIndexRequest("bntang666_index");
// 2.客戶端執行請求
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
}
判斷索引是否存在
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testExistsIndex() throws IOException {
GetIndexRequest request = new GetIndexRequest("bntang666_index");
boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
}
刪除索引
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testDeleteIndex() throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest("test2");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
}
文檔API
創建POJO(實體類)
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private String name;
private int age;
}
添加文檔
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testAddDocument() throws IOException {
// 1.創建對象
User user = new User("灰灰", 23);
// 2.創建請求
IndexRequest request = new IndexRequest("bntang666_index");
// 3.規則
request.id("1");
request.timeout(TimeValue.timeValueSeconds(1));
// 4.將我們的數據放入請求中
request.source(JSON.toJSONString(user), XContentType.JSON);
// 5.客戶端發送請求,獲取響應結果
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
System.out.println(response.status());
}
}
判斷文檔是否存在
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testIsExists() throws IOException {
GetRequest request = new GetRequest("bntang666_index","1");
// 不獲取返回的_source的上下文了
request.fetchSourceContext(new FetchSourceContext(false));
request.storedFields("_none_");
boolean exists = client.exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
}
}
獲取文檔信息
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testGetDocument() throws IOException {
GetRequest request = new GetRequest("bntang666_index", "1");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSource());
System.out.println(response);
}
}
修改文檔信息
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testUpdateDocument() throws IOException {
UpdateRequest request = new UpdateRequest("bntang666_index", "1");
request.timeout("1s");
User user = new User("灰灰說Java", 18);
request.doc(JSON.toJSONString(user), XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
}
刪除文檔信息
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testDeleteDocument() throws IOException {
DeleteRequest request = new DeleteRequest("bntang666_index", "1");
request.timeout("1s");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());
}
}
批量插入文檔信息
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testBatchInsertDocument() throws IOException {
BulkRequest request = new BulkRequest();
request.timeout("10s");
List<Object> list = new ArrayList<>();
list.add(new User("BNTang1", 23));
list.add(new User("BNTang2", 23));
list.add(new User("BNTang3", 23));
list.add(new User("BNTang6661", 23));
list.add(new User("BNTang6662", 23));
list.add(new User("BNTang6663", 23));
for (int i = 0; i < list.size(); i++) {
request.add(
new IndexRequest("bntang666_index")
.id("" + (i + 1))
.source(JSON.toJSONString(list.get(i)), XContentType.JSON)
);
}
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.hasFailures());
}
}
🐤批量修改和批量刪除只需要在循環當中修改對應的API即可
查詢API
精確查詢
@SpringBootTest
class SpringbootEsApiApplicationTests {
@Autowired
@Qualifier(value = "restHighLevelClient")
private RestHighLevelClient client;
@Test
void testSearch() throws IOException {
SearchRequest request = new SearchRequest("bntang666_index");
// 構建搜索條件
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查詢條件,我們可以使用
TermQueryBuilder termQuery = QueryBuilders.termQuery("name", "BNTang1");
sourceBuilder.query(termQuery);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response.getHits()));
System.out.println("======================================");
for (SearchHit searchHit : response.getHits().getHits()) {
System.out.println(searchHit.getSourceAsMap());
}
}
}
- SearchRequest:搜索請求
- SearchSourceBuilder:條件構造
- HighlightBuilder:構建高亮
- TermQueryBuilder:精確查詢
- MatchAllQueryBuilder:匹配所有
xxx QueryBuild 對應的就是Kibana中的命令