(4) setSearchType(String searchType),与setSearchType(SearchType searchType)类似,区别在于其值为字符串型的SearchType,值可为dfs_query_then_fetch、dfsQueryThenFetch、dfs_query_and_fetch、dfsQueryAndFetch、query_then_fetch、queryThenFetch、query_and_fetch或queryAndFetch;
(5) setScroll(Scroll scroll)、setScroll(TimeValue keepAlive)和setScroll(String keepAlive),设置滚动,参数为Scroll时,直接用new Scroll(TimeValue)构造一个Scroll,为TimeValue或String时需要将TimeValue和String转化为Scroll;
(6) setTimeout(TimeValue timeout)和setTimeout(String timeout),设置搜索的超时时间;
(7) setQuery,设置查询使用的Query;
(8) setFilter,设置过滤器;
(9) setMinScore,设置Score的最小数量;
(10) setFrom,从哪一个Score开始查;
(11) setSize,需要查询出多少条结果;
检索出结果后,通过response.getHits()可以得到所有的SearchHit,得到Hit后,便可迭代Hit取到对应的Document,转化成为需要的实体。
2.搜索高亮显示
SearchRequestBuilder中的addHighlightedField()方法可以定制在哪个域值的检索结果的关键字上增加高亮
public void search() throws IOException {
// 自定义集群结点名称
String clusterName = "elasticsearch_pudongping";
// 获取客户端
Client client = ESClient.initClient(clusterName);
// 创建查询索引,参数productindex表示要查询的索引库为productindex
SearchRequestBuilder searchRequestBuilder = client
.prepareSearch("productindex");
// 设置查询索引类型,setTypes("productType1", "productType2","productType3");
// 用来设定在多个类型中搜索
searchRequestBuilder.setTypes("productIndex");
// 设置查询类型 1.SearchType.DFS_QUERY_THEN_FETCH = 精确查询 2.SearchType.SCAN = 扫描查询,无序
searchRequestBuilder.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
// 设置查询关键词
searchRequestBuilder
.setQuery(QueryBuilders.fieldQuery("title", "Acer"));
// 查询过滤器过滤价格在4000-5000内 这里范围为[4000,5000]区间闭包含,搜索结果包含价格为4000和价格为5000的数据
searchRequestBuilder.setFilter(FilterBuilders.rangeFilter("price")
.from(4000).to(5000));
// 分页应用
searchRequestBuilder.setFrom(0).setSize(60);
// 设置是否按查询匹配度排序
searchRequestBuilder.setExplain(true);
//设置高亮显示
searchRequestBuilder.addHighlightedField("title");
searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red\">");
searchRequestBuilder.setHighlighterPostTags("</span>");
// 执行搜索,返回搜索响应信息
SearchResponse response = searchRequestBuilder.execute().actionGet();
//获取搜索的文档结果
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
ObjectMapper mapper = new ObjectMapper();
for (int i = 0; i < hits.length; i++) {
SearchHit hit = hits[i];
//将文档中的每一个对象转换json串值
String json = hit.getSourceAsString();
//将json串值转换成对应的实体对象
Product product = mapper.readValue(json, Product.class);
//获取对应的高亮域
Map<String, HighlightField> result = hit.highlightFields();
//从设定的高亮域中取得指定域
HighlightField titleField = result.get("title");
//取得定义的高亮标签
Text[] titleTexts = titleField.fragments();
//为title串值增加自定义的高亮标签
String title = "";
for(Text text : titleTexts){
title += text;
}
//将追加了高亮标签的串值重新填充到对应的对象
product.setTitle(title);
//打印高亮标签追加完成后的实体对象
System.out.println(product);
}
System.out.println("search success ..");
}
程序运行结果: