public class EteEsConstants {
public static final String INDEX_NAME_SUFFIX_KEY = "indexNameSuffix";
public static final String ES_QUERY_STRING_KEY = "esQueryString";
public static final String ETE_ES_ID_KEY = "id";
public static final String ETE_ES_SEARCH = "/_search"
}
---------------------------------------------------------------------------- 分割線 ------------------------------------------------------------------------------
import com.alibaba.fastjson.JSONObject;
import com.*.logengine.ete.elasticsearch.util.ElasticsearchUtil;
import com.*.logengine.ete.elasticsearch.util.EteEsConstants;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import java.io.IOException;
public abstract class EsBaseDao {
private static final String ES_DOC_TYPE = "doc_type"; // 以什麼形式返回查詢結果
private static final String ETE_ES_ID_KEY = EteEsConstants.ETE_ES_ID_KEY;
private static final String ETE_ES_SEARCH = EteEsConstants.ETE_ES_SEARCH;
public String create(JSONObject jsonObject,String indexNameSuffix){
// String indexNameSuffix = jsonObject.getString(EteEsConstants.INDEX_NAME_SUFFIX_KEY);
String indexName = getIndexFullName(indexNameSuffix);
return ElasticsearchUtil.addData(indexName, ES_DOC_TYPE, jsonObject.getString(ETE_ES_ID_KEY), jsonObject);
}
public String update(JSONObject jsonObject,String indexName){
return ElasticsearchUtil.modifyData(indexName, ES_DOC_TYPE, jsonObject.getString(ETE_ES_ID_KEY), jsonObject);
}
public String queryByAlias(IndexRequest indexRequest) throws IOException {
String method = ElasticsearchUtil.HTTP_METHOD_POST;
return ElasticsearchUtil.queryData(method,indexRequest,getEndPoint(getAliasIndexName()));
}
public String query(String queryString) throws IOException {
String method = ElasticsearchUtil.HTTP_METHOD_POST;
return ElasticsearchUtil.search(method,getEndPoint(getAliasIndexName()),queryString);
}
public JSONObject searchByReturnJSONObject(String queryString) throws IOException {
String method = ElasticsearchUtil.HTTP_METHOD_POST;
return ElasticsearchUtil.searchByReturnJSONObject(method,getEndPoint(getAliasIndexName()),queryString);
}
public JSONObject query(BoolQueryBuilder queryBuilders) throws IOException {
String queryStr = "{" +
"\"query\":"+
queryBuilders.toString()+
"}";
String method = ElasticsearchUtil.HTTP_METHOD_POST;
return ElasticsearchUtil.searchByReturnJSONObject(method,getEndPoint(getAliasIndexName()),queryStr);
}
public String query(String queryString,String indexNameSuffix) throws IOException {
String method = ElasticsearchUtil.HTTP_METHOD_GET;
String endPoint = getEndPoint(getIndexFullName(indexNameSuffix));
return ElasticsearchUtil.search(method,endPoint,queryString);
}
private String getIndexFullName(String indexSuffix){
return getIndexNamePrefix()+indexSuffix;
}
private String getEndPoint(String indexFullNam){
return "/"+indexFullNam+"/"+ ES_DOC_TYPE +ETE_ES_SEARCH;
}
protected abstract String getIndexNamePrefix(); // 抽象類,需要實現在繼承了EsBaseDao的Dao裏,以獲取需要查詢的索引名或索引名前綴
protected abstract String getAliasIndexName(); // 抽象類,需要實現在繼承了EsBaseDao的Dao裏,以獲取需要查詢的索引名別名
public String getEsDocType(){
return ES_DOC_TYPE;
};
}