ES查询操作

Posted 街机玩家

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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查询操作的主要内容,如果未能解决你的问题,请参考以下文章

Es7.x使用RestHighLevelClient进行查询操作

es的语法操作命令

架构师成长记_第八周_08_ES-文档的基本操作

架构师成长记_第八周_08_ES-文档的基本操作

Es7.x使用RestHighLevelClient进行查询操作

ES查询操作