以下是一个简单的Maven工程和Spring demo案例,演示如何使用Java调用Elasticsearch。
配置Maven依赖
在pom.xml文件中添加以下依赖:
org.elasticsearch elasticsearch 6.5.4 org.elasticsearch.client transport 6.5.4 org.elasticsearch.client rest 6.5.4
创建Elasticsearch连接
创建一个Elasticsearch连接配置类,用于连接Elasticsearch集群。示例代码如下:
@Configuration
public class ElasticsearchConfig {
private static final String HOST = "localhost";
private static final int PORT = 9300;
@Bean
public RestHighLevelClient restHighLevelClient() {
RestClientBuilder builder = RestClient.builder(new HttpHost(HOST, PORT));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}
创建Elasticsearch操作类
创建一个Elasticsearch操作类,用于执行Elasticsearch的操作。示例代码如下:
@Component
public class ElasticsearchService {
private RestHighLevelClient client;
@Autowired
public ElasticsearchService(RestHighLevelClient client) {
this.client = client;
}
public void createIndex(String indexName) throws IOException {
CreateIndexRequest request = new CreateIndexRequest(indexName);
client.indices().create(request, RequestOptions.DEFAULT);
}
public void deleteIndex(String indexName) throws IOException {
DeleteIndexRequest request = new DeleteIndexRequest(indexName);
client.indices().delete(request, RequestOptions.DEFAULT);
}
public void insert(String indexName, String id, String jsonString) throws IOException {
IndexRequest request = new IndexRequest(indexName);
request.id(id);
request.source(jsonString, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
}
public void update(String indexName, String id, String jsonString) throws IOException {
UpdateRequest request = new UpdateRequest(indexName, id);
request.doc(jsonString, XContentType.JSON);
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
}
public void delete(String indexName, String id) throws IOException {
DeleteRequest request = new DeleteRequest(indexName, id);
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
}
public SearchHits search(String indexName, QueryBuilder queryBuilder) throws IOException {
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(queryBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
return hits;
}
}
4. 创建Spring Boot控制器
创建一个Spring Boot控制器,在其中调用Elasticsearch操作类执行Elasticsearch操作。示例代码如下:
@RestController
public class ElasticsearchController {
@Autowired
private ElasticsearchService elasticsearchService;
@GetMapping("/createIndex")
public String createIndex() throws IOException {
elasticsearchService.createIndex("my_index");
return "Index created successfully";
}
@GetMapping("/deleteIndex")
public String deleteIndex() throws IOException {
elasticsearchService.deleteIndex("my_index");
return "Index deleted successfully";
}
@PostMapping("/insert")
public String insert() throws IOException {
String jsonString = "{"title":"Java Programming","author":"John Doe"}";
elasticsearchService.insert("my_index", "1", jsonString);
return "Document inserted successfully";
}
@PostMapping("/update")
public String update() throws IOException {
String jsonString = "{"title":"Java Programming","author":"Jane Doe"}";
elasticsearchService.update("my_index", "1", jsonString);
return "Document updated successfully";
}
@GetMapping("/delete")
public String delete() throws IOException {
elasticsearchService.delete("my_index", "1");
return "Document deleted successfully";
}
@GetMapping("/search")
public SearchHits search() throws IOException {
QueryBuilder queryBuilder = QueryBuilders.matchQuery("title", "Java");
SearchHits hits = elasticsearchService.search("my_index", queryBuilder);
return hits;
}
}
以上代码演示了如何创建Elasticsearch连接、执行Elasticsearch
操作,以及在Spring Boot控制器中调用Elasticsearch操作类执行操作。
测试
运行Spring Boot应用程序,并访问以下URL以执行相应的操作:
/createIndex:创建名为“my_index”的索引。
/deleteIndex:删除名为“my_index”的索引。
/insert:向名为“my_index”的索引插入一条文档。
/update:更新名为“my_index”的索引中的文档。
/delete:从名为“my_index”的索引中删除文档。
/search:从名为“my_index”的索引中搜索匹配“Java”的文档。
可以使用Postman或类似的工具进行测试。
以上就是一个基本的Maven工程和Spring demo案例,演示了如何使用Java调用Elasticsearch。这只是一个简单的示例,实际应用中可能需要更复杂和细致的实现。
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net