赞
踩
目录
4.3 将QueryBuilder对象都必须添加到SearchSourceBuilder
1请求本身的信息,如 HTTP 状态码,执行时间,或者请求是否超时
3.2 SearchHits 中包含了所有命中的全局信息,如查询命中的数量或者最大分值
3.3查询每一条命中数据的信息如index、type、id、score
用于与搜索文档、聚合、建议相关的任何操作,还提供了请求突出显示结果文档的方法
- SearchRequest searchRequest = new SearchRequest(); //1
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); //2
- searchSourceBuilder.query(QueryBuilders.matchAllQuery()); //3
- searchRequest.source(searchSourceBuilder); //4
1创建搜索请求。如果没有参数,这将对所有索引运行。
2大多数搜索参数都添加到SearchSourceBuilder中。它为进入搜索请求主体的所有内容提供了setter。
3向SearchSourceBuilder添加一个match_all查询。
4将SearchSourceBuilder添加到SearchRequest。
SearchRequest searchRequest = new SearchRequest("user");
searchRequest.indices("banl","hh");
searchRequest.routing("routing");
searchRequest.preference("_local");
大多数控制搜索行为的选项都可以在SearchSourceBuilder上设置,它或多或少包含与RESTAPI的搜索请求正文中的选项等效的选项。
- SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //1
- sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy")); //2
- sourceBuilder.from(0); //3
- sourceBuilder.size(5); //4
- sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //5
1使用默认选项创建SearchSourceBuilder。
2设置查询。可以是任何类型的QueryBuilder
3设置“从”选项,该选项确定要从中开始搜索的结果索引。默认值为0。
4设置大小选项,该选项确定要返回的搜索命中数。默认值为10。
5设置一个可选超时,用于控制允许搜索的时间。
在此之后,只需将SearchSourceBuilder添加到SearchRequest
搜索查询是使用QueryBuilder对象创建的。Elasticsearch的查询DSL支持的每种搜索查询类型都有一个QueryBuilder。
MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "kimchy"); //1
1创建一个全文匹配查询,在字段“user”上匹配文本“kimchy”。
创建后,QueryBuilder对象提供了配置其创建的搜索查询选项的方法:
- matchQueryBuilder.fuzziness(Fuzziness.AUTO); //1
- matchQueryBuilder.prefixLength(3); //2
- matchQueryBuilder.maxExpansions(10); //3
1在匹配查询上启用模糊匹配 例如,搜索 quick brown fox
时会匹配一个包含 fast brown foxes
的文档
2在匹配查询上设置前缀长度选项
3设置最大扩展选项以控制查询的模糊过程
还可以使用QueryBuilders实用程序类创建QueryBuilder对象。此类提供了可用于使用流畅编程风格创建QueryBuilder对象的帮助器方法:
- ueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
- .fuzziness(Fuzziness.AUTO)
- .prefixLength(3)
- .maxExpansions(10);
无论使用何种方法创建它,QueryBuilder对象都必须添加到SearchSourceBuilder,如下所示:
searchSourceBuilder.query(matchQueryBuilder);
SearchSourceBuilder允许添加一个或多个SortBuilder实例。有四种特殊实现(Field、Score、GeoDistance和ScriptSortBuilder)
- sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); //1
- sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC)); //2
1按分数递减排序(默认值)
2按_id字段升序排序
添加多个则都起作用 上面代码则是按照分数排序也按照id排序
默认情况下,查询请求会返回文档的内容 _source
,当然我们也可以配置它。例如,禁止对 _source
的获取
sourceBuilder.fetchSource(false);
也可以使用通配符模式以更细的粒度包含或排除特定的字段:
- String[] includeFields = new String[] {"title", "user", "innerObject.*"};
- String[] excludeFields = new String[] {"_type"};
- sourceBuilder.fetchSource(includeFields, excludeFields);
可以通过在 SearchSourceBuilder
上设置 HighlightBuilder
完成对结果的高亮,而且可以配置不同的字段具有不同的高亮行为
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- HighlightBuilder highlightBuilder = new HighlightBuilder();
- HighlightBuilder.Field highlightTitle =
- new HighlightBuilder.Field("title"); // title 字段高亮
- highlightTitle.highlighterType("unified"); // 配置高亮类型
- highlightBuilder.field(highlightTitle); // 添加到 builder
- HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
- highlightBuilder.field(highlightUser);
- searchSourceBuilder.highlighter(highlightBuilder);
要实现聚合请求分两步
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
- .field("company.keyword");
- aggregation.subAggregation(AggregationBuilders.avg("average_age")
- .field("age"));
- searchSourceBuilder.aggregation(aggregation);
SuggestionBuilder
实现类是由 SuggestBuilders
工厂类来创建的
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- SuggestionBuilder termSuggestionBuilder =
- SuggestBuilders.termSuggestion("user").text("kmichy");
- SuggestBuilder suggestBuilder = new SuggestBuilder();
- suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
- searchSourceBuilder.suggest(suggestBuilder);
查询执行完成后,会返回 SearchResponse
对象,并在对象中包含查询执行的细节和符合条件的文档集合
- RestStatus status = searchResponse.status(); // HTTP 状态码
- TimeValue took = searchResponse.getTook(); // 查询占用的时间
- Boolean terminatedEarly = searchResponse.isTerminatedEarly(); // 是否由于 SearchSourceBuilder 中设置 terminateAfter 而过早终止
- boolean timedOut = searchResponse.isTimedOut(); // 是否超时
- int totalShards = searchResponse.getTotalShards();
- int successfulShards = searchResponse.getSuccessfulShards();
- int failedShards = searchResponse.getFailedShards();
- for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
- // failures should be handled here
- }
SearchHits hits = searchResponse.getHits();
3.2 SearchHits
中包含了所有命中的全局信息,如查询命中的数量或者最大分值- long totalHits = hits.getTotalHits();
- float maxScore = hits.getMaxScore();
- SearchHit[] searchHits = hits.getHits();
- for (SearchHit hit : searchHits) {
- String index = hit.getIndex();
- String type = hit.getType();
- String id = hit.getId();
- float score = hit.getScore();
- }
- String sourceAsString = hit.getSourceAsString();
- Map<String, Object> sourceAsMap = hit.getSourceAsMap();
- String documentTitle = (String) sourceAsMap.get("title");
- List<Object> users = (List<Object>) sourceAsMap.get("user");
- Map<String, Object> innerObject =
- (Map<String, Object>) sourceAsMap.get("innerObject");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。