Skip to content

Apache Lucene open-source search software

SOLR-4022: Allow sorting on ExternalFileFields

Alan Woodward0f57f8bdef16
diff --git a/solr/core/src/java/org/apache/solr/schema/ExternalFileField.java b/solr/core/src/java/org/apache/solr/schema/ExternalFileField.java
index 64dfa70334b..18faaeabae0 100755
--- a/solr/core/src/java/org/apache/solr/schema/ExternalFileField.java
+++ b/solr/core/src/java/org/apache/solr/schema/ExternalFileField.java
@@ -87,7 +87,8 @@ public class ExternalFileField extends FieldType {
@Override
public SortField getSortField(SchemaField field,boolean reverse) {
- throw new UnsupportedOperationException();
+ FileFloatSource source = getFileFloatSource(field);
+ return source.getSortField(reverse);
}
@Override
@@ -96,7 +97,19 @@ public class ExternalFileField extends FieldType {
}
/**
- * Get a FileFloatSource for the given field, looking in datadir for the relevant file
+ * Get a FileFloatSource for the given field, using the datadir from the
+ * IndexSchema
+ * @param field the field to get a source for
+ * @return a FileFloatSource
+ */
+ public FileFloatSource getFileFloatSource(SchemaField field) {
+ return getFileFloatSource(field, schema.getResourceLoader().getDataDir());
+ }
+
+ /**
+ * Get a FileFloatSource for the given field. Call this in preference to
+ * getFileFloatSource(SchemaField) if this may be called before the Core is
+ * fully initialised (eg in SolrEventListener calls).
* @param field the field to get a source for
* @param datadir the data directory in which to look for the external file
* @return a FileFloatSource
diff --git a/solr/core/src/test-files/solr/collection1/conf/schema-eff.xml b/solr/core/src/test-files/solr/collection1/conf/schema-eff.xml
new file mode 100644
index 00000000000..22449a1346c
--- /dev/null
+++ b/solr/core/src/test-files/solr/collection1/conf/schema-eff.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ -->
+
+<schema name="example" version="1.5">
+
+ <fields>
+
+ <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
+ <field name="eff" type="flax-eff"/>
+
+ </fields>
+
+
+ <!-- Field to use to determine and enforce document uniqueness.
+ Unless this field is marked with required="false", it will be a required field
+ -->
+ <uniqueKey>id</uniqueKey>
+
+ <types>
+
+ <!-- The StrField type is not analyzed, but indexed/stored verbatim. -->
+ <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
+
+ <!-- Our external file field type -->
+ <fieldType name="flax-eff" class="solr.ExternalFileField"/>
+
+ </types>
+
+</schema>
diff --git a/solr/core/src/test-files/solr/external_eff b/solr/core/src/test-files/solr/external_eff
new file mode 100644
index 00000000000..a23f9b554bd
--- /dev/null
+++ b/solr/core/src/test-files/solr/external_eff
@@ -0,0 +1,10 @@
+1=0.354
+2=0.975
+3=0.001
+4=100.35
+5=53.9
+6=70
+7=3.957
+8=1400
+9=24
+10=450
\ No newline at end of file
diff --git a/solr/core/src/test/org/apache/solr/schema/ExternalFileFieldSortTest.java b/solr/core/src/test/org/apache/solr/schema/ExternalFileFieldSortTest.java
new file mode 100644
index 00000000000..b8b373bb26b
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/schema/ExternalFileFieldSortTest.java
@@ -0,0 +1,60 @@
+package org.apache.solr.schema;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.solr.SolrTestCaseJ4;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class ExternalFileFieldSortTest extends SolrTestCaseJ4 {
+
+ @BeforeClass
+ public static void beforeTests() throws Exception {
+ initCore("solrconfig-basic.xml", "schema-eff.xml");
+ updateExternalFile();
+ }
+
+ static void updateExternalFile() throws IOException {
+ final String testHome = SolrTestCaseJ4.getFile("solr/collection1").getParent();
+ String filename = "external_eff";
+ FileUtils.copyFile(new File(testHome + "/" + filename),
+ new File(h.getCore().getDataDir() + "/" + filename));
+ }
+
+ private void addDocuments() {
+ for (int i = 1; i <= 10; i++) {
+ String id = Integer.toString(i);
+ assertU("add a test doc", adoc("id", id));
+ }
+ assertU("commit", commit());
+ }
+
+ @Test
+ public void testSort() {
+ addDocuments();
+ assertQ("query",
+ req("q", "*:*", "sort", "eff asc"),
+ "//result/doc[position()=1]/str[.='3']",
+ "//result/doc[position()=2]/str[.='1']",
+ "//result/doc[position()=10]/str[.='8']");
+ }
+}