1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
public static List<Map<String, Object>> searchListData( String index, String type, QueryBuilder query, Integer size, String fields, String sortField, String highlightField) {
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index); if (StringUtils.isNotEmpty(type)) { searchRequestBuilder.setTypes(type.split(",")); }
if (StringUtils.isNotEmpty(highlightField)) { String[] highlightFields = highlightField.split(","); HighlightBuilder.Field field = null; HighlightBuilder highlightBuilder = new HighlightBuilder(); for(String f : highlightFields) { field = new HighlightBuilder.Field(f); field.preTags("<span style=\"color:red\">").postTags("</span>"); highlightBuilder.field(field); } searchRequestBuilder.highlighter(highlightBuilder); }
searchRequestBuilder.setQuery(query);
if (StringUtils.isNotEmpty(fields)) { searchRequestBuilder.setFetchSource(fields.split(","), null); } searchRequestBuilder.setFetchSource(true);
if (StringUtils.isNotEmpty(sortField)) { searchRequestBuilder.addSort(sortField, SortOrder.DESC); }
if (size != null && size > 0) { searchRequestBuilder.setSize(size); }
LOGGER.info("\n{}", searchRequestBuilder);
SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
long totalHits = searchResponse.getHits().totalHits; long length = searchResponse.getHits().getHits().length;
LOGGER.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);
if (searchResponse.status().getStatus() == 200) { return setSearchResponse(searchResponse, highlightField); } return null;
}
|