ElasticSearch【基本操作以及集成 SpringBoot】
    		       		warning:
    		            这篇文章距离上次修改已过452天,其中的内容可能已经有所变动。
    		        
        		                
                在ElasticSearch中,我们可以进行基本的操作,如索引创建、文档的增删改查等。同时,我们也可以将ElasticSearch集成到SpringBoot项目中,以便更好地管理和使用ElasticSearch。
- 创建索引
 
@Autowired
private RestHighLevelClient client;
 
public boolean createIndex(String indexName) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    return createIndexResponse.isAcknowledged();
}- 删除索引
 
public boolean deleteIndex(String indexName) throws IOException {
    DeleteIndexRequest request = new DeleteIndexRequest(indexName);
    AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
    return deleteIndexResponse.isAcknowledged();
}- 添加文档
 
public boolean addDocument(String indexName, String jsonString) throws IOException {
    IndexRequest request = new IndexRequest(indexName);
    request.source(jsonString, XContentType.JSON);
    IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
    return indexResponse.getResult() == DocWriteResponse.Result.CREATED;
}- 获取文档
 
public String getDocument(String indexName, String id) throws IOException {
    GetRequest getRequest = new GetRequest(indexName, id);
    GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
    return getResponse.getSourceAsString();
}- 更新文档
 
public boolean updateDocument(String indexName, String id, String jsonString) throws IOException {
    UpdateRequest request = new UpdateRequest(indexName, id);
    request.doc(jsonString, XContentType.JSON);
    UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
    return updateResponse.getResult() == DocWriteResponse.Result.UPDATED;
}- 删除文档
 
public boolean deleteDocument(String indexName, String id) throws IOException {
    DeleteRequest request = new DeleteRequest(indexName, id);
    DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
    return deleteResponse.getResult() == DocWriteResponse.Result.DELETED;
}- 搜索文档
 
public List<Map<String, Object>> searchDocument(String indexName, String keyword) throws IOException {
    SearchRequest searchRequest = new SearchRequest(indexName);
    SearchSourceBuilder searchSou           
评论已关闭