SOLR-4537: Clean up schema information REST API

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1454141 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Steven Rowe 2013-03-07 22:41:18 +00:00
parent 62dcc4274a
commit 14c9f28f57
7 changed files with 117 additions and 61 deletions

View File

@ -38,6 +38,35 @@ TBD...
Detailed Change List
----------------------
================== 4.3.0 ==================
Versions of Major Components
---------------------
Apache Tika 1.3
Carrot2 3.6.2
Velocity 1.7 and Velocity Tools 2.0
Apache UIMA 2.3.1
Apache ZooKeeper 3.4.5
Upgrading from Solr 4.2.0
----------------------
(No upgrade instructions yet)
Detailed Change List
----------------------
New Features
----------------------
Bug Fixes
----------------------
Other Changes
----------------------
* SOLR-4537: Clean up schema information REST API. (Steve Rowe)
================== 4.2.0 ==================
Versions of Major Components

View File

@ -18,14 +18,10 @@ package org.apache.solr.rest;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.schema.CopyField;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.restlet.resource.ResourceException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.LinkedHashSet;
/**
@ -38,10 +34,10 @@ abstract class BaseFieldResource extends BaseSchemaResource {
private static final String SHOW_DEFAULTS = "showDefaults";
private LinkedHashMap<String,SimpleOrderedMap<Object>> requestedFields;
private LinkedHashSet<String> requestedFields;
private boolean showDefaults;
protected LinkedHashMap<String,SimpleOrderedMap<Object>> getRequestedFields() {
protected LinkedHashSet<String> getRequestedFields() {
return requestedFields;
}
@ -70,10 +66,10 @@ abstract class BaseFieldResource extends BaseSchemaResource {
if (null != flParam) {
String[] fields = flParam.trim().split("[,\\s]+");
if (fields.length > 0) {
requestedFields = new LinkedHashMap<String,SimpleOrderedMap<Object>>();
requestedFields = new LinkedHashSet<String>();
for (String field : fields) {
if ( ! field.isEmpty()) {
requestedFields.put(field, null);
if ( ! field.trim().isEmpty()) {
requestedFields.add(field.trim());
}
}
}
@ -93,9 +89,9 @@ abstract class BaseFieldResource extends BaseSchemaResource {
SimpleOrderedMap<Object> properties = field.getNamedPropertyValues(showDefaults);
if ( ! getSchema().getFields().containsKey(field.getName())) {
String dynamicBase = getSchema().getDynamicPattern(field.getName());
// Add dynamicBase property if it's different from the field name.
if ( ! field.getName().equals(dynamicBase)) {
// Don't add dynamicBase property if it's identical to the field name.
properties.add(DYNAMIC_BASE, getSchema().getDynamicPattern(field.getName()));
properties.add(DYNAMIC_BASE, dynamicBase);
}
}
if (field == getSchema().getUniqueKeyField()) {

View File

@ -21,6 +21,7 @@ import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
@ -28,6 +29,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -55,32 +57,29 @@ public class DynamicFieldCollectionResource extends BaseFieldResource implements
public Representation get() {
try {
SchemaField[] dynamicFields = getSchema().getDynamicFieldPrototypes();
List<SimpleOrderedMap<Object>> props = new ArrayList<SimpleOrderedMap<Object>>(dynamicFields.length);
if (null != getRequestedFields()) {
List<SimpleOrderedMap<Object>> props = new ArrayList<SimpleOrderedMap<Object>>();
if (null == getRequestedFields()) {
for (IndexSchema.DynamicField dynamicField : getSchema().getDynamicFields()) {
if ( ! dynamicField.getRegex().startsWith(INTERNAL_POLY_FIELD_PREFIX)) { // omit internal polyfields
props.add(getFieldProperties(dynamicField.getPrototype()));
}
}
} else {
if (0 == getRequestedFields().size()) {
String message = "Empty " + CommonParams.FL + " parameter value";
throw new SolrException(ErrorCode.BAD_REQUEST, message);
}
for (SchemaField prototype : dynamicFields) {
if (getRequestedFields().containsKey(prototype.getName())) {
getRequestedFields().put(prototype.getName(), getFieldProperties(prototype));
}
Map<String,SchemaField> dynamicFieldsByName = new HashMap<String,SchemaField>();
for (IndexSchema.DynamicField dynamicField : getSchema().getDynamicFields()) {
dynamicFieldsByName.put(dynamicField.getRegex(), dynamicField.getPrototype());
}
// Use the same order as the fl parameter
for (Map.Entry<String,SimpleOrderedMap<Object>> requestedField : getRequestedFields().entrySet()) {
SimpleOrderedMap<Object> fieldProperties = requestedField.getValue();
// Should there be some form of error condition
// if one or more of the requested fields were not found?
if (null != fieldProperties) {
props.add(fieldProperties);
}
}
} else {
for (SchemaField prototype : dynamicFields) {
// omit internal polyfields
if ( ! prototype.getName().startsWith(INTERNAL_POLY_FIELD_PREFIX)) {
props.add(getFieldProperties(prototype));
for (String dynamicFieldName : getRequestedFields()) {
final SchemaField dynamicSchemaField = dynamicFieldsByName.get(dynamicFieldName);
if (null == dynamicSchemaField) {
log.info("Requested dynamic field '" + dynamicFieldName + "' not found.");
} else {
props.add(getFieldProperties(dynamicSchemaField));
}
}
}

View File

@ -21,29 +21,37 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.schema.SchemaField;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* This class responds to requests at /solr/(corename)/schema/fields
* <p/>
* One query parameters are supported:
* Two query parameters are supported:
* <ul>
* <li>
* "fl": a comma- and/or space-separated list of fields to send properties
* for in the response, rather than the default: all of them.
* </li>
* <li>
* "includeDynamic": if the "fl" parameter is specified, matching dynamic
* fields are included in the response and identified with the "dynamicBase"
* property. If the "fl" parameter is not specified, the "includeDynamic"
* query parameter is ignored.
* </li>
* </ul>
*/
public class FieldCollectionResource extends BaseFieldResource implements GETable {
private static final Logger log = LoggerFactory.getLogger(FieldCollectionResource.class);
private boolean includeDynamic;
public FieldCollectionResource() {
super();
@ -52,40 +60,39 @@ public class FieldCollectionResource extends BaseFieldResource implements GETabl
@Override
public void doInit() throws ResourceException {
super.doInit();
includeDynamic = getSolrRequest().getParams().getBool(INCLUDE_DYNAMIC_PARAM, false);
}
@Override
public Representation get() {
try {
// Get all explicitly defined fields from the schema
Set<String> fieldNames = new HashSet<String>(getSchema().getFields().keySet());
final List<SimpleOrderedMap<Object>> fieldCollectionProperties = new ArrayList<SimpleOrderedMap<Object>>(fieldNames.size());
final List<SimpleOrderedMap<Object>> props = new ArrayList<SimpleOrderedMap<Object>>();
if (null == getRequestedFields()) {
SortedSet<String> fieldNames = new TreeSet<String>(getSchema().getFields().keySet());
for (String fieldName : fieldNames) {
fieldCollectionProperties.add(getFieldProperties(getSchema().getFieldOrNull(fieldName)));
props.add(getFieldProperties(getSchema().getFields().get(fieldName)));
}
} else {
if (0 == getRequestedFields().size()) {
String message = "Empty " + CommonParams.FL + " parameter value";
throw new SolrException(ErrorCode.BAD_REQUEST, message);
}
for (String field : fieldNames) {
if (getRequestedFields().containsKey(field)) {
getRequestedFields().put(field, getFieldProperties(getSchema().getFieldOrNull(field)));
}
}
// Use the same order as the fl parameter
for (SimpleOrderedMap<Object> fieldProperties : getRequestedFields().values()) {
// Should there be some form of error condition
// if one or more of the requested fields were not found?
if (null != fieldProperties) {
fieldCollectionProperties.add(fieldProperties);
for (String fieldName : getRequestedFields()) {
final SchemaField field;
if (includeDynamic) {
field = getSchema().getFieldOrNull(fieldName);
} else {
field = getSchema().getFields().get(fieldName);
}
if (null == field) {
log.info("Requested field '" + fieldName + "' not found.");
} else {
props.add(getFieldProperties(field));
}
}
}
getSolrResponse().add(SchemaRestApi.FIELDS, fieldCollectionProperties);
getSolrResponse().add(SchemaRestApi.FIELDS, props);
} catch (Exception e) {
getSolrResponse().setException(e);
}

View File

@ -68,11 +68,10 @@ public class FieldResource extends BaseFieldResource implements GETable {
final String message = "Field name is missing";
throw new SolrException(ErrorCode.BAD_REQUEST, message);
} else {
SchemaField field;
final SchemaField field;
if (includeDynamic) {
field = getSchema().getFieldOrNull(fieldName);
} else {
// Don't look for matches among dynamic fields
field = getSchema().getFields().get(fieldName);
}
if (null == field) {

View File

@ -841,7 +841,8 @@ public final class IndexSchema {
public final static class DynamicField extends DynamicReplacement {
final SchemaField prototype;
private final SchemaField prototype;
public SchemaField getPrototype() { return prototype; }
DynamicField(SchemaField prototype) {
super(prototype.name);

View File

@ -24,9 +24,9 @@ public class TestFieldCollectionResource extends SchemaRestletTestBase {
@Test
public void testGetAllFields() throws Exception {
assertQ("/schema/fields?indent=on&wt=xml",
"(/response/arr[@name='fields']/lst/str[@name='name'])[1] = 'custstopfilt'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[2] = 'lowerfilt'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[3] = 'test_basictv'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[1] = 'HTMLstandardtok'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[2] = 'HTMLwhitetok'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[3] = '_version_'",
"count(//copySources/str) = count(//copyDests/str)");
}
@ -37,6 +37,31 @@ public class TestFieldCollectionResource extends SchemaRestletTestBase {
"(/response/arr[@name='fields']/lst/str[@name='name'])[1] = 'id'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[2] = '_version_'");
}
@Test
public void testGetThreeFieldsDontIncludeDynamic() throws IOException {
//
assertQ("/schema/fields?indent=on&wt=xml&fl=id,_version_,price_i",
"count(/response/arr[@name='fields']/lst/str[@name='name']) = 2",
"(/response/arr[@name='fields']/lst/str[@name='name'])[1] = 'id'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[2] = '_version_'");
}
@Test
public void testGetThreeFieldsIncludeDynamic() throws IOException {
assertQ("/schema/fields?indent=on&wt=xml&fl=id,_version_,price_i&includeDynamic=on",
"count(/response/arr[@name='fields']/lst/str[@name='name']) = 3",
"(/response/arr[@name='fields']/lst/str[@name='name'])[1] = 'id'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[2] = '_version_'",
"(/response/arr[@name='fields']/lst/str[@name='name'])[3] = 'price_i'",
"/response/arr[@name='fields']/lst[ str[@name='name']='price_i' "
+" and str[@name='dynamicBase']='*_i']");
}
@Test
public void testNotFoundFields() throws IOException {
@ -48,9 +73,9 @@ public class TestFieldCollectionResource extends SchemaRestletTestBase {
@Test
public void testJsonGetAllFields() throws Exception {
assertJQ("/schema/fields?indent=on",
"/fields/[0]/name=='custstopfilt'",
"/fields/[1]/name=='lowerfilt'",
"/fields/[2]/name=='test_basictv'");
"/fields/[0]/name=='HTMLstandardtok'",
"/fields/[1]/name=='HTMLwhitetok'",
"/fields/[2]/name=='_version_'");
}
@Test