Skip to content

Apache Lucene open-source search software

SOLR-1110 -- Support sorting on trie fields with Distributed Search

Shalin Shekhar Mangar4f2697e90449
diff --git a/CHANGES.txt b/CHANGES.txt
index a0c8cb1316d..ecfe2ea294f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -213,6 +213,8 @@ New Features
invoked from a sub-reader. top() is implicitly used for the
ord() and rord() functions. (yonik)
+50. SOLR-1110: Support sorting on trie fields with Distributed Search. (Mark Miller, Uwe Schindler via shalin)
+
Optimizations
----------------------
1. SOLR-374: Use IndexReader.reopen to save resources by re-using parts of the
diff --git a/src/java/org/apache/solr/handler/component/QueryComponent.java b/src/java/org/apache/solr/handler/component/QueryComponent.java
index b44496e29c9..9d160ad48ca 100644
--- a/src/java/org/apache/solr/handler/component/QueryComponent.java
+++ b/src/java/org/apache/solr/handler/component/QueryComponent.java
@@ -21,6 +21,11 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.*;
+import org.apache.lucene.search.ExtendedFieldCache.DoubleParser;
+import org.apache.lucene.search.ExtendedFieldCache.LongParser;
+import org.apache.lucene.search.FieldCache.FloatParser;
+import org.apache.lucene.search.FieldCache.IntParser;
+import org.apache.lucene.search.FieldCache.Parser;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.queryParser.ParseException;
import org.apache.solr.common.SolrDocument;
@@ -194,16 +199,16 @@ public class QueryComponent extends SearchComponent
switch (type) {
case SortField.INT:
- comparator = comparatorInt (reader, fieldname);
+ comparator = comparatorInt (reader, fieldname, sortField.getParser());
break;
case SortField.FLOAT:
- comparator = comparatorFloat (reader, fieldname);
+ comparator = comparatorFloat (reader, fieldname, sortField.getParser());
break;
case SortField.LONG:
- comparator = comparatorLong(reader, fieldname);
+ comparator = comparatorLong(reader, fieldname, sortField.getParser());
break;
case SortField.DOUBLE:
- comparator = comparatorDouble(reader, fieldname);
+ comparator = comparatorDouble(reader, fieldname, sortField.getParser());
break;
case SortField.STRING:
if (sortField.getLocale() != null) comparator = comparatorStringLocale (reader, fieldname, sortField.getLocale());
@@ -538,13 +543,14 @@ public class QueryComponent extends SearchComponent
* Returns a comparator for sorting hits according to a field containing integers.
* @param reader Index to use.
* @param fieldname Fieldable containg integer values.
+ * @param parser used to parse term values, null for default.
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
- static ScoreDocComparator comparatorInt (final IndexReader reader, final String fieldname)
+ static ScoreDocComparator comparatorInt (final IndexReader reader, final String fieldname, Parser parser)
throws IOException {
final String field = fieldname.intern();
- final int[] fieldOrder = FieldCache.DEFAULT.getInts (reader, field);
+ final int[] fieldOrder = parser == null ? FieldCache.DEFAULT.getInts (reader, field) : FieldCache.DEFAULT.getInts (reader, field, (IntParser) parser);
return new ScoreDocComparator() {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -569,13 +575,14 @@ public class QueryComponent extends SearchComponent
* Returns a comparator for sorting hits according to a field containing integers.
* @param reader Index to use.
* @param fieldname Fieldable containg integer values.
+ * @param parser used to parse term values, null for default.
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
- static ScoreDocComparator comparatorLong (final IndexReader reader, final String fieldname)
+ static ScoreDocComparator comparatorLong (final IndexReader reader, final String fieldname, Parser parser)
throws IOException {
final String field = fieldname.intern();
- final long[] fieldOrder = ExtendedFieldCache.EXT_DEFAULT.getLongs (reader, field);
+ final long[] fieldOrder = parser == null ? ExtendedFieldCache.EXT_DEFAULT.getLongs(reader, field) : ExtendedFieldCache.EXT_DEFAULT.getLongs(reader, field, (LongParser) parser);
return new ScoreDocComparator() {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -600,13 +607,14 @@ public class QueryComponent extends SearchComponent
* Returns a comparator for sorting hits according to a field containing floats.
* @param reader Index to use.
* @param fieldname Fieldable containg float values.
+ * @param parser used to parse term values, null for default.
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
- static ScoreDocComparator comparatorFloat (final IndexReader reader, final String fieldname)
+ static ScoreDocComparator comparatorFloat (final IndexReader reader, final String fieldname, Parser parser)
throws IOException {
final String field = fieldname.intern();
- final float[] fieldOrder = FieldCache.DEFAULT.getFloats (reader, field);
+ final float[] fieldOrder = parser == null ? FieldCache.DEFAULT.getFloats(reader, field) : FieldCache.DEFAULT.getFloats(reader, field, (FloatParser) parser);
return new ScoreDocComparator () {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
@@ -632,13 +640,14 @@ public class QueryComponent extends SearchComponent
* Returns a comparator for sorting hits according to a field containing doubles.
* @param reader Index to use.
* @param fieldname Fieldable containg float values.
+ * @param parser used to parse term values, null for default.
* @return Comparator for sorting hits.
* @throws IOException If an error occurs reading the index.
*/
- static ScoreDocComparator comparatorDouble(final IndexReader reader, final String fieldname)
+ static ScoreDocComparator comparatorDouble(final IndexReader reader, final String fieldname, Parser parser)
throws IOException {
final String field = fieldname.intern();
- final double[] fieldOrder = ExtendedFieldCache.EXT_DEFAULT.getDoubles (reader, field);
+ final double[] fieldOrder = parser == null ? ExtendedFieldCache.EXT_DEFAULT.getDoubles(reader, field) : ExtendedFieldCache.EXT_DEFAULT.getDoubles(reader, field, (DoubleParser) parser);
return new ScoreDocComparator () {
public final int compare (final ScoreDoc i, final ScoreDoc j) {
diff --git a/src/test/org/apache/solr/TestDistributedSearch.java b/src/test/org/apache/solr/TestDistributedSearch.java
index 1474c3b85d3..29781ac9692 100755
--- a/src/test/org/apache/solr/TestDistributedSearch.java
+++ b/src/test/org/apache/solr/TestDistributedSearch.java
@@ -58,6 +58,7 @@ public class TestDistributedSearch extends TestCase {
String id="id";
String t1="a_t";
String i1="a_i";
+ String tlong = "tlong";
String oddField="oddField_s";
String missingField="missing_but_valid_field_t";
String invalidField="invalid_field_not_in_schema";
@@ -465,20 +466,20 @@ public class TestDistributedSearch extends TestCase {
public void doTest() throws Exception {
del("*:*");
- index(id,1, i1, 100,t1,"now is the time for all good men"
+ index(id,1, i1, 100, tlong, 100,t1,"now is the time for all good men"
,"foo_f", 1.414f, "foo_b", "true", "foo_d", 1.414d);
- index(id,2, i1, 50 ,t1,"to come to the aid of their country.");
- index(id,3, i1, 2 ,t1,"how now brown cow");
- index(id,4, i1, -100 ,t1,"the quick fox jumped over the lazy dog");
- index(id,5, i1, 500 ,t1,"the quick fox jumped way over the lazy dog");
- index(id,6, i1, -600 ,t1,"humpty dumpy sat on a wall");
- index(id,7, i1, 123 ,t1,"humpty dumpy had a great fall");
- index(id,8, i1, 876 ,t1,"all the kings horses and all the kings men");
- index(id,9, i1, 7 ,t1,"couldn't put humpty together again");
- index(id,10, i1, 4321 ,t1,"this too shall pass");
- index(id,11, i1, -987 ,t1,"An eye for eye only ends up making the whole world blind.");
- index(id,12, i1, 379 ,t1,"Great works are performed, not by strength, but by perseverance.");
- index(id,13, i1, 232 ,t1,"no eggs on wall, lesson learned", oddField, "odd man out");
+ index(id,2, i1, 50 , tlong, 50,t1,"to come to the aid of their country.");
+ index(id,3, i1, 2, tlong, 2,t1,"how now brown cow");
+ index(id,4, i1, -100 ,tlong, 101,t1,"the quick fox jumped over the lazy dog");
+ index(id,5, i1, 500, tlong, 500 ,t1,"the quick fox jumped way over the lazy dog");
+ index(id,6, i1, -600, tlong, 600 ,t1,"humpty dumpy sat on a wall");
+ index(id,7, i1, 123, tlong, 123 ,t1,"humpty dumpy had a great fall");
+ index(id,8, i1, 876, tlong, 876,t1,"all the kings horses and all the kings men");
+ index(id,9, i1, 7, tlong, 7,t1,"couldn't put humpty together again");
+ index(id,10, i1, 4321, tlong, 4321,t1,"this too shall pass");
+ index(id,11, i1, -987, tlong, 987,t1,"An eye for eye only ends up making the whole world blind.");
+ index(id,12, i1, 379, tlong, 379,t1,"Great works are performed, not by strength, but by perseverance.");
+ index(id,13, i1, 232, tlong, 232,t1,"no eggs on wall, lesson learned", oddField, "odd man out");
index(id, 14, "SubjectTerms_mfacet", new String[] {"mathematical models", "mathematical analysis"});
index(id, 15, "SubjectTerms_mfacet", new String[] {"test 1", "test 2", "test3"});
@@ -497,6 +498,7 @@ public class TestDistributedSearch extends TestCase {
// these queries should be exactly ordered and scores should exactly match
query("q","*:*", "sort",i1+" desc");
query("q","*:*", "sort",i1+" desc", "fl","*,score");
+ query("q","*:*", "sort",tlong+" desc");
handle.put("maxScore", SKIPVAL);
query("q","{!func}"+i1);// does not expect maxScore. So if it comes ,ignore it. JavaBinCodec.writeSolrDocumentList()
//is agnostic of request params.
@@ -592,8 +594,4 @@ public class TestDistributedSearch extends TestCase {
destroyServers();
}
-
}
-
-
-
diff --git a/src/test/test-files/solr/conf/schema.xml b/src/test/test-files/solr/conf/schema.xml
index ed9819464f8..a653249f39f 100644
--- a/src/test/test-files/solr/conf/schema.xml
+++ b/src/test/test-files/solr/conf/schema.xml
@@ -342,7 +342,8 @@
</fieldtype>
<fieldType name="uuid" class="solr.UUIDField" />
-
+
+ <fieldType name="tlong" class="solr.TrieField" type="long" omitNorms="true" positionIncrementGap="0" indexed="true" stored="false" />
</types>
@@ -431,6 +432,7 @@
<field name="intDefault" type="sint" indexed="true" stored="true" default="42" multiValued="false"/>
+ <field name="tlong" type="tlong" indexed="true" stored="true" />
<!-- Dynamic field definitions. If a field name is not found, dynamicFields
will be used if the name matches any of the patterns.