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

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@769040 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2009-04-27 16:05:13 +00:00
parent f1be0a4f20
commit 4f2697e904
4 changed files with 41 additions and 30 deletions

View File

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

View File

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

View File

@ -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();
}
}

View File

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