1
package org.apache.solr.search.grouping.distributed.command;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.queryParser.ParseException;
21
import org.apache.lucene.search.*;
22
import org.apache.solr.request.SolrQueryRequest;
23
import org.apache.solr.search.DocSet;
24
import org.apache.solr.search.QParser;
25
import org.apache.solr.search.SolrIndexSearcher;
26
import org.apache.solr.search.grouping.Command;
27
import org.apache.solr.search.grouping.collector.FilterCollector;
29
import java.io.IOException;
30
import java.util.Arrays;
31
import java.util.List;
36
public class QueryCommand implements Command<QueryCommandResult> {
38
public static class Builder {
41
private String queryString;
43
private DocSet docSet;
44
private Integer docsToCollect;
45
private boolean needScores;
47
public Builder setSort(Sort sort) {
52
public Builder setQuery(Query query) {
58
* Sets the group query from the specified groupQueryString.
59
* The groupQueryString is parsed into a query.
61
* @param groupQueryString The group query string to parse
62
* @param request The current request
64
* @throws ParseException If parsing the groupQueryString failed
66
public Builder setQuery(String groupQueryString, SolrQueryRequest request) throws ParseException {
67
QParser parser = QParser.getParser(groupQueryString, null, request);
68
this.queryString = groupQueryString;
69
return setQuery(parser.getQuery());
72
public Builder setDocSet(DocSet docSet) {
78
* Sets the docSet based on the created {@link DocSet}
80
* @param searcher The searcher executing the
82
* @throws IOException If I/O related errors occur.
84
public Builder setDocSet(SolrIndexSearcher searcher) throws IOException {
85
return setDocSet(searcher.getDocSet(query));
88
public Builder setDocsToCollect(int docsToCollect) {
89
this.docsToCollect = docsToCollect;
93
public Builder setNeedScores(boolean needScores) {
94
this.needScores = needScores;
98
public QueryCommand build() {
99
if (sort == null || query == null || docSet == null || docsToCollect == null) {
100
throw new IllegalStateException("All fields must be set");
103
return new QueryCommand(sort, query, docsToCollect, needScores, docSet, queryString);
108
private final Sort sort;
109
private final Query query;
110
private final DocSet docSet;
111
private final int docsToCollect;
112
private final boolean needScores;
113
private final String queryString;
115
private TopDocsCollector collector;
116
private FilterCollector filterCollector;
118
private QueryCommand(Sort sort, Query query, int docsToCollect, boolean needScores, DocSet docSet, String queryString) {
121
this.docsToCollect = docsToCollect;
122
this.needScores = needScores;
123
this.docSet = docSet;
124
this.queryString = queryString;
127
public List<Collector> create() throws IOException {
128
if (sort == null || sort == Sort.RELEVANCE) {
129
collector = TopScoreDocCollector.create(docsToCollect, true);
131
collector = TopFieldCollector.create(sort, docsToCollect, true, needScores, needScores, true);
133
filterCollector = new FilterCollector(docSet, collector);
134
return Arrays.asList((Collector) filterCollector);
137
public QueryCommandResult result() {
138
return new QueryCommandResult(collector.topDocs(), filterCollector.getMatches());
141
public String getKey() {
142
return queryString != null ? queryString : query.toString();
145
public Sort getGroupSort() {
149
public Sort getSortWithinGroup() {