SOLR-1099 follow up -- Fix NPE in FieldAnalysisRequestHandler

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@771099 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-05-03 18:47:50 +00:00
parent 99a48e5604
commit 46cee7fa27
2 changed files with 29 additions and 15 deletions

View File

@ -82,6 +82,8 @@ import java.util.Set;
* <td>No</td>
* </tr>
* </table>
* <p>Note that if neither analysis.fieldname and analysis.fieldtype is specified, then the default search field's
* analyzer is used.</p>
*
* @version $Id$
* @since solr 1.4
@ -92,7 +94,7 @@ public class FieldAnalysisRequestHandler extends AnalysisRequestHandlerBase {
* {@inheritDoc}
*/
protected NamedList doAnalysis(SolrQueryRequest req) throws Exception {
FieldAnalysisRequest analysisRequest = resolveAnalysisRequest(req.getParams());
FieldAnalysisRequest analysisRequest = resolveAnalysisRequest(req);
IndexSchema indexSchema = req.getCore().getSchema();
return handleAnalysisRequest(analysisRequest, indexSchema);
}
@ -122,19 +124,26 @@ public class FieldAnalysisRequestHandler extends AnalysisRequestHandlerBase {
/**
* Resolves the AnalysisRequest based on the parameters in the given SolrParams.
*
* @param solrParams SolrParams taken from request
* @param req the request
*
* @return AnalysisRequest containing all the information about what needs to be analyzed, and using what
* fields/types
*/
FieldAnalysisRequest resolveAnalysisRequest(SolrParams solrParams) {
FieldAnalysisRequest resolveAnalysisRequest(SolrQueryRequest req) {
SolrParams solrParams = req.getParams();
FieldAnalysisRequest analysisRequest = new FieldAnalysisRequest();
boolean useDefaultSearchField = true;
if (solrParams.get(AnalysisParams.FIELD_TYPE) != null) {
analysisRequest.setFieldTypes(Arrays.asList(solrParams.get(AnalysisParams.FIELD_TYPE).split(",")));
useDefaultSearchField = false;
}
if (solrParams.get(AnalysisParams.FIELD_NAME) != null) {
analysisRequest.setFieldNames(Arrays.asList(solrParams.get(AnalysisParams.FIELD_NAME).split(",")));
useDefaultSearchField = false;
}
if (useDefaultSearchField) {
analysisRequest.addFieldName(req.getSchema().getSolrQueryParser(null).getField());
}
analysisRequest.setQuery(solrParams.get(AnalysisParams.QUERY, solrParams.get(CommonParams.Q)));
analysisRequest.setFieldValue(solrParams.get(AnalysisParams.FIELD_VALUE));
@ -154,15 +163,19 @@ public class FieldAnalysisRequestHandler extends AnalysisRequestHandlerBase {
NamedList<NamedList> analysisResults = new SimpleOrderedMap<NamedList>();
NamedList<NamedList> fieldTypeAnalysisResults = new SimpleOrderedMap<NamedList>();
for (String fieldTypeName : request.getFieldTypes()) {
FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
fieldTypeAnalysisResults.add(fieldTypeName, analyzeValues(request, fieldType, null));
if (request.getFieldTypes() != null) {
for (String fieldTypeName : request.getFieldTypes()) {
FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
fieldTypeAnalysisResults.add(fieldTypeName, analyzeValues(request, fieldType, null));
}
}
NamedList<NamedList> fieldNameAnalysisResults = new SimpleOrderedMap<NamedList>();
for (String fieldName : request.getFieldNames()) {
FieldType fieldType = schema.getFieldType(fieldName);
fieldNameAnalysisResults.add(fieldName, analyzeValues(request, fieldType, fieldName));
if (request.getFieldNames() != null) {
for (String fieldName : request.getFieldNames()) {
FieldType fieldType = schema.getFieldType(fieldName);
fieldNameAnalysisResults.add(fieldName, analyzeValues(request, fieldType, fieldName));
}
}
analysisResults.add("field_types", fieldTypeAnalysisResults);

View File

@ -24,6 +24,7 @@ import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.client.solrj.request.FieldAnalysisRequest;
import org.apache.solr.request.LocalSolrQueryRequest;
import java.util.List;
@ -54,7 +55,7 @@ public class FieldAnalysisRequestHandlerTest extends AnalysisRequestHandlerTestB
}
/**
* Tests the {@link FieldAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.common.params.SolrParams)}
* Tests the {@link FieldAnalysisRequestHandler#resolveAnalysisRequest(org.apache.solr.request.SolrQueryRequest)}
*/
public void testResolveAnalysisRequest() throws Exception {
ModifiableSolrParams params = new ModifiableSolrParams();
@ -63,7 +64,7 @@ public class FieldAnalysisRequestHandlerTest extends AnalysisRequestHandlerTestB
params.add(AnalysisParams.FIELD_VALUE, "the quick red fox jumped over the lazy brown dogs");
params.add(CommonParams.Q, "fox brown");
FieldAnalysisRequest request = handler.resolveAnalysisRequest(params);
FieldAnalysisRequest request = handler.resolveAnalysisRequest(new LocalSolrQueryRequest(h.getCore(), params));
List<String> fieldNames = request.getFieldNames();
assertEquals("Expecting 2 field names", 2, fieldNames.size());
assertEquals("text", fieldNames.get(0));
@ -78,21 +79,21 @@ public class FieldAnalysisRequestHandlerTest extends AnalysisRequestHandlerTestB
// testing overide of query value using analysis.query param
params.add(AnalysisParams.QUERY, "quick lazy");
request = handler.resolveAnalysisRequest(params);
request = handler.resolveAnalysisRequest(new LocalSolrQueryRequest(h.getCore(), params));
assertEquals("quick lazy", request.getQuery());
// testing analysis.showmatch param
params.add(AnalysisParams.SHOW_MATCH, "false");
request = handler.resolveAnalysisRequest(params);
request = handler.resolveAnalysisRequest(new LocalSolrQueryRequest(h.getCore(), params));
assertFalse(request.isShowMatch());
params.set(AnalysisParams.SHOW_MATCH, "true");
request = handler.resolveAnalysisRequest(params);
request = handler.resolveAnalysisRequest(new LocalSolrQueryRequest(h.getCore(), params));
assertTrue(request.isShowMatch());
// testing absence of query value
params.remove(CommonParams.Q);
params.remove(AnalysisParams.QUERY);
request = handler.resolveAnalysisRequest(params);
request = handler.resolveAnalysisRequest(new LocalSolrQueryRequest(h.getCore(), params));
assertNull(request.getQuery());
}