配置工作
需导入elasticsearch依赖,注意与电脑上安装的elasticsearch版本保持一致
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-data-elasticsearch
</artifactId>
</dependency>
2.编写elasticsearch配置类
@Configuration
public class ElasticSearchClientConfig {
public RestHighLevelClient
restHighLevelClient(){
RestHighLevelClient restHighLevelClient
=new RestHighLevelClient(
RestClient
.builder(
new HttpHost("127.0.0.1",9200,"http")));
return restHighLevelClient
;
}
}
索引API操作测试
@SpringBootTest
class SpringbootElasticsearchApplicationTests {
@Autowired
RestHighLevelClient restHighLevelClient
;
@Test
void CreateIndexDemo() throws IOException
{
CreateIndexRequest createIndexRequest
= new CreateIndexRequest("wzl_1");
CreateIndexResponse createIndexResponse
= restHighLevelClient
.indices().create(createIndexRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(createIndexResponse
);
}
@Test
void GetIndexDemo() throws IOException
{
GetIndexRequest getIndexRequest
= new GetIndexRequest("wzl_1");
boolean exists
= restHighLevelClient
.indices().exists(getIndexRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(exists
);
}
@Test
void DelIndexDemo() throws IOException
{
DeleteIndexRequest deleteIndexRequest
= new DeleteIndexRequest("wzl_1");
AcknowledgedResponse delete
= restHighLevelClient
.indices().delete(deleteIndexRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(delete
.isAcknowledged());
}
}
文档API操作测试
@Test
void CreateDocDemo() throws IOException
{
User user
= new User("吴木木", 21);
IndexRequest indexRequest
= new IndexRequest("wzl_1");
indexRequest
.id("1");
indexRequest
.timeout(TimeValue
.timeValueSeconds(1));
indexRequest
.source(JSON
.toJSONString(user
), XContentType
.JSON
);
IndexResponse indexResponse
= restHighLevelClient
.index(indexRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(indexResponse
.toString());
System
.out
.println(indexResponse
.status());
}
@Test
void getDocDemo() throws IOException
{
GetRequest getRequest
= new GetRequest("wzl_1", "1");
GetResponse documentFields
= restHighLevelClient
.get(getRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(documentFields
.getSourceAsString());
System
.out
.println(documentFields
);
}
@Test
void updateDocDemo() throws IOException
{
UpdateRequest updateRequest
= new UpdateRequest("wzl_1", "1");
updateRequest
.timeout(TimeValue
.timeValueSeconds(1));
User user
= new User("吴", 10);
updateRequest
.doc(JSON
.toJSONString(user
), XContentType
.JSON
);
UpdateResponse updateResponse
= restHighLevelClient
.update(updateRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(updateResponse
.status());
}
@Test
void deleteDocDemo() throws IOException
{
DeleteRequest deleteRequest
= new DeleteRequest("wzl_1", "1");
DeleteResponse delete
= restHighLevelClient
.delete(deleteRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(delete
.status());
}
@Test
void batchInsertDemo() throws IOException
{
BulkRequest bulkRequest
= new BulkRequest();
bulkRequest
.timeout(TimeValue
.timeValueSeconds(10));
List
<User> userList
= new ArrayList<>();
userList
.add(new User("木木一号", 21));
userList
.add(new User("木木二号", 21));
userList
.add(new User("木木三号", 21));
userList
.add(new User("木木四号", 21));
for (int i
= 0; i
< userList
.size(); i
++) {
bulkRequest
.add(new IndexRequest("wzl_1")
.id("" + (i
+ 1))
.source(JSON
.toJSONString(userList
.get(i
)), XContentType
.JSON
));
}
BulkResponse bulk
= restHighLevelClient
.bulk(bulkRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(bulk
.hasFailures());
}
@Test
void batchDelDemo() throws IOException
{
BulkRequest bulkRequest
= new BulkRequest();
bulkRequest
.timeout(TimeValue
.timeValueSeconds(10));
for (int i
= 1; i
< 5; i
++) {
bulkRequest
.add(new DeleteRequest("wzl_1", "" + i
));
}
BulkResponse bulk
= restHighLevelClient
.bulk(bulkRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(bulk
.hasFailures());
}
@Test
void searchDemo() throws IOException
{
SearchRequest searchRequest
= new SearchRequest("wzl_1");
SearchSourceBuilder searchSourceBuilder
= new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder
= QueryBuilders
.termQuery("age", "21");
searchSourceBuilder
.query(termQueryBuilder
);
searchRequest
.source(searchSourceBuilder
);
SearchResponse search
= restHighLevelClient
.search(searchRequest
, RequestOptions
.DEFAULT
);
System
.out
.println(JSON
.toJSONString(search
.getHits()));
for (SearchHit filed
:
search
.getHits().getHits()) {
System
.out
.println(filed
.getSourceAsMap());
}
}
学习☞:https://www.bilibili.com/video/BV17a4y1x7zq?p=14