SOLR-4022: Allow sorting on ExternalFileFields

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1408646 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Alan Woodward 2012-11-13 09:19:10 +00:00
parent ddb20fd43d
commit 0f57f8bdef
4 changed files with 130 additions and 2 deletions

View File

@ -87,7 +87,8 @@ public class ExternalFileField extends FieldType {
@Override @Override
public SortField getSortField(SchemaField field,boolean reverse) { public SortField getSortField(SchemaField field,boolean reverse) {
throw new UnsupportedOperationException(); FileFloatSource source = getFileFloatSource(field);
return source.getSortField(reverse);
} }
@Override @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 field the field to get a source for
* @param datadir the data directory in which to look for the external file * @param datadir the data directory in which to look for the external file
* @return a FileFloatSource * @return a FileFloatSource

View File

@ -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>

View File

@ -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

View File

@ -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']");
}
}