Maven项目的pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spoon</groupId>
<artifactId>RestElasticSearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.4.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
</project>
创建索引
package demo;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
public class ESCreatIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.201.130",9200,"http")));
CreateIndexRequest request = new CreateIndexRequest("user");
CreateIndexResponse indexResponse = esClient.indices()
.create(request, RequestOptions.DEFAULT);
boolean ack = indexResponse.isAcknowledged();
System.out.println("索引操作: "+ack);
esClient.close();
}
}
查询索引
package demo;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
public class ESSearchIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.201.130",9200,"http")));
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse indexResponse = esClient.indices()
.get(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.getAliases());
System.out.println(indexResponse.getMappings());
System.out.println(indexResponse.getSettings());
System.out.println(indexResponse.getIndices()[0]);
esClient.close();
}
}
删除索引
package demo;
import java.io.IOException;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
public class ESDeleteIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.201.130",9200,"http")));
DeleteIndexRequest request = new DeleteIndexRequest("user");
AcknowledgedResponse indexResponse = esClient.indices()
.delete(request, RequestOptions.DEFAULT);
boolean ack = indexResponse.isAcknowledged();
System.out.println("索引操作: "+ack);
esClient.close();
}
}
网友评论