SOLR-764: fix lex sorting for distributed search, change sort string from lex to index

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@749056 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2009-03-01 16:29:31 +00:00
parent bfadf96ede
commit 439a3905ba
7 changed files with 31 additions and 27 deletions

View File

@ -89,14 +89,14 @@ public interface FacetParams {
/**
* String option: "count" causes facets to be sorted
* by the count, "lex" results in lexicographical order.
* by the count, "index" results in index order.
*/
public static final String FACET_SORT = FACET + ".sort";
public static final String FACET_SORT_COUNT = "count";
public static final String FACET_SORT_COUNT_LEGACY = "true";
public static final String FACET_SORT_LEX = "lex";
public static final String FACET_SORT_LEX_LEGACY = "false";
public static final String FACET_SORT_INDEX = "index";
public static final String FACET_SORT_INDEX_LEGACY = "false";
/**
* Only return constraints of a facet field with the given prefix.
@ -151,7 +151,7 @@ public interface FacetParams {
*/
public static final String FACET_DATE_OTHER = FACET_DATE + ".other";
/**
/**
* An enumeration of the legal values for FACET_DATE_OTHER...
* <ul>
* <li>before = the count of matches before the start date</li>

View File

@ -32,6 +32,7 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.request.SimpleFacets;
import org.apache.lucene.util.OpenBitSet;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.schema.FieldType;
import org.apache.lucene.queryParser.ParseException;
/**
@ -269,7 +270,7 @@ public class FacetComponent extends SearchComponent
for (DistribFieldFacet dff : fi.facets.values()) {
if (dff.limit <= 0) continue; // no need to check these facets for refinement
if (dff.minCount <= 1 && dff.sort.equals(FacetParams.FACET_SORT_LEX)) continue;
if (dff.minCount <= 1 && dff.sort.equals(FacetParams.FACET_SORT_INDEX)) continue;
dff._toRefine = new List[rb.shards.length];
ShardFacetCount[] counts = dff.getCountSorted();
@ -373,7 +374,7 @@ public class FacetComponent extends SearchComponent
if (counts == null || dff.needRefinements) {
counts = dff.getCountSorted();
}
} else if (dff.sort.equals(FacetParams.FACET_SORT_LEX)) {
} else if (dff.sort.equals(FacetParams.FACET_SORT_INDEX)) {
counts = dff.getLexSorted();
} else { // TODO: log error or throw exception?
counts = dff.getLexSorted();
@ -513,6 +514,7 @@ class QueryFacet extends FacetBase {
class FieldFacet extends FacetBase {
String field; // the field to facet on... "myfield" for {!key=foo}myfield
FieldType ftype;
int offset;
int limit;
int minCount;
@ -523,11 +525,12 @@ class FieldFacet extends FacetBase {
public FieldFacet(ResponseBuilder rb, String facetStr) {
super(rb, FacetParams.FACET_FIELD, facetStr);
fillParams(rb.req.getParams(), facetOn);
fillParams(rb, rb.req.getParams(), facetOn);
}
private void fillParams(SolrParams params, String field) {
private void fillParams(ResponseBuilder rb, SolrParams params, String field) {
this.field = field;
this.ftype = rb.req.getSchema().getFieldTypeNoEx(this.field);
this.offset = params.getFieldInt(field, FacetParams.FACET_OFFSET, 0);
this.limit = params.getFieldInt(field, FacetParams.FACET_LIMIT, 100);
Integer mincount = params.getFieldInt(field, FacetParams.FACET_MINCOUNT);
@ -540,11 +543,11 @@ class FieldFacet extends FacetBase {
this.minCount = mincount;
this.missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false);
// default to sorting by count if there is a limit.
this.sort = params.getFieldParam(field, FacetParams.FACET_SORT, limit>0 ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_LEX);
this.sort = params.getFieldParam(field, FacetParams.FACET_SORT, limit>0 ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_INDEX);
if (this.sort.equals(FacetParams.FACET_SORT_COUNT_LEGACY)) {
this.sort = FacetParams.FACET_SORT_COUNT;
} else if (this.sort.equals(FacetParams.FACET_SORT_LEX_LEGACY)) {
this.sort = FacetParams.FACET_SORT_LEX;
} else if (this.sort.equals(FacetParams.FACET_SORT_INDEX_LEGACY)) {
this.sort = FacetParams.FACET_SORT_INDEX;
}
this.prefix = params.getFieldParam(field,FacetParams.FACET_PREFIX);
}
@ -592,6 +595,7 @@ class DistribFieldFacet extends FieldFacet {
if (sfc == null) {
sfc = new ShardFacetCount();
sfc.name = name;
sfc.indexed = ftype == null ? sfc.name : ftype.toInternal(sfc.name);
sfc.termNum = termNum++;
counts.put(name, sfc);
}
@ -617,7 +621,7 @@ class DistribFieldFacet extends FieldFacet {
ShardFacetCount[] arr = counts.values().toArray(new ShardFacetCount[counts.size()]);
Arrays.sort(arr, new Comparator<ShardFacetCount>() {
public int compare(ShardFacetCount o1, ShardFacetCount o2) {
return o1.name.compareTo(o2.name);
return o1.indexed.compareTo(o2.indexed);
}
});
countSorted = arr;
@ -630,8 +634,7 @@ class DistribFieldFacet extends FieldFacet {
public int compare(ShardFacetCount o1, ShardFacetCount o2) {
if (o2.count < o1.count) return -1;
else if (o1.count < o2.count) return 1;
// TODO: handle tiebreaks for types other than strings
return o1.name.compareTo(o2.name);
return o1.indexed.compareTo(o2.indexed);
}
});
countSorted = arr;
@ -650,6 +653,7 @@ class DistribFieldFacet extends FieldFacet {
class ShardFacetCount {
String name;
String indexed; // the indexed form of the name... used for comparisons.
long count;
int termNum; // term number starting at 0 (used in bit arrays)

View File

@ -222,7 +222,7 @@ public class SimpleFacets {
}
boolean missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false);
// default to sorting if there is a limit.
String sort = params.getFieldParam(field, FacetParams.FACET_SORT, limit>0 ? "count" : "lex");
String sort = params.getFieldParam(field, FacetParams.FACET_SORT, limit>0 ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_INDEX);
String prefix = params.getFieldParam(field,FacetParams.FACET_PREFIX);

View File

@ -217,7 +217,7 @@ public class SolrQuery extends ModifiableSolrParams
*
* @return facet sort or default of true. <br />
* true corresponds to
* {@link FacetParams#FACET_SORT_COUNT} and <br />false to {@link FacetParams#FACET_SORT_LEX}
* {@link FacetParams#FACET_SORT_COUNT} and <br />false to {@link FacetParams#FACET_SORT_INDEX}
*
* @deprecated Use {@link #getFacetSortString()} instead.
*/
@ -241,11 +241,11 @@ public class SolrQuery extends ModifiableSolrParams
* @param sort sort facets
* @return this
* @deprecated Use {@link #setFacetSort(String)} instead, true corresponds to
* {@link FacetParams#FACET_SORT_COUNT} and false to {@link FacetParams#FACET_SORT_LEX}.
* {@link FacetParams#FACET_SORT_COUNT} and false to {@link FacetParams#FACET_SORT_INDEX}.
*/
@Deprecated
public SolrQuery setFacetSort(boolean sort) {
this.set(FacetParams.FACET_SORT, sort == true ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_LEX);
this.set(FacetParams.FACET_SORT, sort == true ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_INDEX);
return this;
}

View File

@ -541,7 +541,7 @@ public class TestDistributedSearch extends TestCase {
query("q","*:*", "rows",100, "facet","true", "facet.field",t1);
query("q","*:*", "rows",100, "facet","true", "facet.field",t1, "facet.limit",-1, "facet.sort","count");
query("q","*:*", "rows",100, "facet","true", "facet.field",t1, "facet.limit",-1, "facet.sort","lex");
query("q","*:*", "rows",100, "facet","true", "facet.field",t1, "facet.limit",-1, "facet.sort","index");
query("q","*:*", "rows",100, "facet","true", "facet.field",t1,"facet.limit",1);
query("q","*:*", "rows",100, "facet","true", "facet.query","quick", "facet.query","all", "facet.query","*:*");
query("q","*:*", "rows",100, "facet","true", "facet.field",t1, "facet.offset",1);

View File

@ -95,8 +95,8 @@ public class SolrQueryTest extends TestCase {
public void testFacetSort() {
SolrQuery q = new SolrQuery("dog");
assertEquals("count", q.getFacetSortString());
q.setFacetSort("lex");
assertEquals("lex", q.getFacetSortString());
q.setFacetSort("index");
assertEquals("index", q.getFacetSortString());
}
public void testFacetSortLegacy() {
@ -110,7 +110,7 @@ public class SolrQueryTest extends TestCase {
SolrQuery q = new SolrQuery("foo");
assertEquals(10, q.setFacetLimit(10).getFacetLimit());
assertEquals(10, q.setFacetMinCount(10).getFacetMinCount());
assertEquals("lex", q.setFacetSort("lex").getFacetSortString());
assertEquals("index", q.setFacetSort("index").getFacetSortString());
assertEquals(10, q.setHighlightSnippets(10).getHighlightSnippets());
assertEquals(10, q.setHighlightFragsize(10).getHighlightFragsize());
assertEquals(true, q.setHighlightRequireFieldMatch(true).getHighlightRequireFieldMatch());

View File

@ -457,7 +457,7 @@ public class SimpleFacetsTest extends AbstractSolrTestCase {
,"facet.mincount","1"
,"facet.offset","0"
,"facet.limit","6"
,"facet.sort","lex"
,"facet.sort","index"
)
,"*[count(//lst[@name='facet_fields']/lst/int)=6]"
,pre+"/int[1][@name='A'][.='1']"
@ -476,7 +476,7 @@ public class SimpleFacetsTest extends AbstractSolrTestCase {
,"facet.mincount","1"
,"facet.offset","3"
,"facet.limit","2"
,"facet.sort","lex"
,"facet.sort","index"
)
,"*[count(//lst[@name='facet_fields']/lst/int)=2]"
,pre+"/int[1][@name='E'][.='3']"
@ -491,7 +491,7 @@ public class SimpleFacetsTest extends AbstractSolrTestCase {
,"facet.mincount","2"
,"facet.offset","1"
,"facet.limit","2"
,"facet.sort","lex"
,"facet.sort","index"
)
,"*[count(//lst[@name='facet_fields']/lst/int)=1]"
,pre+"/int[1][@name='G'][.='5']"
@ -555,7 +555,7 @@ public class SimpleFacetsTest extends AbstractSolrTestCase {
,"facet.mincount","0"
,"facet.offset","0"
,"facet.limit","100"
,"facet.sort","lex"
,"facet.sort","index"
,"facet.prefix","B"
)
,"*[count(//lst[@name='facet_fields']/lst/int)=3]"
@ -573,7 +573,7 @@ public class SimpleFacetsTest extends AbstractSolrTestCase {
,"facet.mincount","0"
,"facet.offset","0"
,"facet.limit","100"
,"facet.sort","lex"
,"facet.sort","index"
,"facet.prefix","B"
)
,"*[count(//lst[@name='facet_fields']/lst/int)=3]"