首页 > 代码库 > ES查询操作

ES查询操作

/**
*
* method_name:simpleSearch
* date:2016年11月4日上午11:10:53
* author:terry
* return_type:List<Map<String,Object>>
* description:简单查询
*/
public static List<Map<String,Object>> simpleSearch(String alias,String[] indexNames,String[] types, HashMap<String, Object[]> mustSearchContentMap, int from, int offset, String sortField, String sortType,String exisiFiled){
long sTime = System.currentTimeMillis();
if (offset <= 0) {
return null;
}
try {
SearchRequestBuilder searchRequestBuilder = searchClient.prepareSearch(alias).setSearchType(SearchType.DEFAULT)//SearchType.DEFAULT
.setFrom(from).setSize(offset).setExplain(true);
if(types != null){
searchRequestBuilder.setTypes(types);
}
if (sortField == null || sortField.isEmpty() || sortType == null || sortType.isEmpty()) {
/*如果不需要排序*/
}
else {
/*如果需要排序*/
org.elasticsearch.search.sort.SortOrder sortOrder = sortType.equals("desc") ? org.elasticsearch.search.sort.SortOrder.DESC : org.elasticsearch.search.sort.SortOrder.ASC;
searchRequestBuilder = searchRequestBuilder.addSort(sortField, sortOrder);
}

if(mustSearchContentMap == null){//查询条件为空 则查询所有数据
searchRequestBuilder = searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
}else{
BoolQueryBuilder bool = EsTools.createQueryBuilder(mustSearchContentMap, SearchLogic.must);
bool.filter(QueryBuilders.existsQuery(exisiFiled));
searchRequestBuilder = searchRequestBuilder.setQuery(bool);
}
//
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
long eTime = System.currentTimeMillis();
long time = eTime - sTime;
logger.info("====================simpleSearch查询耗时:"+time+"毫秒===============================");
return getSearchResult(searchResponse);
}
catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
}
return null;
}

ES查询操作