yangyoupeng

elasticsearch client

Showing 28 changed files with 1663 additions and 0 deletions
.settings
.project
.settings
target
.classpath
.project
<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>com.zhaoonline.common</groupId>
<artifactId>zhaoonline-utils</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name></name>
<description>封装各种使用的API client.包括Elasticsearch的client</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<logback.version>1.1.7</logback.version>
<es.version>2.1.1</es.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<!-- -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${es.version}</version>
<exclusions>
<exclusion>
<groupId>com.spatial4j</groupId>
<artifactId>spatial4j</artifactId>
</exclusion>
<exclusion>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>securesm</artifactId>
</exclusion>
<exclusion>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</exclusion>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
<exclusion>
<groupId>com.ning</groupId>
<artifactId>compress-lzf</artifactId>
</exclusion>
<exclusion>
<groupId>com.tdunning</groupId>
<artifactId>t-digest</artifactId>
</exclusion>
<exclusion>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
</exclusion>
<exclusion>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.5</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version><!--$NO-MVN-MAN-VER$ -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-access</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://192.168.0.205:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://192.168.0.205:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<attach>true</attach>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
package com.zhaoonline.common.Utils;
import java.io.Closeable;
import java.io.IOException;
public class IOUtils {
public static void closeStream(Closeable stream){
if(stream!=null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.zhaoonline.common.es;
import java.util.ArrayList;
import java.util.List;
public class ESClientConfiguration {
public static String KEY_HOST="host";
public static String KEY_PORT="port";
private List<String> hostPorts=new ArrayList<String>();
public List<String> getHostPorts() {
return hostPorts;
}
public void setHostPorts(List<String> hostPorts) {
this.hostPorts = hostPorts;
}
public void addHostPorts(String hostPort){
hostPorts.add(hostPort);
}
}
package com.zhaoonline.common.es;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.zhaoonline.common.Utils.IOUtils;
import com.zhaoonline.common.es.bean.DeleteResponse;
import com.zhaoonline.common.es.bean.GetResponse;
import com.zhaoonline.common.es.bean.IndexResponse;
import com.zhaoonline.common.es.bean.InterSearchHits;
import com.zhaoonline.common.es.bean.InterSearchResponse;
import com.zhaoonline.common.es.bean.QueryResponse;
import com.zhaoonline.common.json.JsonUtils;
/**
* class name:ESClient <BR>
* class description:Elastic的客户端 <BR>
* Remark: <BR>
* @version 1.00 2016年10月24日
* @author zhaoonline)yangyoupeng
*/
public class ESHttpClient {
private static final String PATH_SEPERATOR = "/";
private String _index;
private String _type;
private CloseableHttpClient httpClient;
private ESClientConfiguration config;
private String path;
private List<HttpHost> hostsList=new ArrayList<HttpHost>();
public ESHttpClient(String index,String type,ESClientConfiguration config){
this._index=index;
this._type=type;
concatPathWithIndexType();
this.config=config;
}
private void concatPathWithIndexType() {
this.path=_index+PATH_SEPERATOR+_type;
}
public void init(){
httpClient =HttpClientFactory.newClient();
List<String> hostPosts=config.getHostPorts();
hostsList=buildHostPortListFromString(hostPosts);
}
private static List<HttpHost> buildHostPortListFromString(List<String> stringhostPortList) {
List<HttpHost> hostPorts=new ArrayList<HttpHost>(stringhostPortList.size());
for(String hostPortString: stringhostPortList){
String[] hostPortArray= hostPortString.split(":");
if(hostPortArray!=null && hostPortArray.length>=2){
String host=String.valueOf(hostPortArray[0]);
String port=String.valueOf(hostPortArray[1]);
HttpHost hostPort= new HttpHost(host,Integer.valueOf(port));
hostPorts.add(hostPort);
}
}
return hostPorts;
}
/**
* Method name: addDoc <BR>
* Description: 添加doc,指定id <BR>
* Remark: <BR>
* @param id
* @param docString
* @return boolean<BR>
* @throws IOException
* @throws UnsupportedOperationException
* @throws JsonMappingException
* @throws JsonParseException
*/
public boolean addDoc(String id, String docString) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException {
String pathWithId=path+PATH_SEPERATOR+id;
CloseableHttpResponse response= sendPostRequest(docString, pathWithId,this.hostsList);
HttpEntity responseEntity=response.getEntity();
IndexResponse indexResponse=JsonUtils.toObject(responseEntity.getContent(), IndexResponse.class);
if(indexResponse !=null){
return indexResponse.isSuccess();
}
IOUtils.closeStream(response);
return false;
}
public Map searchById(String id) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
String pathWithId=path+PATH_SEPERATOR+id;
HttpRequest request=RequestBuilder.get(pathWithId).build();
CloseableHttpResponse httpresponse= sendRequest(request,this.hostsList);
if(httpresponse ==null){
return null;
}
HttpEntity responseEntity=httpresponse.getEntity();
GetResponse response=JsonUtils.toObject(responseEntity.getContent(), GetResponse.class);
IOUtils.closeStream(httpresponse);
return response.getSource();
}
/**
* Method name: search <BR>
* Description: search 利用post方法查询doc<BR>
* Remark: <BR>
* @param queryString
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws UnsupportedOperationException
* @throws IOException List<Map><BR>
*/
public QueryResponse query(String queryString) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
String pathWithId=path+PATH_SEPERATOR+"_search";
CloseableHttpResponse response= sendPostRequest(queryString,pathWithId,this.hostsList);
HttpEntity responseEntity=response.getEntity();
String str=EntityUtils.toString(responseEntity);
InterSearchResponse searchResponse=JsonUtils.toObject(str, InterSearchResponse.class);
if(searchResponse ==null){
return null;
}
QueryResponse queryResponse =new QueryResponse();
InterSearchHits searchHits=searchResponse.getHits();
queryResponse.setTotal(searchHits.getTotal());
queryResponse.addData(searchHits.getHits());
IOUtils.closeStream(response);
return queryResponse;
}
/**
* Method name: query <BR>
* Description: 根据pagesize返回内容 <BR>
* Remark: <BR>
* @param queryBuilder
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws UnsupportedOperationException
* @throws IOException QueryResponse<BR>
*/
public QueryResponse query(ZhaoQueryBuilder queryBuilder) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
QueryResponse queryResponse=this.query(queryBuilder.toQueryString());
return queryResponse;
}
/**
* Method name: queryAll <BR>
* Description: 返回所有doc <BR>
* Remark: <BR>
* @param queryBuilder
* @return
* @throws JsonParseException
* @throws JsonMappingException
* @throws UnsupportedOperationException
* @throws IOException QueryResponse<BR>
*/
public QueryResponse queryAll(ZhaoQueryBuilder queryBuilder) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
QueryResponse returnResponse=new QueryResponse();
int size=queryBuilder.getPage().getSize();
boolean readDone=false;
while(!readDone){
QueryResponse queryResponse=this.query(queryBuilder);
int totalSize=queryResponse.getTotal().intValue();
int returnDataSize=queryResponse.getDataList().size();
returnResponse.addData(queryResponse.getDataList());
int alreadyReadSize=queryBuilder.getPage().getStart()+returnDataSize;
if(alreadyReadSize >=totalSize ){
readDone=true;
returnResponse.setTotal(new Long(totalSize));
break;
}
queryBuilder.setPage(alreadyReadSize, size);
}
return returnResponse;
}
/**
* Method name: addDoc <BR>
* Description: 添加doc,id由ES自动生成 <BR>
* Remark: <BR>
* @param docString
* @return boolean<BR>
* @throws IOException
* @throws UnsupportedOperationException
*/
public boolean addDoc(String docString) throws UnsupportedOperationException, IOException {
CloseableHttpResponse response= sendPostRequest(docString, this.path,this.hostsList);
HttpEntity responseEntity=response.getEntity();
IndexResponse indexResponse=JsonUtils.toObject(responseEntity.getContent(), IndexResponse.class);
if(indexResponse !=null){
return true;
}
IOUtils.closeStream(response);
return false;
}
private CloseableHttpResponse sendPostRequest(String source, String path,List<HttpHost> hostsList) {
HttpEntity entity=new StringEntity(source,ContentType.APPLICATION_JSON);
HttpRequest request=RequestBuilder.post(path).setEntity(entity).build();
CloseableHttpResponse response=sendRequest(request,hostsList);
return response;
}
private CloseableHttpResponse sendRequest(HttpRequest request,List<HttpHost> hostsList) {
CloseableHttpResponse response=null;
for(HttpHost httpHost:hostsList){
try {
response= httpClient.execute(httpHost, request);
if(response !=null){
return response;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return response;
}
public void close(){
IOUtils.closeStream(httpClient);
}
public boolean deleteDoc(String id) throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException {
String pathWithId=path+PATH_SEPERATOR+id;
HttpRequest request=RequestBuilder.delete(pathWithId).build();
CloseableHttpResponse response= sendRequest(request,this.hostsList);
if(response ==null){
return false;
}
HttpEntity responseEntity=response.getEntity();
DeleteResponse deleteResponse=JsonUtils.toObject(responseEntity.getContent(), DeleteResponse.class);
IOUtils.closeStream(response);
return deleteResponse.isSuccess();
}
}
package com.zhaoonline.common.es;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
public class HttpClientFactory {
private static Integer SOCKET_TIMEOUT =10000;
private static Integer CONNECTION_TIMEOUT=2000;
public static final CloseableHttpClient newClient() {
final HttpClientBuilder builder = HttpClientBuilder.create();
builder.setConnectionManager(newConnectionManager());
final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(CONNECTION_TIMEOUT).build();
builder.setDefaultRequestConfig(requestConfig);
CloseableHttpClient httpclient = builder.build();
return httpclient;
}
public static final HttpClientConnectionManager newConnectionManager() {
// 默认支持http和https协议
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(200);
cm.setDefaultMaxPerRoute(20);
cm.setValidateAfterInactivity(0);
return cm;
}
}
package com.zhaoonline.common.es;
public class Page {
private int start=0;
private int size=10;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
package com.zhaoonline.common.es;
import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.bucket.missing.Missing;
import com.zhaoonline.common.es.bean.MissQuery;
import com.zhaoonline.common.es.bean.QueryInt;
import com.zhaoonline.common.es.bean.RangeQuery;
import com.zhaoonline.common.es.bean.TermQuery;
import com.zhaoonline.common.es.bean.TermsQuery;
/**
* class name:ZhaoBooleanQuery <BR>
* class description: 本解析器,解析出来查询,作为es的filter <BR>
* Remark: <BR>
* @version 1.00 2016年10月25日
* @author zhaoonline)yangyoupeng
*/
public class ZhaoBooleanFilterQuery implements ZhaoQueryParser{
@Override
public String parse(ZhaoQueryBuilder queryBuilder) {
BoolQueryBuilder booleanQuery=QueryBuilders.boolQuery();
List<QueryInt> andQuery=queryBuilder.getAndQuery();
processMust(booleanQuery, andQuery);
processShould(queryBuilder, booleanQuery);
processMustNot(queryBuilder, booleanQuery);
return QueryBuilders.constantScoreQuery(booleanQuery).toString();
}
private void processMustNot(ZhaoQueryBuilder queryBuilder, BoolQueryBuilder booleanQuery) {
List<QueryInt> notQuery=queryBuilder.getNotQuery();
for(QueryInt q:notQuery){
if(q instanceof TermQuery){
TermQuery tq=(TermQuery)q;
booleanQuery.mustNot(QueryBuilders.termQuery(tq.getTerm(), tq.getTermValue()));
}
if(q instanceof TermsQuery){
TermsQuery tsq=(TermsQuery)q;
booleanQuery.mustNot(QueryBuilders.termsQuery(tsq.getTerm(), tsq.getValues()));
}
if(q instanceof RangeQuery){
RangeQuery rq=(RangeQuery)q;
booleanQuery.mustNot(QueryBuilders.rangeQuery(rq.getRangeKey()).from(rq.getFrom()).to(rq.getTo()));
}
if(q instanceof MissQuery){
MissQuery mq=(MissQuery)q;
booleanQuery.mustNot(QueryBuilders.missingQuery(mq.getMissingfield()));
}
}
}
private void processShould(ZhaoQueryBuilder queryBuilder, BoolQueryBuilder booleanQuery) {
List<QueryInt> orQuery=queryBuilder.getOrQuery();
for(QueryInt q:orQuery){
if(q instanceof TermQuery){
TermQuery tq=(TermQuery)q;
booleanQuery.should(QueryBuilders.termQuery(tq.getTerm(), tq.getTermValue()));
}
if(q instanceof TermsQuery){
TermsQuery tsq=(TermsQuery)q;
booleanQuery.should(QueryBuilders.termsQuery(tsq.getTerm(), tsq.getValues()));
}
if(q instanceof RangeQuery){
RangeQuery rq=(RangeQuery)q;
booleanQuery.should(QueryBuilders.rangeQuery(rq.getRangeKey()).from(rq.getFrom()).to(rq.getTo()));
}
if(q instanceof MissQuery){
MissQuery mq=(MissQuery)q;
booleanQuery.should(QueryBuilders.missingQuery(mq.getMissingfield()));
}
}
}
private void processMust(BoolQueryBuilder booleanQuery, List<QueryInt> andQuery) {
for(QueryInt q:andQuery){
if(q instanceof TermQuery){
TermQuery tq=(TermQuery)q;
booleanQuery.must(QueryBuilders.termQuery(tq.getTerm(), tq.getTermValue()));
}
if(q instanceof TermsQuery){
TermsQuery tsq=(TermsQuery)q;
booleanQuery.must(QueryBuilders.termsQuery(tsq.getTerm(), tsq.getValues()));
}
if(q instanceof RangeQuery){
RangeQuery rq=(RangeQuery)q;
booleanQuery.must(QueryBuilders.rangeQuery(rq.getRangeKey()).from(rq.getFrom()).to(rq.getTo()));
}
if(q instanceof MissQuery){
MissQuery mq=(MissQuery)q;
booleanQuery.must(QueryBuilders.missingQuery(mq.getMissingfield()));
}
}
}
}
package com.zhaoonline.common.es;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilders;
import com.zhaoonline.common.es.bean.QueryInt;
import com.zhaoonline.common.es.bean.SortOrder;
import com.zhaoonline.common.es.bean.SortQueryBean;
public class ZhaoQueryBuilder {
private List<String> fields =new ArrayList<String>();
private List<QueryInt> andQuery=new ArrayList<QueryInt>();
private List<QueryInt> orQuery=new ArrayList<QueryInt>();
private List<QueryInt> notQuery=new ArrayList<QueryInt>();
private List<SortQueryBean> sortQuerys=new ArrayList<SortQueryBean>();
private Page page =new Page();
private ZhaoQueryParser parser=null;
public ZhaoQueryBuilder(){
this.parser=new ZhaoBooleanFilterQuery();
}
public ZhaoQueryBuilder(ZhaoQueryParser parser){
this.parser=parser;
}
public ZhaoQueryBuilder addFiled(String field){
fields.add(field);
return this;
}
public void setPage(int start,int size){
page=new Page();
page.setSize(size);
page.setStart(start);
}
public void and(QueryInt... querys){
for(QueryInt tq:querys){
andQuery.add(tq);
}
}
public void or(QueryInt... querys){
for(QueryInt tq:querys){
orQuery.add(tq);
}
}
public void not(QueryInt... querys){
for(QueryInt tq:querys){
notQuery.add(tq);
}
}
public void addSortOrder(String sortKey,SortOrder order){
sortQuerys.add(new SortQueryBean(sortKey,order));
}
public String toQueryString(){
StringBuilder query=new StringBuilder();
query.append("{");
String pageString=toPageString();
if(pageString !=null){
query.append(pageString);
query.append(",");
}
String fieldsString=toFieldsString();
if(fieldsString != null){
query.append(fieldsString);
query.append(",");
}
String interQueryString = toRawQueryString(query);
query.append("\"query\":");
if(interQueryString !=null){
query.append(interQueryString);
}else{
query.append(QueryBuilders.matchAllQuery());
}
String sortString=toSortString();
if(sortString != null){
query.append(",");
query.append(sortString);
}
query.append("}");
return query.toString();
}
private String toRawQueryString(StringBuilder query) {
String interQueryString=parser.parse(this);
return interQueryString;
}
public String toSortString() {
if(sortQuerys!=null && sortQuerys.size() !=0){
StringBuilder builder=new StringBuilder();
builder.append("\"sort\":").append("[");
Iterator<SortQueryBean> itr=sortQuerys.iterator();
while(itr.hasNext()){
SortQueryBean field=itr.next();
builder.append("{").append("\"").append(field.getSortKey()).append("\"").append(":").append("\"").append(field.getSortOrder().getValue()).append("\"").append("}");
if(itr.hasNext()){
builder.append(",");
}
}
builder.append("]");
return builder.toString();
}
return null;
}
public String toFieldsString(){
if(fields!=null && fields.size() !=0){
StringBuilder builder=new StringBuilder();
builder.append("\"fields\":").append("[");
Iterator<String> itr=fields.iterator();
while(itr.hasNext()){
String field=itr.next();
builder.append("\"").append(field).append("\"");
if(itr.hasNext()){
builder.append(",");
}
}
builder.append("]");
return builder.toString();
}
return null;
}
public String toPageString(){
if(page != null){
StringBuilder builder=new StringBuilder();
builder.append("\"from\":").append(page.getStart());
builder.append(",");
builder.append("\"size\":").append(page.getSize());
return builder.toString();
}
return null;
}
public List<String> getFields() {
return fields;
}
public void setFields(List<String> fields) {
this.fields = fields;
}
public List<QueryInt> getAndQuery() {
return andQuery;
}
public void setAndQuery(List<QueryInt> andQuery) {
this.andQuery = andQuery;
}
public List<QueryInt> getOrQuery() {
return orQuery;
}
public void setOrQuery(List<QueryInt> orQuery) {
this.orQuery = orQuery;
}
public List<QueryInt> getNotQuery() {
return notQuery;
}
public void setNotQuery(List<QueryInt> notQuery) {
this.notQuery = notQuery;
}
public List<SortQueryBean> getSortQuerys() {
return sortQuerys;
}
public void setSortQuerys(List<SortQueryBean> sortQuerys) {
this.sortQuerys = sortQuerys;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public ZhaoQueryParser getParser() {
return parser;
}
public void setParser(ZhaoQueryParser parser) {
this.parser = parser;
}
}
package com.zhaoonline.common.es;
public interface ZhaoQueryParser {
public String parse(ZhaoQueryBuilder queryBuilder);
}
package com.zhaoonline.common.es.bean;
import com.fasterxml.jackson.annotation.JsonProperty;
public class CommonESResponse {
@JsonProperty("_index")
protected String index;
@JsonProperty("_id")
protected String id;
@JsonProperty("_type")
protected String type;
@JsonProperty("_version")
protected long version;
public String getIndex() {
return index;
}
public void setIndex(String index) {
this.index = index;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getVersion() {
return version;
}
public void setVersion(long version) {
this.version = version;
}
}
package com.zhaoonline.common.es.bean;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public class DeleteResponse extends CommonESResponse {
@JsonProperty("found")
private boolean found;
@JsonProperty("_shards")
private ShardInfo shards;
public boolean isFound() {
return found;
}
public void setFound(boolean found) {
this.found = found;
}
public ShardInfo getShards() {
return shards;
}
public void setShards(ShardInfo shards) {
this.shards = shards;
}
@JsonIgnore
public boolean isSuccess(){
if(shards !=null){
return (shards.getFailed() == 0);
}
return false;
}
}
package com.zhaoonline.common.es.bean;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
public class GetResponse extends CommonESResponse {
@JsonProperty("found")
private boolean found;
public boolean isFound() {
return found;
}
public void setFound(boolean found) {
this.found = found;
}
@JsonProperty("_source")
private Map source;
public Map getSource() {
return source;
}
public void setSource(Map source) {
this.source = source;
}
}
package com.zhaoonline.common.es.bean;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
public class IndexResponse extends CommonESResponse{
@JsonProperty("created")
private boolean created;
@JsonProperty("_shards")
private ShardInfo shards;
public boolean isCreated() {
return created;
}
public void setCreated(boolean created) {
this.created = created;
}
public ShardInfo getShards() {
return shards;
}
public void setShards(ShardInfo shards) {
this.shards = shards;
}
@JsonIgnore
public boolean isSuccess(){
if(shards !=null){
return (shards.getFailed() == 0);
}
return false;
}
}
package com.zhaoonline.common.es.bean;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
public class InterSearchHits {
private long total;
private List<Map<String,Object>> hits;
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<Map<String, Object>> getHits() {
return hits;
}
public void setHits(List<Map<String, Object>> hits) {
this.hits = hits;
}
}
package com.zhaoonline.common.es.bean;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class InterSearchResponse {
@JsonProperty("timed_out")
private boolean timedOut;
@JsonProperty("took")
private long took;
@JsonProperty("_shards")
private ShardInfo shard;
private InterSearchHits hits;
public boolean isTimedOut() {
return timedOut;
}
public void setTimedOut(boolean timedOut) {
this.timedOut = timedOut;
}
public long getTook() {
return took;
}
public void setTook(long took) {
this.took = took;
}
public ShardInfo getShard() {
return shard;
}
public void setShard(ShardInfo shard) {
this.shard = shard;
}
public InterSearchHits getHits() {
return hits;
}
public void setHits(InterSearchHits hits) {
this.hits = hits;
}
}
package com.zhaoonline.common.es.bean;
public class MissQuery implements QueryInt {
private String missingfield;
public MissQuery(String missingfield){
this.missingfield=missingfield;
}
public String getMissingfield() {
return missingfield;
}
public void setMissingfield(String missingfield) {
this.missingfield = missingfield;
}
@Override
public String getType() {
return "missing";
}
@Override
public String toQueryString() {
return null;
}
}
package com.zhaoonline.common.es.bean;
public interface QueryInt {
public String getType();
public String toQueryString();
}
package com.zhaoonline.common.es.bean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class QueryResponse {
private Long total;
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
private List<Map<String,Object>> dataList=new ArrayList<>();
public List<Map<String, Object>> getDataList() {
return dataList;
}
public void setDataList(List<Map<String, Object>> data) {
this.dataList = data;
}
public void addData(Map<String, Object> source) {
dataList.add(source);
}
public void addData(List<Map<String, Object>> data) {
dataList.addAll(data);
}
}
package com.zhaoonline.common.es.bean;
import org.elasticsearch.index.query.QueryBuilders;
public class RangeQuery implements QueryInt {
private Object from;
private Object to;
private String rangeKey;
public RangeQuery(String rangeKey,Object from,Object to){
this.rangeKey=rangeKey;
this.from=from;
this.to=to;
}
public Object getFrom() {
return from;
}
public void setFrom(Object from) {
this.from = from;
}
public Object getTo() {
return to;
}
public void setTo(Object to) {
this.to = to;
}
public String getRangeKey() {
return rangeKey;
}
public void setRangeKey(String rangeKey) {
this.rangeKey = rangeKey;
}
@Override
public String getType() {
return "range";
}
@Override
public String toQueryString() {
return QueryBuilders.rangeQuery(rangeKey).from(from).to(to).toString();
}
}
package com.zhaoonline.common.es.bean;
public class ShardInfo {
private int total;
private int successful;
private int failed;
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getSuccessful() {
return successful;
}
public void setSuccessful(int successful) {
this.successful = successful;
}
public int getFailed() {
return failed;
}
public void setFailed(int failed) {
this.failed = failed;
}
}
package com.zhaoonline.common.es.bean;
public enum SortOrder {
DESC("DESC"),ASC("ASC");
private final String orderType;
SortOrder(String orderType){
this.orderType=orderType;
}
public String getValue() { return orderType.toLowerCase(); }
}
package com.zhaoonline.common.es.bean;
public class SortQueryBean {
private String sortKey;
private SortOrder sortOrder;
public SortQueryBean(String sortKey,SortOrder order){
this.sortKey=sortKey;
this.sortOrder=order;
}
public String getSortKey() {
return sortKey;
}
public void setSortKey(String sortKey) {
this.sortKey = sortKey;
}
public SortOrder getSortOrder() {
return sortOrder;
}
public void setSortOrder(SortOrder sortOrder) {
this.sortOrder = sortOrder;
}
}
package com.zhaoonline.common.es.bean;
import javax.management.Query;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
public class TermQuery implements QueryInt{
public String term;
private Object termValue;
public TermQuery(String term, Object termValue) {
this.term=term;
this.termValue=termValue;
}
public String getTerm() {
return term;
}
public void setTerm(String term) {
this.term = term;
}
public Object getTermValue() {
return termValue;
}
public void setTermValue(Object termValue) {
this.termValue = termValue;
}
@Override
public String getType() {
return "term";
}
@Override
public String toQueryString() {
return QueryBuilders.termQuery(term, termValue).toString();
}
}
package com.zhaoonline.common.es.bean;
import java.util.ArrayList;
import java.util.List;
public class TermsQuery implements QueryInt{
private String term;
private List<Object> values=new ArrayList<Object>();
public TermsQuery(String term){
this.term=term;
}
public TermsQuery addValue(Object value){
values.add(value);
return this;
}
public String getTerm(){
return term;
}
public List<Object> getValues(){
return values;
}
@Override
public String getType() {
return null;
}
@Override
public String toQueryString() {
return null;
}
}
package com.zhaoonline.common.json;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
private static ObjectMapper mapper= new ObjectMapper();
/**
* Method name: toObject <BR>
* Description: 读取失败就返回null <BR>
* Remark: <BR>
* @param inputStram
* @param clazz
* @return T<BR>
* @throws IOException
* @throws JsonMappingException
* @throws JsonParseException
*/
public static <T> T toObject(InputStream inputStram ,Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
return mapper.readValue(inputStram, clazz);
}
public static String toJson(Object object) throws JsonProcessingException {
return mapper.writeValueAsString(object);
}
public static <T> T toObject(String jsonString,Class<T> clazz) throws IOException {
return mapper.readValue(jsonString, clazz);
}
public static Map toMap(String jsonString) throws IOException {
return toObject(jsonString,Map.class);
}
}
package com.zhaoonline.common.es;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.zhaoonline.common.es.bean.DeleteResponse;
import com.zhaoonline.common.es.bean.QueryResponse;
import com.zhaoonline.common.es.bean.TermQuery;
import com.zhaoonline.common.es.bean.TermsQuery;
import com.zhaoonline.common.json.JsonUtils;
public class TestESClient {
@Test
public void testAddDoc() throws UnsupportedOperationException, IOException{
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
Map object=new HashMap<>();
object.put("testKey", "testValue");
String docString=JsonUtils.toJson(object);
String id="1";
boolean result=client.addDoc(id,docString);
Assert.assertTrue(result);
}
@Test
public void testAddDocAutoId() throws UnsupportedOperationException, IOException{
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
Map object=new HashMap<>();
object.put("testKey", "testValue");
String docString=JsonUtils.toJson(object);
boolean result=client.addDoc(docString);
Assert.assertTrue(result);
}
@Test
public void testDeleteDocById() throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
//make sure the doc insert
testAddDoc();
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
String id="1";
boolean response=client.deleteDoc(id);
Assert.assertTrue(response);
}
@Test
public void testGetDocById() throws UnsupportedOperationException, IOException{
testAddDoc();
String index="zhaoon1";
String type="test1";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
Map source= client.searchById("1");
Assert.assertNotNull(source);
}
@Test
public void testQuery() throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
String index="zhaoonline";
String type="service";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
buidler.setPage(0,100);
TermsQuery termsQuery=new TermsQuery("serviceName");
termsQuery.addValue("dubbo2").addValue("dubbo23");
buidler.and(termsQuery);
TermQuery termQuery=new TermQuery("serviceName", "dubbo2");
buidler.not(termQuery);
QueryResponse reponse=client.query(buidler);
Assert.assertNotNull(reponse);
System.out.println( reponse.getDataList().size());
Assert.assertEquals(new Long(1), reponse.getTotal());
client.close();
}
@Test
public void testQueryWithPageInfo() throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
String index="zhaoonline";
String type="service";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
buidler.setPage(0,100);
QueryResponse reponse=client.query(buidler);
Assert.assertNotNull(reponse);
System.out.println(reponse.getDataList().size());
Assert.assertEquals(reponse.getTotal().intValue(),reponse.getDataList().size());
client.close();
}
@Test
public void testQueryAll() throws JsonParseException, JsonMappingException, UnsupportedOperationException, IOException{
String index="zhaoonline";
String type="service";
ESClientConfiguration config=new ESClientConfiguration();
config.addHostPorts("127.0.0.1:9200");
ESHttpClient client= new ESHttpClient(index,type,config);
client.init();
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
QueryResponse reponse=client.queryAll(buidler);
Set<String> idset=new HashSet<>();
for(Map<String, Object> map:reponse.getDataList()){
idset.add((String) map.get("_id"));
}
Assert.assertNotNull(reponse);
Assert.assertEquals(reponse.getTotal().intValue(), idset.size());
client.close();
}
}
package com.zhaoonline.common.es;
import org.junit.Assert;
import org.junit.Test;
import com.fasterxml.jackson.databind.deser.impl.ExternalTypeHandler.Builder;
import com.zhaoonline.common.es.bean.QueryInt;
import com.zhaoonline.common.es.bean.SortOrder;
import com.zhaoonline.common.es.bean.TermQuery;
import com.zhaoonline.common.es.bean.TermsQuery;
public class TestZhaoQueryBuilder {
@Test
public void testToFieldsString(){
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
buidler.addFiled("test1").addFiled("test2");
String filedsString=buidler.toFieldsString();
Assert.assertEquals("\"fields\":[\"test1\",\"test2\"]", filedsString);
}
@Test
public void testToFieldsStringNull(){
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
// buidler.addFiled("test1").addFiled("test2");
String filedsString=buidler.toFieldsString();
Assert.assertNull(filedsString);
}
@Test
public void testtoSortString(){
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
buidler.addSortOrder("test1", SortOrder.ASC);
buidler.addSortOrder("test2", SortOrder.DESC);
String sortsString=buidler.toSortString();
System.out.println(sortsString);
Assert.assertEquals("\"sort\":[{\"test1\":\"asc\"},{\"test2\":\"desc\"}]", sortsString);
}
@Test
public void testtoPageString(){
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
buidler.setPage(10, 100);
String pageString=buidler.toPageString();
System.out.println(pageString);
Assert.assertEquals("\"from\":10,\"size\":100", pageString);
}
@Test
public void testtoQueryString(){
ZhaoQueryBuilder buidler=new ZhaoQueryBuilder();
buidler.addFiled("test1").addFiled("test2");
QueryInt querys=new TermQuery("test2", "123445");
QueryInt querys1=new TermQuery("test1", "123442");
buidler.and(querys,querys1);
TermsQuery notQuery=new TermsQuery("test1");
notQuery.addValue("121");
notQuery.addValue("123");
buidler.not(notQuery);
buidler.addSortOrder("test1", SortOrder.ASC);
buidler.addSortOrder("test2", SortOrder.DESC);
buidler.setPage(10, 100);
String queryString=buidler.toQueryString();
System.out.println(queryString);
}
}