[color=rgba(0, 0, 0, 0.75)]使用jestClient查询ES常见问题
[color=rgba(0, 0, 0, 0.75)]使用JestClient快速查询ES
[color=rgba(0, 0, 0, 0.75)]众所周知,ES提供连接的方法有两种,一种是HTTP请求方式,一种是TCP/IP方式,ES对这两种方式提供了不同的接口,可以在ES的配置文件中查看详细端口号,在此不在赘述。
现在微服务的全家桶来说,基于spring boot和spring cloud很多情况下需要使用ES中间件进行存储一些数据或者是实现一些功能,比如分词、改造队列等等。所以spring也有相应的spring-data-ElasticSeach组件包连接ES进行相应的操作,其底层是使用了TCP/IP进行连接的,因为ES之后的版本TCP/IP的连接方式将不再支持,所以我们对新版本的ES使用JestClient进行连接。
开始我们的查询旅途吧~
首先加入我们的jestClient和ES的依赖 - <dependency>
- <groupId>io.searchbox</groupId>
- <artifactId>jest</artifactId>
- <version>5.3.4</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>6.3.2</version>
- </dependency>
复制代码指定我们的ES服务地址,当然是http连接的端口地址 - spring.elasticsearch.jest.uris=http://127.0.0.1:9200
复制代码 [color=rgba(0, 0, 0, 0.749019607843137)]
接下来我们写一个简单精确查询的例子,注意如果是要查询英文,所传入的值应该做小写处理,不然搜不到
- import io.searchbox.client.JestClient;
- import io.searchbox.client.JestResult;
- import io.searchbox.core.Search;
- import org.elasticsearch.index.query.QueryBuilders;
- import org.elasticsearch.search.builder.SearchSourceBuilder;
- import org.springframework.beans.factory.annotation.Autowired;
- import java.io.IOException;
- public class test {
- @Autowired
- JestClient jestClient;
- public void seach() throws IOException {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.termQuery("字段名","123"));
- Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
- JestResult jestResult = jestClient.execute(search);
- }
- }
复制代码 然后使用jestResult获取各种你想要的查询值
要想改变搜索条件,那么参考下面的方法
模糊搜索,*为通配符
- public void seach() throws IOException {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.wildcardQuery("字段名","*123*"));
- Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
- JestResult jestResult = jestClient.execute(search);
- }
复制代码 模糊搜索+分页请求,使用方法类似于sql里的limit
- public void seach() throws IOException {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.wildcardQuery("字段名","*123*"));
- searchSourceBuilder.from(0).size(10);
- Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
- JestResult jestResult = jestClient.execute(search);
- }
复制代码 搜索排序,其中排序方法使用org.elasticsearch.search.sort.SortOrder里的方法
- public void seach() throws IOException {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.wildcardQuery("字段名","*123*"));
- searchSourceBuilder.from(0).size(10).sort("排序字段名", SortOrder.DESC);
- Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
- JestResult jestResult = jestClient.execute(search);
- }
复制代码 区间查询,其中rangeQuery也可只传起始值,或者只传终值,就是大于小于的意思
- public void seach() throws IOException {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- searchSourceBuilder.query(QueryBuilders.rangeQuery("字段名").from("起始值").to("终值"));
- Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
- JestResult jestResult = jestClient.execute(search);
- }
复制代码 多重条件查询,在这里就需要注意了,我们往往会遇到多种条件一起查询的场景。由于每一次调用SearchSourceBuilder的query方法都会进行重新覆盖对象,也就是说SearchSourceBuilder只会使用最后一次query方法获取的查询语句,所以当多重条件执行的时候使用boolQueryBuilder的must函数进行条件的拼接可以完美解决这个问题。
- public void Search(String id,String phone, Date startTime, Date endTime, Integer pageNum, Integer pageSize) throws ParseException {
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
- if (id!=null){
- boolQueryBuilder.must(QueryBuilders.termQuery("id", id.toLowerCase()));
- }
- if (phone!=null){
- boolQueryBuilder.must(QueryBuilders.wildcardQuery("phone","*"+phone+"*"));
- }
- if (startTime!=null||endTime!=null){boolQueryBuilder.must(QueryBuilders.rangeQuery("DateTime").from(startTime).to(endTime));
- }
- if (pageNum!=null&&pageNum!=0){
- searchSourceBuilder.from((pageNum-1)*pageSize).size(pageSize).sort("DateTime", SortOrder.DESC);
- }
- Search search =new Search.Builder(searchSourceBuilder.toString()).addIndex("索引名").addType("type名").build();
- JestResult jestResult = jestClient.execute(search);
- }
复制代码 因为ES的特殊性,我们往往查询的时候会查出很多数据,所以ES在不加入size的查询请求时只会默认返回10条数据,所以我们一般查询指定条件的时候都会加上限制个数(size)的条件.
看到这里你应该对通过jestClient查询ES小有心得了,快去实现吧。
|