ElasticSearch的JavaAPI
1、导入依赖
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
2、创建索引
package com.ycz.ES;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientCreate {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
CreateIndexRequest request = new CreateIndexRequest("user");
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("索引创建" + createIndexResponse);
client.close();
}
}
3、索引删除
package com.ycz.ES;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
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;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientDeleteIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
DeleteIndexRequest request = new DeleteIndexRequest("user");
AcknowledgedResponse delete1 = client.indices().delete(request , RequestOptions.DEFAULT);
System.out.println("索引删除" + delete1);
client.close();
}
}
4、索引查询
package com.ycz.ES;
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;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientSearchIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println("索引查询" + getIndexResponse.getAliases());
System.out.println("索引查询" + getIndexResponse.getMappings());
client.close();
}
}
5、指定索引插入数据
package com.ycz.ES.doc;
import com.alibaba.fastjson.JSONObject;
import com.ycz.ES.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientInsertData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
IndexRequest request = new IndexRequest();
request.index("user").id("10001");
User user = new User();
user.setAddress("深圳");
user.setAge(158);
user.setName("Alex");
Object o = JSONObject.toJSONString(user);
System.out.println(o.toString());
request.source(o.toString() , XContentType.JSON).type();
IndexResponse index = client.index(request, RequestOptions.DEFAULT);
System.out.println(index.getResult());
client.close();
}
}
6、更新数据
package com.ycz.ES.doc;
import com.alibaba.fastjson.JSONObject;
import com.ycz.ES.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientUpdateData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
UpdateRequest request = new UpdateRequest();
request.index("user").id("10001");
User user = new User();
user.setAddress("深圳");
user.setAge(158);
user.setName("asda");
Object o = JSONObject.toJSONString(user);
System.out.println(o.toString());
request.doc(o.toString() , XContentType.JSON).type();
UpdateResponse update = client.update(request, RequestOptions.DEFAULT);
System.out.println(update.getResult());
client.close();
}
}
7、查询索引指定id查询
package com.ycz.ES.doc;
import com.alibaba.fastjson.JSONObject;
import com.ycz.ES.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.get.GetIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
GetRequest request = new GetRequest();
request.index("user").id("10001");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());
client.close();
}
}
8、批量插入
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientBatchSaveData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("user").id("100002").source(XContentType.JSON , "name" , "ycz" , "age" , 22 , "address","东莞"));
request.add(new IndexRequest().index("user").id("100003").source(XContentType.JSON , "name" , "zhangsan" , "age" , 10 , "address","深圳"));
request.add(new IndexRequest().index("user").id("100004").source(XContentType.JSON , "name" , "lisi" , "age" , 30 , "address","佛山"));
BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(bulk.status());
client.close();
}
}
9、批量删除
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientBatchDELETEData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user").id("100002"));
request.add(new DeleteRequest().index("user").id("100003"));
request.add(new DeleteRequest().index("user").id("100004"));
BulkResponse bulk = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(bulk.status());
client.close();
}
}
10、指定索引查询所有数据
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetAllData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(search.getTook());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
11、分页查询
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetAllData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
query.from(0);
query.size(2);
searchRequest.source(query);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(search.getTook());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
12、分页条件查询
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetAllData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().
query(QueryBuilders.matchQuery("name" , "zhangsan"));
query.from(0);
query.size(2);
searchRequest.source(query);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(search.getTook());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
12、分页条件查询,降序
这边数据不够,再插入一条名字为张三,年龄为20的
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
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.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetAllData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().
query(QueryBuilders.matchQuery("name" , "zhangsan"));
query.sort("age" , SortOrder.DESC);
query.from(0);
query.size(2);
searchRequest.source(query);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(search.getTook());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
13、范围查询
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
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.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetRangeData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().
query(QueryBuilders.matchAllQuery());
RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("age");
rangeQueryBuilder.lte(20);
rangeQueryBuilder.gte(10);
query.query(rangeQueryBuilder);
searchRequest.source(query);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(search.getTook());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
14、模糊查询
匹配一个
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
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.Fuzziness;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetFuzzyData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder query = new SearchSourceBuilder().
query(QueryBuilders.matchAllQuery());
query.query(QueryBuilders.fuzzyQuery("name" , "zhangsan").fuzziness(Fuzziness.ONE));//这里控制匹配多少个
searchRequest.source(query);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
System.out.println(search.getTook());
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
15、聚合查询
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
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.Fuzziness;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetAggsData {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.avg("avg_age").field("age");
builder.aggregation(aggregationBuilder);
searchRequest.source(builder);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
Aggregations aggregations = search.getAggregations();
Map<String, Aggregation> asMap = aggregations.getAsMap();
for (String s : asMap.keySet()) {
System.out.println(s);
System.out.println(asMap.get(s));
}
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}
16、根据年龄分组查询
package com.ycz.ES.doc;
import org.apache.http.HttpHost;
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.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
/**
* @Description:
* @Author: Alex
* @Date 2022-06-26-22:19
* @Version: V1.0
**/
public class ESTestClientGetAggs2Data {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost" , 9200 , "http"))
);
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("user");
SearchSourceBuilder builder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("age_group").field("age");
builder.aggregation(aggregationBuilder);
searchRequest.source(builder);
SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = search.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
client.close();
}
}