elasticsearch安装和使用ik分词器,分享Java资深架构师的成长之路
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
public class ElasticsearchExample {
public static void main(String[] args) throws IOException {
// 初始化Elasticsearch客户端
RestClientBuilder builder = RestClient.builder(
new HttpHost("localhost", 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
// 检查Elasticsearch集群的健康状态
ClusterHealthRequest request = new ClusterHealthRequest();
ClusterHealthResponse response = client.cluster().health(request, RequestOptions.DEFAULT);
System.out.println("Elasticsearch cluster health: " + response.getStatus());
// 关闭Elasticsearch客户端
client.close();
}
}
这段代码演示了如何使用Elasticsearch Java API连接到本地运行的Elasticsearch实例,并检查集群的健康状态。在实际使用中,你需要根据自己的Elasticsearch服务器地址和端口进行相应的调整。这个简单的例子可以作为开始使用Elasticsearch Java API的基础。
评论已关闭