controller

查询标题或者内容中含有关键字的数据。

1
2
3
4
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword,
"content", "title");
List<Map<String, Object>> list = ElasticsearchUtil.
searchListData(indexName, esType, queryBuilder, 10, "content,title", null, "content,title");

查询

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
/**
* 使用分词查询
*
* @param index 索引名称
* @param type 类型名称,可传入多个type逗号分隔
* @param query 查询条件
* @param size 文档大小限制
* @param fields 需要显示的字段,逗号分隔(缺省为全部字段)
* @param sortField 排序字段
* @param highlightField 高亮字段
* @return
*/
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);
}

//打印的内容 可以在 Elasticsearch head 和 Kibana 上执行查询
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;

}

设置高亮

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private static List<Map<String, Object>> setSearchResponse(SearchResponse searchResponse, String highlightField) {
List<Map<String, Object>> sourceList = new ArrayList<Map<String, Object>>();
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
searchHit.getSourceAsMap().put("id", searchHit.getId());
String[] highlightFields = highlightField.split(",");
for(String field : highlightFields){
StringBuffer stringBuffer = new StringBuffer();
try {
Text[] text = searchHit.getHighlightFields().get(field).getFragments();
if (text != null) {
for (Text str : text) {
stringBuffer.append(str.string());
}
//遍历 高亮结果集,覆盖 正常结果集
searchHit.getSourceAsMap().put(field, stringBuffer);
}
}catch (NullPointerException ignored){
}
}
sourceList.add(searchHit.getSourceAsMap());
}
return sourceList;
}