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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.client.solrj.request;
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;
29
import java.io.IOException;
30
import java.util.Collection;
31
import java.util.LinkedList;
32
import java.util.List;
35
* A request for the org.apache.solr.handler.DocumentAnalysisRequestHandler.
37
* @version $Id: FieldAnalysisRequest.java 1053583 2010-12-29 09:38:12Z ryan $
40
public class FieldAnalysisRequest extends SolrRequest {
42
private String fieldValue;
44
private boolean showMatch;
45
private List<String> fieldNames;
46
private List<String> fieldTypes;
49
* Constructs a new FieldAnalysisRequest with a default uri of "/fieldanalysis".
51
public FieldAnalysisRequest() {
52
super(METHOD.GET, "/analysis/field");
56
* Constructs a new FieldAnalysisRequest with a given uri.
58
* @param uri the uri of the request handler.
60
public FieldAnalysisRequest(String uri) {
61
super(METHOD.GET, uri);
68
public Collection<ContentStream> getContentStreams() throws IOException {
76
public SolrParams getParams() {
77
ModifiableSolrParams params = new ModifiableSolrParams();
78
params.set(AnalysisParams.FIELD_VALUE, fieldValue);
80
params.add(AnalysisParams.QUERY, query);
81
params.add(AnalysisParams.SHOW_MATCH, String.valueOf(showMatch));
83
if (fieldNames != null) {
84
String fieldNameValue = listToCommaDelimitedString(fieldNames);
85
params.add(AnalysisParams.FIELD_NAME, fieldNameValue);
87
if (fieldTypes != null) {
88
String fieldTypeValue = listToCommaDelimitedString(fieldTypes);
89
params.add(AnalysisParams.FIELD_TYPE, fieldTypeValue);
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");
102
if (fieldValue == null) {
103
throw new IllegalStateException("The field value must be set");
105
long startTime = System.currentTimeMillis();
106
FieldAnalysisResponse res = new FieldAnalysisResponse();
107
res.setResponse(server.request(this));
108
res.setElapsedTime(System.currentTimeMillis() - startTime);
113
//================================================ Helper Methods ==================================================
116
* Convers the given list of string to a comma-separated string.
118
* @param list The list of string.
120
* @return The comma-separated string.
122
static String listToCommaDelimitedString(List<String> list) {
123
StringBuilder result = new StringBuilder();
124
for (String str : list) {
125
if (result.length() > 0) {
130
return result.toString();
134
//============================================ Setter/Getter Methods ===============================================
137
* Sets the field value to be analyzed.
139
* @param fieldValue The field value to be analyzed.
141
* @return This FieldAnalysisRequest (fluent interface support).
143
public FieldAnalysisRequest setFieldValue(String fieldValue) {
144
this.fieldValue = fieldValue;
149
* Returns the field value that will be analyzed when this request is processed.
151
* @return The field value that will be analyzed when this request is processed.
153
public String getFieldValue() {
158
* Sets the query to be analyzed. May be {@code null} indicated that no query analysis should take place.
160
* @param query The query to be analyzed.
162
* @return This FieldAnalysisRequest (fluent interface support).
164
public FieldAnalysisRequest setQuery(String query) {
170
* Returns the query that will be analyzed. May return {@code null} indicating that no query analysis will be
173
* @return The query that will be analyzed. May return {@code null} indicating that no query analysis will be
176
public String getQuery() {
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}.
184
* @param showMatch Sets whether index time tokens that match query time tokens should be marked as a "match".
186
* @return This FieldAnalysisRequest (fluent interface support).
188
public FieldAnalysisRequest setShowMatch(boolean showMatch) {
189
this.showMatch = showMatch;
194
* Returns whether index time tokens that match query time tokens should be marked as a "match".
196
* @return Whether index time tokens that match query time tokens should be marked as a "match".
198
* @see #setShowMatch(boolean)
200
public boolean isShowMatch() {
205
* Adds the given field name for analysis.
207
* @param fieldName A field name on which the analysis should be performed.
209
* @return this FieldAnalysisRequest (fluent interface support).
211
public FieldAnalysisRequest addFieldName(String fieldName) {
212
if (fieldNames == null) {
213
fieldNames = new LinkedList<String>();
215
fieldNames.add(fieldName);
220
* Sets the field names on which the analysis should be performed.
222
* @param fieldNames The field names on which the analysis should be performed.
224
* @return this FieldAnalysisRequest (fluent interface support).
226
public FieldAnalysisRequest setFieldNames(List<String> fieldNames) {
227
this.fieldNames = fieldNames;
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.
235
* @return The field names the analysis should be performed on.
237
public List<String> getFieldNames() {
242
* Adds the given field type for analysis.
244
* @param fieldTypeName A field type name on which analysis should be performed.
246
* @return This FieldAnalysisRequest (fluent interface support).
248
public FieldAnalysisRequest addFieldType(String fieldTypeName) {
249
if (fieldTypes == null) {
250
fieldTypes = new LinkedList<String>();
252
fieldTypes.add(fieldTypeName);
257
* Sets the field types on which analysis should be performed.
259
* @param fieldTypes The field type names on which analysis should be performed.
261
* @return This FieldAnalysisRequest (fluent interface support).
263
public FieldAnalysisRequest setFieldTypes(List<String> fieldTypes) {
264
this.fieldTypes = fieldTypes;
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.
273
* @return The field types the analysis should be performed on.
275
public List<String> getFieldTypes() {