这段代码的功能是查询出es里面的数据进行分页展示,使用的jar版本为2.4
大概流程
添加maven依赖
org.elasticsearch
elasticsearch
2.4.0
properties属性文件配置
spring.elasticsearch.host=192.168.101.123
spring.elasticsearch.port=9300
spring的配置文件,初始化TransportClient 对象
package com.unioncast.ssp.front.config;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* ElasticsearchConfig
*
* @author zhangzhe
* @date 2017/2/21 16:33
*/
@Configuration
@PropertySource(value = "classpath:/elasticsearch.properties")
public class ElasticsearchConfig {
@Value("${spring.elasticsearch.host}")
private String host;
@Value("${spring.elasticsearch.port}")
private int port;
private static final Logger LOG = LogManager.getLogger(ElasticsearchConfig.class);
@Bean
public TransportClient elasticsearchClient(){ //向spring注入es的客户端操作对象
TransportClient transportClient = null;
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "bigData-cluster").build();
try {
transportClient = TransportClient
.builder()
.settings(settings)
.build()
.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(host), port)
);
} catch (UnknownHostException e) {
LOG.error("创建elasticsearch客户端失败");
}
LOG.info("创建elasticsearch客户端成功");
return transportClient;
}
}
对数据进行查询操作的实现类
package com.unioncast.ssp.front.service.ssp.elasticsearchData;
import com.unioncast.common.page.Pagination;
import com.unioncast.common.restClient.RestResponse;
import com.unioncast.common.ssp.model.SspAdvertiser;
import com.unioncast.common.ssp.model.SspCreative;
import com.unioncast.ssp.front.service.ssp.SspAdvertiserService;
import com.unioncast.ssp.front.service.ssp.SspCreativeService;
import com.unioncast.ssp.front.service.ssp.SspOrderService;
import com.unioncast.ssp.front.service.ssp.SspPlanService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.*;
/**
* @author zhangzhe
* @date 2017/2/21 10:25
*/
@Component
public class ElasticsearchADReportImpl implements ElasticsearchADReport {
private static final Logger LOG = LogManager.getLogger(ElasticsearchADReportImpl.class);
private static final String INDEX = "hehe"; //es的索引表名称
private static final String TYPE = "kafka_type"; //es的索引类型
private static final Integer PAGESIZE = 10; //分页的每页显示数
private static final String ESC = "ip"; //用于排序的字段
@Resource
TransportClient transportClient; //注入es操作对象
@Resource
SspAdvertiserService sspAdvertiserService;
@Resource
SspOrderService sspOrderService;
@Resource
SspPlanService sspPlanService;
@Resource
SspCreativeService sspCreativeService;
//es查询
public Pagination
附:
查询类型
1、query and fetch
向索引的所有分片(shard)都发出查询请求,各分片返回的时候把元素文档(document)和计算后的排名信息一起返回。这种搜索方式是最快的。因为相比下面的几种搜索方式,这种查询方法只需要去shard查询一次。但是各个shard返回的结果的数量之和可能是用户要求的size的n倍。
2、query then fetch(默认的搜索方式)
如果你搜索时,没有指定搜索方式,就是使用的这种搜索方式。这种搜索方式,大概分两个步骤,第一步,先向所有的shard发出请求,各分片只返回排序和排名相关的信息(注意,不包括文档document),然后按照各分片返回的分数进行重新排序和排名,取前size个文档。然后进行第二步,去相关的shard取document。这种方式返回的document与用户要求的size是相等的。
3、DFS query and fetch
这种方式比第一种方式多了一个初始化散发(initial scatter)步骤,有这一步,据说可以更精确控制搜索打分和排名。
4、DFS query then fetch
比第2种方式多了一个初始化散发(initial scatter)步骤。