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

« back to all changes in this revision

Viewing changes to solr/solrj/src/java/org/apache/solr/client/solrj/request/FieldAnalysisRequest.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
 
/**
2
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 
 * contributor license agreements.  See the NOTICE file distributed with
4
 
 * this work for additional information regarding copyright ownership.
5
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 
 * (the "License"); you may not use this file except in compliance with
7
 
 * the License.  You may obtain a copy of the License at
8
 
 *
9
 
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
11
 
 * Unless required by applicable law or agreed to in writing, software
12
 
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 
 * See the License for the specific language governing permissions and
15
 
 * limitations under the License.
16
 
 */
17
 
 
18
 
package org.apache.solr.client.solrj.request;
19
 
 
20
 
import org.apache.solr.client.solrj.SolrRequest;
21
 
import org.apache.solr.client.solrj.SolrServer;
22
 
import org.apache.solr.client.solrj.SolrServerException;
23
 
import org.apache.solr.client.solrj.response.FieldAnalysisResponse;
24
 
import org.apache.solr.common.params.AnalysisParams;
25
 
import org.apache.solr.common.params.ModifiableSolrParams;
26
 
import org.apache.solr.common.params.SolrParams;
27
 
import org.apache.solr.common.util.ContentStream;
28
 
 
29
 
import java.io.IOException;
30
 
import java.util.Collection;
31
 
import java.util.LinkedList;
32
 
import java.util.List;
33
 
 
34
 
/**
35
 
 * A request for the org.apache.solr.handler.DocumentAnalysisRequestHandler.
36
 
 *
37
 
 * @version $Id: FieldAnalysisRequest.java 1053583 2010-12-29 09:38:12Z ryan $
38
 
 * @since solr.14
39
 
 */
40
 
public class FieldAnalysisRequest extends SolrRequest {
41
 
 
42
 
  private String fieldValue;
43
 
  private String query;
44
 
  private boolean showMatch;
45
 
  private List<String> fieldNames;
46
 
  private List<String> fieldTypes;
47
 
 
48
 
  /**
49
 
   * Constructs a new FieldAnalysisRequest with a default uri of "/fieldanalysis".
50
 
   */
51
 
  public FieldAnalysisRequest() {
52
 
    super(METHOD.GET, "/analysis/field");
53
 
  }
54
 
 
55
 
  /**
56
 
   * Constructs a new FieldAnalysisRequest with a given uri.
57
 
   *
58
 
   * @param uri the uri of the request handler.
59
 
   */
60
 
  public FieldAnalysisRequest(String uri) {
61
 
    super(METHOD.GET, uri);
62
 
  }
63
 
 
64
 
  /**
65
 
   * {@inheritDoc}
66
 
   */
67
 
  @Override
68
 
  public Collection<ContentStream> getContentStreams() throws IOException {
69
 
    return null;
70
 
  }
71
 
 
72
 
  /**
73
 
   * {@inheritDoc}
74
 
   */
75
 
  @Override
76
 
  public SolrParams getParams() {
77
 
    ModifiableSolrParams params = new ModifiableSolrParams();
78
 
    params.set(AnalysisParams.FIELD_VALUE, fieldValue);
79
 
    if (query != null) {
80
 
      params.add(AnalysisParams.QUERY, query);
81
 
      params.add(AnalysisParams.SHOW_MATCH, String.valueOf(showMatch));
82
 
    }
83
 
    if (fieldNames != null) {
84
 
      String fieldNameValue = listToCommaDelimitedString(fieldNames);
85
 
      params.add(AnalysisParams.FIELD_NAME, fieldNameValue);
86
 
    }
87
 
    if (fieldTypes != null) {
88
 
      String fieldTypeValue = listToCommaDelimitedString(fieldTypes);
89
 
      params.add(AnalysisParams.FIELD_TYPE, fieldTypeValue);
90
 
    }
91
 
    return params;
92
 
  }
93
 
 
94
 
  /**
95
 
   * {@inheritDoc}
96
 
   */
97
 
  @Override
98
 
  public FieldAnalysisResponse process(SolrServer server) throws SolrServerException, IOException {
99
 
    if (fieldTypes == null && fieldNames == null) {
100
 
      throw new IllegalStateException("At least one field type or field name need to be specified");
101
 
    }
102
 
    if (fieldValue == null) {
103
 
      throw new IllegalStateException("The field value must be set");
104
 
    }
105
 
    long startTime = System.currentTimeMillis();
106
 
    FieldAnalysisResponse res = new FieldAnalysisResponse();
107
 
    res.setResponse(server.request(this));
108
 
    res.setElapsedTime(System.currentTimeMillis() - startTime);
109
 
    return res;
110
 
  }
111
 
 
112
 
 
113
 
  //================================================ Helper Methods ==================================================
114
 
 
115
 
  /**
116
 
   * Convers the given list of string to a comma-separated string.
117
 
   *
118
 
   * @param list The list of string.
119
 
   *
120
 
   * @return The comma-separated string.
121
 
   */
122
 
  static String listToCommaDelimitedString(List<String> list) {
123
 
    StringBuilder result = new StringBuilder();
124
 
    for (String str : list) {
125
 
      if (result.length() > 0) {
126
 
        result.append(",");
127
 
      }
128
 
      result.append(str);
129
 
    }
130
 
    return result.toString();
131
 
  }
132
 
 
133
 
 
134
 
  //============================================ Setter/Getter Methods ===============================================
135
 
 
136
 
  /**
137
 
   * Sets the field value to be analyzed.
138
 
   *
139
 
   * @param fieldValue The field value to be analyzed.
140
 
   *
141
 
   * @return This FieldAnalysisRequest (fluent interface support).
142
 
   */
143
 
  public FieldAnalysisRequest setFieldValue(String fieldValue) {
144
 
    this.fieldValue = fieldValue;
145
 
    return this;
146
 
  }
147
 
 
148
 
  /**
149
 
   * Returns the field value that will be analyzed when this request is processed.
150
 
   *
151
 
   * @return The field value that will be analyzed when this request is processed.
152
 
   */
153
 
  public String getFieldValue() {
154
 
    return fieldValue;
155
 
  }
156
 
 
157
 
  /**
158
 
   * Sets the query to be analyzed. May be {@code null} indicated that no query analysis should take place.
159
 
   *
160
 
   * @param query The query to be analyzed.
161
 
   *
162
 
   * @return This FieldAnalysisRequest (fluent interface support).
163
 
   */
164
 
  public FieldAnalysisRequest setQuery(String query) {
165
 
    this.query = query;
166
 
    return this;
167
 
  }
168
 
 
169
 
  /**
170
 
   * Returns the query that will be analyzed. May return {@code null} indicating that no query analysis will be
171
 
   * performed.
172
 
   *
173
 
   * @return The query that will be analyzed. May return {@code null} indicating that no query analysis will be
174
 
   *         performed.
175
 
   */
176
 
  public String getQuery() {
177
 
    return query;
178
 
  }
179
 
 
180
 
  /**
181
 
   * Sets whether index time tokens that match query time tokens should be marked as a "match". By default this is set
182
 
   * to {@code false}. Obviously, this flag is ignored if when the query is set to {@code null}.
183
 
   *
184
 
   * @param showMatch Sets whether index time tokens that match query time tokens should be marked as a "match".
185
 
   *
186
 
   * @return This FieldAnalysisRequest (fluent interface support).
187
 
   */
188
 
  public FieldAnalysisRequest setShowMatch(boolean showMatch) {
189
 
    this.showMatch = showMatch;
190
 
    return this;
191
 
  }
192
 
 
193
 
  /**
194
 
   * Returns whether index time tokens that match query time tokens should be marked as a "match".
195
 
   *
196
 
   * @return Whether index time tokens that match query time tokens should be marked as a "match".
197
 
   *
198
 
   * @see #setShowMatch(boolean)
199
 
   */
200
 
  public boolean isShowMatch() {
201
 
    return showMatch;
202
 
  }
203
 
 
204
 
  /**
205
 
   * Adds the given field name for analysis.
206
 
   *
207
 
   * @param fieldName A field name on which the analysis should be performed.
208
 
   *
209
 
   * @return this FieldAnalysisRequest (fluent interface support).
210
 
   */
211
 
  public FieldAnalysisRequest addFieldName(String fieldName) {
212
 
    if (fieldNames == null) {
213
 
      fieldNames = new LinkedList<String>();
214
 
    }
215
 
    fieldNames.add(fieldName);
216
 
    return this;
217
 
  }
218
 
 
219
 
  /**
220
 
     * Sets the field names on which the analysis should be performed.
221
 
     *
222
 
     * @param fieldNames The field names on which the analysis should be performed.
223
 
     *
224
 
     * @return this FieldAnalysisRequest (fluent interface support).
225
 
     */
226
 
  public FieldAnalysisRequest setFieldNames(List<String> fieldNames) {
227
 
    this.fieldNames = fieldNames;
228
 
    return this;
229
 
  }
230
 
 
231
 
  /**
232
 
   * Returns a list of field names the analysis should be performed on. May return {@code null} indicating that no
233
 
   * analysis will be performed on field names.
234
 
   *
235
 
   * @return The field names the analysis should be performed on.
236
 
   */
237
 
  public List<String> getFieldNames() {
238
 
    return fieldNames;
239
 
  }
240
 
 
241
 
  /**
242
 
   * Adds the given field type for analysis.
243
 
   *
244
 
   * @param fieldTypeName A field type name on which analysis should be performed.
245
 
   *
246
 
   * @return This FieldAnalysisRequest (fluent interface support).
247
 
   */
248
 
  public FieldAnalysisRequest addFieldType(String fieldTypeName) {
249
 
    if (fieldTypes == null) {
250
 
      fieldTypes = new LinkedList<String>();
251
 
    }
252
 
    fieldTypes.add(fieldTypeName);
253
 
    return this;
254
 
  }
255
 
 
256
 
/**
257
 
   * Sets the field types on which analysis should be performed.
258
 
   *
259
 
   * @param fieldTypes The field type names on which analysis should be performed.
260
 
   *
261
 
   * @return This FieldAnalysisRequest (fluent interface support).
262
 
   */
263
 
  public FieldAnalysisRequest setFieldTypes(List<String> fieldTypes) {
264
 
    this.fieldTypes = fieldTypes;
265
 
    return this;
266
 
  }
267
 
 
268
 
 
269
 
  /**
270
 
   * Returns a list of field types the analysis should be performed on. May return {@code null} indicating that no
271
 
   * analysis will be peformed on field types.
272
 
   *
273
 
   * @return The field types the analysis should be performed on.
274
 
   */
275
 
  public List<String> getFieldTypes() {
276
 
    return fieldTypes;
277
 
  }
278
 
 
279
 
}