3、java操作ES篇
3.1、摸索java链接ES的流程
- 自行创建一个maven项目
3.1.1、依赖管理
点击查看代码
<properties>
<ES-version>7.8.0</ES-version>
<log4j-version>1.2.17</log4j-version>
<junit-version>4.13.2</junit-version>
<jackson-version>2.13.0</jackson-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<!-- 注意:这里的版本问题,要和下载的window的ES版本一致,甚至后续用linux搭建也是一样的
到时用linux时,ES、kibana的版本都有这样的限定
-->
<version>${ES-version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<!-- 注意:这里别搞成了elasticsearch-client
这个东西在7.x已经不推荐使用了,而到了8.0之后,这个elasticsearch-client已经完全被废弃了
-->
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<!-- 同样的,注意版本问题 -->
<version>${ES-version}</version>
</dependency>
<!-- 这个我其实没用,要用的话,可以导入:log4j-api和log4j-core -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
3.1.2、摸索链接流程
3.1.2.1、获取父项目中的依赖
点击查看代码
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
3.1.2.2、摸索流程
点击查看代码
package cn.zixieqing;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName ConnectionTest
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class ConnectionTest {
/*
* @Author ZiXieQing
* @Description // TODO 测试java链接ES
* @Date 2021/12/14
* @Param
* @return
*/
// 下面这个逻辑,对照shiro中的realm、manager、FilterFactoryBean的逻辑来看( 没用过的就当我没说^_^ )
@Test
public void test() throws IOException {
// 3、创建HttpHost
HttpHost host = new HttpHost("127.0.0.1", 9200);// 发现需要:String hostname, int port 这就很简单了涩
// 当然:这个方法重载中有一个参数scheme 这个是:访问方式 根据需求用http / https都可以 这里想传的话用:http就可以了
// 2、创建RestClientBuilder 但是:点击源码发现 - 没有构造方法
// 既然没有,那肯定提供得有和xml版的mybatis加载完xml文件之后的builder之类的,找一下
RestClientBuilder clientBuilder = RestClient.builder(host);
// 发现1、有重载;2、重载之中有几个参数,而HttpHost... hosts 这个参数貌似贴近我们想要的东西了,所以建一个HttpHost
// 1、要链接client,那肯定需要一个client咯,正好:导入得有high-level-client
RestHighLevelClient esClient = new RestHighLevelClient(clientBuilder); // 发现需要RestClientBuilder,那就建
// 4、测试:只要 esClient. 就可以看到一些很熟悉的方法,可以在这里测试调一下哪些方法,然后去postman中获取数据看一下对不对
// 这里不多做说明:java链接ES客户端的流程就是上面这样的,不过:这和MySQL数据库链接一样,记得不用了就关闭
esClient.close(); // 当然:封装之后,这个关闭操作应该放出来,然后在封装的工具中只需要返回这个链接对象即可
}
}
3.2、java中操作ES索引
3.2.1、向父项目获取自己要的依赖
点击查看代码
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.2.2、封装链接对象
点击查看代码
package cn.zixieqing.utile;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @ClassName ESClientUtil
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class ESClientUtil {
private static final String HOST = "127.0.0.1"; // 用localhost也行,不过后面用linux就要ip,所以:算在这里养成习惯吧
private static final Integer PORT = 9200;
public static RestHighLevelClient getESClient() {
return new RestHighLevelClient( RestClient.builder( new HttpHost( HOST, PORT ) ) );
}
}
3.2.3、创建索引
点击查看代码
package cn.zixieqing;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName CreateIndex
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class CreateIndex {
@Test
public void createIndexTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 创建索引
// CreateIndexRequest() 第一个参数:要创建的索引名 第二个参数:请求选项 默认即可
CreateIndexResponse response = esClient.indices().create(
new CreateIndexRequest("person"), RequestOptions.DEFAULT );
// 查看是否添加成功 核心方法:isAcknowledged()
System.out.println( response.isAcknowledged() );
esClient.close();
}
}
用postman检验一下:
3.2.4、查询索引
点击查看代码
package cn.zixieqing.index;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName SearchIndex
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class SearchIndex {
/*
* @Author ZiXieQing
* @Description // TODO 查询索引
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void searchIndexTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 获取索引
GetIndexResponse response = esClient.indices().get(
new GetIndexRequest("person"), RequestOptions.DEFAULT );
// 熟悉GetIndexResponse中的几个api
System.out.println( "Aliases" + response.getAliases() );
System.out.println( "Mappings" + response.getMappings() );
System.out.println( "Settings" + response.getSettings() ); // 这三者在用postman玩的时候,返回结果中都有
esClient.close();
}
}
3.2.5、删除索引
点击查看代码
package cn.zixieqing.index;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName DeleteIndex
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class DeleteIndex {
/*
* @Author ZiXieQing
* @Description // TODO 删除索引
* @Date 2021/12/14
* @Param
* @return
*/
@Test
public void deleteIndexTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 删除索引
AcknowledgedResponse response = esClient.indices().delete(
new DeleteIndexRequest("person"), RequestOptions.DEFAULT );
// 检验一下:是否删除成功
System.out.println( response.isAcknowledged() );
esClient.close();
}
}
用postman再检测一下:
3.3、java操作ES中的_doc - 重点中的重点
3.3.1、创建doc
这里还需要jackson-databind:前面已经导入
同时:为了偷懒,所以把lombok也一起导入了
父项目依赖管理
点击查看代码
<lombok-version>1.18.22</lombok-version>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
</dependency>
子项目获取依赖:
点击查看代码
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
编写实体类
点击查看代码
package cn.zixieqing.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @ClassName UserEntity
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain = true) // lombok的链式调用
public class UserEntity implements Serializable {
private String id;
private String name;
private String sex;
}
测试:
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.entity.UserEntity;
import cn.zixieqing.utile.ESClientUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName InsertDoc
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class InsertDoc {
/*
* @Author ZiXieQing
* @Description // TODO 新增文档_doc,当然下面这个过程利用ESClient封装的思路也可以抽离
* @Date 2021/12/14
* @Param
* @return
*/
@Test
public void insertDocTest() throws IOException {
// 1、获取链接
RestHighLevelClient esClient = ESClientUtil.getESClient();
IndexRequest request = new IndexRequest();
// 选择索引及设置唯一标识
request.index("user").id("10002");
// 2、添加数据
// IndexRequest和IndexResponse这中间就是:做添加doc操作
UserEntity userEntity = new UserEntity();
userEntity.setId( "100" ).setName( "紫邪情" ).setSex( "女" ); // lombok链式调用
// 转json 注:objectMapper是jackson-databind中的,不是ES中的
String userJson = new ObjectMapper().writeValueAsString( userEntity );
// 把转成的json字符串存到ES中去
request.source( userJson , XContentType.JSON);
// 3、发起请求 获取响应对象
IndexResponse response = esClient.index( request, RequestOptions.DEFAULT );
// 看看这个IndexResponse有哪些有用的api
System.out.println( "响应转态:" + response.getResult() ); // 其他的一点 就可以看到了,都是字面意思
esClient.close();
}
}
**postman检测一下:
我的测试结构如下:
3.3.2、修改doc
- 这个修改是指的局部修改,全量修改就不用想了
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName DeleteDoc
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class UpdateDoc {
/*
* @Author ZiXieQing
* @Description // TODO 删除doc
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void deleteDoc() throws IOException {
// 1、获取链接对象
RestHighLevelClient esClient = ESClientUtil.getESClient();
UpdateRequest request = new UpdateRequest();
// 获取索引
request.index("user").id("10002");
// 2、修改doc数据
request.doc(XContentType.JSON, "name", "邪公子");
// 3、发起请求、获得响应对象
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println( "响应状态为:" + response.getResult() );
esClient.close();
}
}
postman检验一下:
3.3.3、查询doc
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName GetDoc
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class GetDoc {
@Test
public void getDocTest() throws IOException {
// 1、获取链接对象
RestHighLevelClient esClient = ESClientUtil.getESClient();
GetRequest request = new GetRequest();
request.index("user").id("10002");
// 2、发起请求、获取响应对象
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
// 3、获取结果 推荐用getSourceAsString()
String result = response.getSourceAsString();
System.out.println( "获得的doc为:" + result );
esClient.close();
}
}
3.3.4、删除doc
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName DeleteDoc
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class DeleteDoc {
/*
* @Author ZiXieQing
* @Description // TODO 删除doc
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void deleteDocTest() throws IOException {
// 1、获取链接对象
RestHighLevelClient esClient = ESClientUtil.getESClient();
DeleteRequest request = new DeleteRequest();
// 获取索引
request.index("user").id("10002");
// 2、做删除操作
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println( "响应状态为:" + response.getResult() );
// 3、关闭链接
esClient.close();
}
}
再次获取检验一下:
3.4.5、批量新增_doc数据
- 本质:把请求封装了而已,从而让这个请求可以传递各种类型参数,如:删除的、修改的、新增的,这样就可以搭配for循环
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName BatchDeleteDoc
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class BatchInsertDoc {
/*
* @Author ZiXieQing
* @Description // TODO 批量添加doc数据
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void batchInsertDocTest() throws IOException {
// 1、获取链接对象
RestHighLevelClient esClient = ESClientUtil.getESClient();
BulkRequest request = new BulkRequest();
// 当然:source的第二个参数都是传个对象,这里为了偷懒,嫖了别人的代码
request.add( new IndexRequest()
.index("user")
.id("520")
.source( XContentType.JSON, "name", "小紫1") );
request.add( new IndexRequest()
.index("user")
.id("521")
.source( XContentType.JSON, "name", "小紫2") );
request.add( new IndexRequest()
.index("user")
.id("522")
.source( XContentType.JSON, "name", "小紫3") );
// 2、发送请求
BulkResponse response = esClient.bulk( request, RequestOptions.DEFAULT );
// 查看执行时间
System.out.println( response.getTook() );
esClient.close();
}
}
postman检验一下:
点击查看代码
http://127.0.0.1:9200/user/_search 请求方式 get
// 返回结果
{
"took": 585,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "user",
"_type": "_doc",
"_id": "520",
"_score": 1.0,
"_source": {
"name": "小紫1"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "521",
"_score": 1.0,
"_source": {
"name": "小紫2"
}
},
{
"_index": "user",
"_type": "_doc",
"_id": "522",
"_score": 1.0,
"_source": {
"name": "小紫3"
}
}
]
}
}
3.4.6、批量删除_doc数据
- 本质:把请求封装了而已,从而让这个请求可以传递各种类型参数,如:删除的、修改的、新增的,这样就可以搭配for循环
点击查看代码
package cn.zixieqing.doc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName BarchDeleteDoc
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class BatchDeleteDoc {
/*
* @Author ZiXieQing
* @Description // TODO 批量删除doc数据
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void batchDeleteDoc() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
BulkRequest request = new BulkRequest();
// 和批量添加相比,变的地方就在这里而已
request.add(new DeleteRequest().index("user").id("520"));
request.add(new DeleteRequest().index("user").id("521"));
request.add(new DeleteRequest().index("user").id("522"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
esClient.close();
}
}
postman检验一下:
3.4.7、高级查询 - 重点
3.4.7.1、全量查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName MatchAll
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class MatchAll {
/*
* @Author ZiXieQing
* @Description // TODO 高级查询 - 全量查询 就是基础语法中在请求体内使用match_all那个知识点
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void matchAllTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 全量查询 match_all
SearchResponse response = esClient.search( new SearchRequest()
.indices("user")
.source(
new SearchSourceBuilder()
.query( QueryBuilders.matchAllQuery() )
), RequestOptions.DEFAULT );
// 查看执行了多少时间
System.out.println( response.getTook() );
// 把数据遍历出来看一下
for ( SearchHit data : response.getHits() ) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
// 上面的看不懂,那就看下面拆分的过程
// // 1、获取链接对象
// RestHighLevelClient esClient = ESClientUtil.getESClient();
//
// // 3、创建SearchRequest对象
// SearchRequest request = new SearchRequest();
// request.indices("user");
//
// // 5、创建SearchSourceBuilder对象
// SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//
// // 6、进行查询 发现:需要QueryBuilders对象,看源码发现:没有构造,可是:有matchAllQuery()
// searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
//
// // 4、调用source()方法获取数据,但是发现:需要SearchSourceBuilder,继续创建
// request.source( searchSourceBuilder );
//
// // 2、发送请求 发现:需要SearchRequest 那就建一个
// SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
//
// // 7、获取数据
// for (SearchHit data : response.getHits()) {
// System.out.println( data.getSourceAsString() );
// }
//
// // 8、关闭链接
// esClient.close();
}
}
3.4.7.2、条件查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName TermQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class TermQuery {
/*
* @Author ZiXieQing
* @Description // TODO term条件查询 注意:这里不是说的基础篇中的 filter range 那个条件啊( 这个条件要求的是查询字段为int类型的 )
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void termQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 条件查询
SearchResponse response = esClient.search( new SearchRequest()
.indices("user")
.source( new SearchSourceBuilder()
.query( QueryBuilders.termQuery("_id", "520" ) ) ), // 对照全量查询:变的就是这里的方法调用
RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
}
3.4.7.3、分页查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName LimitQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class LimitQuery {
/*
* @Author ZiXieQing
* @Description // TODO 分页查询 对应基础篇中的from size
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void limitQueryTest() throws IOException {
// 1、获取链接对象
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 3、创建SearchSourceBuilder
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 4、查询出所有的数据
SearchSourceBuilder sourceBuilder = searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
// 5、对数据进行分页操作
sourceBuilder.from(0);
sourceBuilder.size(2);
// 2、发送请求
SearchResponse response = esClient.search( new SearchRequest()
.indices("user")
.source( searchSourceBuilder )
, RequestOptions.DEFAULT );
// 7、查看数据
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
// 8、关闭链接
esClient.close();
}
}
3.4.7.4、排序查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName SortQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class SortQuery {
@Test
public void sortQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SearchSourceBuilder sourceBuilder = searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
// 排序 以什么字段排序、排序方式是什么( 注意:别犯低级错误啊,用字符串来搞排序 )
sourceBuilder.sort("_id", SortOrder.DESC);
SearchResponse response = esClient.search( new SearchRequest()
.indices("user")
.source( searchSourceBuilder ),
RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
}
3.4.7.5、条件过滤查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName FilterQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class FilterQuery {
/*
* @Author ZiXieQing
* @Description // TODO 查询过滤
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void filterQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 查询结果只需要什么?
String[] includes = { "name" };
// 查询结果不需要什么?
String[] excludes = {}; // 根据需求自行填充
searchSourceBuilder.fetchSource( includes,excludes );
SearchResponse response = esClient.search( new SearchRequest()
.indices("user")
.source( searchSourceBuilder )
, RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
}
我的数据没弄好,我建的doc中只有一个name,而老衲又懒得加了,所以:这里别让结果把自己搞混了
3.4.7.6、组合查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName UnionQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class UnionQuery {
/*
* @Author ZiXieQing
* @Description // TODO 组合查询至must查询
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void mustQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 注意:这里产生了改变,是调用的boolQuery()
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 必须包含什么?
boolQuery.must( QueryBuilders.matchQuery("author", "邪") );
boolQuery.must( QueryBuilders.matchQuery("sex", "girl") );
// 当然:也就有mustNot()不包含什么了
SearchResponse response = esClient.search( new SearchRequest()
.source( new SearchSourceBuilder()
.query( boolQuery ) )
, RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
/*
* @Author ZiXieQing
* @Description // TODO 组合查询之should查询
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void shouldQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 懒得烧蛇吃 不写了,知道这个should和must一样,复制粘贴多个条件即可
boolQuery.should( QueryBuilders.matchQuery("title", "是") );
SearchResponse response = esClient.search( new SearchRequest()
.source( new SearchSourceBuilder()
.query(boolQuery) )
, RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
}
must查询的结果:
should查询的结果
3.4.7.7、范围查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
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.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName RangeQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class RangeQuery {
/*
* @Author ZiXieQing
* @Description // TODO 范围查询 即:基础篇中的filter range
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void rangeQuery() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("id");
// 对结果进行处理 gt > lt < gte >= lte <=
rangeQuery.gt("10000");
SearchResponse response = esClient.search( new SearchRequest()
.source( new SearchSourceBuilder()
.query( rangeQuery ) )
, RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
}
3.4.7.8、模糊查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName FuzzyQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class FuzzyQuery {
/*
* @Author ZiXieQing
* @Description // TODO 模糊查询
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void fuzzyQuery() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 模糊查询
// fuzziness( Fuzziness.ONE ) 表示的是:字符误差数 取值有:zero、one、two、auto
// 误差数 指的是:fuzzyQuery("author","网二")这里面匹配的字符的误差嘛
// 可以有几个字符不一样 / 多 / 少几个字符?
FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("author","网二").fuzziness( Fuzziness.ONE );
SearchResponse response = esClient.search( new SearchRequest()
.source( new SearchSourceBuilder()
.query(fuzzyQuery) )
, RequestOptions.DEFAULT );
for (SearchHit data : response.getHits()) {
System.out.println( data.getSourceAsString() );
}
esClient.close();
}
}
3.4.7.9、高亮查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName HighLightQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
public class HighLightQuery {
/*
* @Author ZiXieQing
* @Description // TODO 高亮查询 highLight
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void highLightQueryTest() throws IOException {
// 1、获取链接对象
RestHighLevelClient esClient = ESClientUtil.getESClient();
// 3、创建SearchSourceBuilder对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 4、查询什么数据?
TermsQueryBuilder termsQuery = QueryBuilders.termsQuery("author", "小紫1");
// 5、构建高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
// 高亮编写
highlightBuilder.preTags("<span color='blue'>"); // 构建标签前缀
highlightBuilder.postTags("</span>"); // 构建标签后缀
highlightBuilder.field("author"); // 构建高亮字段
// 6、设置高亮
searchSourceBuilder.highlighter( highlightBuilder );
// 7、进行查询
searchSourceBuilder.query( termsQuery );
// 2、发送请求、获取响应对象
SearchResponse response = esClient.search( new SearchRequest().indices("user").source( searchSourceBuilder ) , RequestOptions.DEFAULT);
// 验证
System.out.println(response);
for (SearchHit hit : response.getHits()) {
System.out.println(hit.getSourceAsString());
System.out.println( hit.getHighlightFields());
}
// 9、关闭链接
esClient.close();
}
}
3.4.7.10、聚合查询
点击查看代码
package cn.zixieqing.docHighLevel.queryDoc;
import cn.zixieqing.utile.ESClientUtil;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import java.io.IOException;
/**
* @ClassName AggQuery
* @Author ZiXieQing
* @Date 2021/12/14
* Version 1.0
**/
// 聚合查询
public class AggQuery {
/*
* @Author ZiXieQing
* @Description // TODO 最大值查询
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void maxQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// max("maxId") 这个名字是随便取的 不讲究,就是取个名字而已 联想:有max、就有min、avg、count、sum......
// 注:方法变成term()就是分组了
// field("_id") 对哪个字段求最大值
searchSourceBuilder.aggregation( AggregationBuilders.max("maxId").field("id") );
SearchResponse response = esClient.search(new SearchRequest().source(searchSourceBuilder), RequestOptions.DEFAULT);
// 检验
System.out.println(response);
esClient.close();
}
/*
* @Author ZiXieQing
* @Description // TODO 分组查询
* @Date 2021/12/14
* @Param []
* @return void
*/
@Test
public void groupQueryTest() throws IOException {
RestHighLevelClient esClient = ESClientUtil.getESClient();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 分组
searchSourceBuilder.aggregation( AggregationBuilders.terms("groupQuery").field("author") );
SearchResponse response = esClient.search( new SearchRequest().source(searchSourceBuilder), RequestOptions.DEFAULT);
System.out.println(response);
esClient.close();
}
}