import com.alibaba.fastjson.JSON; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.http.HttpHost; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import java.io.IOException; import java.util.Map; class User { String name; String[] fields; public User() { } public User(String name) { this.name = name; } public User(String name, String[] strings) { this.name = name; this.fields = strings; } } public class TestEsClient { public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("localhost", 9200, "http"))); /** // 创建索引的请求 CreateIndexRequest indexRequest = new CreateIndexRequest("test_index2"); // mapping规则去别的地方写好之后,复制粘贴过来,IDEA会自动转义相关符号 String mapping = "{\n" + " \"properties\":{\n" + " \"name\":{\n" + " \"type\":\"keyword\"\n" + " }\n" + " }\n" + "}"; // 添加索引的mapping规则 indexRequest.mapping("_doc", mapping, XContentType.JSON); // 发送请求 CreateIndexResponse response = client.indices().create(indexRequest, RequestOptions.DEFAULT); System.out.println(response); //添加文档 XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); xContentBuilder.startObject(); { xContentBuilder.field("user", "hankin"); xContentBuilder.field("message", "Go ELK"); } xContentBuilder.endObject(); IndexRequest request = new IndexRequest("test_index2"); request.source(xContentBuilder); request.type("_doc"); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); System.out.println(indexResponse); */ // 构造搜索请求 SearchRequest searchRequest = new SearchRequest("test_index2"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(); searchRequest.source(searchSourceBuilder); // 执行搜索请求 SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); // 处理搜索结果 SearchHits hits = searchResponse.getHits(); for (SearchHit hit : hits.getHits()) { Map<String, Object> sourceAsMap = hit.getSourceAsMap(); System.out.println(sourceAsMap); } String jsonResponse = searchResponse.toString(); System.out.println("jsonResponse: " + jsonResponse); JsonObject jsonObject = new JsonParser().parse(jsonResponse).getAsJsonObject(); System.out.println("jsonObject.toString(): " + jsonObject.toString()); // 关闭连接 client.close(); } }
<?xml version="1.0" encoding="UTF-8"?> <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>org.example</groupId> <artifactId>TestElasticSearchClient</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.5.4</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.5.4</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>6.5.4</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>