~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/grouping/distributed/command/QueryCommand.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.search.grouping.distributed.command;
2
 
 
3
 
/*
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
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;
28
 
 
29
 
import java.io.IOException;
30
 
import java.util.Arrays;
31
 
import java.util.List;
32
 
 
33
 
/**
34
 
 *
35
 
 */
36
 
public class QueryCommand implements Command<QueryCommandResult> {
37
 
 
38
 
  public static class Builder {
39
 
 
40
 
    private Sort sort;
41
 
    private String queryString;
42
 
    private Query query;
43
 
    private DocSet docSet;
44
 
    private Integer docsToCollect;
45
 
    private boolean needScores;
46
 
 
47
 
    public Builder setSort(Sort sort) {
48
 
      this.sort = sort;
49
 
      return this;
50
 
    }
51
 
 
52
 
    public Builder setQuery(Query query) {
53
 
      this.query = query;
54
 
      return this;
55
 
    }
56
 
 
57
 
    /**
58
 
     * Sets the group query from the specified groupQueryString.
59
 
     * The groupQueryString is parsed into a query.
60
 
     *
61
 
     * @param groupQueryString The group query string to parse
62
 
     * @param request The current request
63
 
     * @return this
64
 
     * @throws ParseException If parsing the groupQueryString failed
65
 
     */
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());
70
 
    }
71
 
 
72
 
    public Builder setDocSet(DocSet docSet) {
73
 
      this.docSet = docSet;
74
 
      return this;
75
 
    }
76
 
 
77
 
    /**
78
 
     * Sets the docSet based on the created {@link DocSet}
79
 
     *
80
 
     * @param searcher The searcher executing the
81
 
     * @return this
82
 
     * @throws IOException If I/O related errors occur.
83
 
     */
84
 
    public Builder setDocSet(SolrIndexSearcher searcher) throws IOException {
85
 
      return setDocSet(searcher.getDocSet(query));
86
 
    }
87
 
 
88
 
    public Builder setDocsToCollect(int docsToCollect) {
89
 
      this.docsToCollect = docsToCollect;
90
 
      return this;
91
 
    }
92
 
 
93
 
    public Builder setNeedScores(boolean needScores) {
94
 
      this.needScores = needScores;
95
 
      return this;
96
 
    }
97
 
 
98
 
    public QueryCommand build() {
99
 
      if (sort == null || query == null || docSet == null || docsToCollect == null) {
100
 
        throw new IllegalStateException("All fields must be set");
101
 
      }
102
 
 
103
 
      return new QueryCommand(sort, query, docsToCollect, needScores, docSet, queryString);
104
 
    }
105
 
 
106
 
  }
107
 
 
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;
114
 
 
115
 
  private TopDocsCollector collector;
116
 
  private FilterCollector filterCollector;
117
 
 
118
 
  private QueryCommand(Sort sort, Query query, int docsToCollect, boolean needScores, DocSet docSet, String queryString) {
119
 
    this.sort = sort;
120
 
    this.query = query;
121
 
    this.docsToCollect = docsToCollect;
122
 
    this.needScores = needScores;
123
 
    this.docSet = docSet;
124
 
    this.queryString = queryString;
125
 
  }
126
 
 
127
 
  public List<Collector> create() throws IOException {
128
 
    if (sort == null || sort == Sort.RELEVANCE) {
129
 
      collector = TopScoreDocCollector.create(docsToCollect, true);
130
 
    } else {
131
 
      collector = TopFieldCollector.create(sort, docsToCollect, true, needScores, needScores, true);
132
 
    }
133
 
    filterCollector = new FilterCollector(docSet, collector);
134
 
    return Arrays.asList((Collector) filterCollector);
135
 
  }
136
 
 
137
 
  public QueryCommandResult result() {
138
 
    return new QueryCommandResult(collector.topDocs(), filterCollector.getMatches());
139
 
  }
140
 
 
141
 
  public String getKey() {
142
 
    return queryString != null ? queryString : query.toString();
143
 
  }
144
 
 
145
 
  public Sort getGroupSort() {
146
 
    return sort;
147
 
  }
148
 
 
149
 
  public Sort getSortWithinGroup() {
150
 
    return null;
151
 
  }
152
 
}