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 * 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 = FACET + ".sort";
public static final String FACET_SORT_COUNT = "count"; public static final String FACET_SORT_COUNT = "count";
public static final String FACET_SORT_COUNT_LEGACY = "true"; public static final String FACET_SORT_COUNT_LEGACY = "true";
public static final String FACET_SORT_LEX = "lex"; public static final String FACET_SORT_INDEX = "index";
public static final String FACET_SORT_LEX_LEGACY = "false"; public static final String FACET_SORT_INDEX_LEGACY = "false";
/** /**
* Only return constraints of a facet field with the given prefix. * Only return constraints of a facet field with the given prefix.

View File

@ -32,6 +32,7 @@ import org.apache.solr.common.SolrException;
import org.apache.solr.request.SimpleFacets; import org.apache.solr.request.SimpleFacets;
import org.apache.lucene.util.OpenBitSet; import org.apache.lucene.util.OpenBitSet;
import org.apache.solr.search.QueryParsing; import org.apache.solr.search.QueryParsing;
import org.apache.solr.schema.FieldType;
import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.ParseException;
/** /**
@ -269,7 +270,7 @@ public class FacetComponent extends SearchComponent
for (DistribFieldFacet dff : fi.facets.values()) { for (DistribFieldFacet dff : fi.facets.values()) {
if (dff.limit <= 0) continue; // no need to check these facets for refinement 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]; dff._toRefine = new List[rb.shards.length];
ShardFacetCount[] counts = dff.getCountSorted(); ShardFacetCount[] counts = dff.getCountSorted();
@ -373,7 +374,7 @@ public class FacetComponent extends SearchComponent
if (counts == null || dff.needRefinements) { if (counts == null || dff.needRefinements) {
counts = dff.getCountSorted(); counts = dff.getCountSorted();
} }
} else if (dff.sort.equals(FacetParams.FACET_SORT_LEX)) { } else if (dff.sort.equals(FacetParams.FACET_SORT_INDEX)) {
counts = dff.getLexSorted(); counts = dff.getLexSorted();
} else { // TODO: log error or throw exception? } else { // TODO: log error or throw exception?
counts = dff.getLexSorted(); counts = dff.getLexSorted();
@ -513,6 +514,7 @@ class QueryFacet extends FacetBase {
class FieldFacet extends FacetBase { class FieldFacet extends FacetBase {
String field; // the field to facet on... "myfield" for {!key=foo}myfield String field; // the field to facet on... "myfield" for {!key=foo}myfield
FieldType ftype;
int offset; int offset;
int limit; int limit;
int minCount; int minCount;
@ -523,11 +525,12 @@ class FieldFacet extends FacetBase {
public FieldFacet(ResponseBuilder rb, String facetStr) { public FieldFacet(ResponseBuilder rb, String facetStr) {
super(rb, FacetParams.FACET_FIELD, 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.field = field;
this.ftype = rb.req.getSchema().getFieldTypeNoEx(this.field);
this.offset = params.getFieldInt(field, FacetParams.FACET_OFFSET, 0); this.offset = params.getFieldInt(field, FacetParams.FACET_OFFSET, 0);
this.limit = params.getFieldInt(field, FacetParams.FACET_LIMIT, 100); this.limit = params.getFieldInt(field, FacetParams.FACET_LIMIT, 100);
Integer mincount = params.getFieldInt(field, FacetParams.FACET_MINCOUNT); Integer mincount = params.getFieldInt(field, FacetParams.FACET_MINCOUNT);
@ -540,11 +543,11 @@ class FieldFacet extends FacetBase {
this.minCount = mincount; this.minCount = mincount;
this.missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false); this.missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false);
// default to sorting by count if there is a limit. // 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)) { if (this.sort.equals(FacetParams.FACET_SORT_COUNT_LEGACY)) {
this.sort = FacetParams.FACET_SORT_COUNT; this.sort = FacetParams.FACET_SORT_COUNT;
} else if (this.sort.equals(FacetParams.FACET_SORT_LEX_LEGACY)) { } else if (this.sort.equals(FacetParams.FACET_SORT_INDEX_LEGACY)) {
this.sort = FacetParams.FACET_SORT_LEX; this.sort = FacetParams.FACET_SORT_INDEX;
} }
this.prefix = params.getFieldParam(field,FacetParams.FACET_PREFIX); this.prefix = params.getFieldParam(field,FacetParams.FACET_PREFIX);
} }
@ -592,6 +595,7 @@ class DistribFieldFacet extends FieldFacet {
if (sfc == null) { if (sfc == null) {
sfc = new ShardFacetCount(); sfc = new ShardFacetCount();
sfc.name = name; sfc.name = name;
sfc.indexed = ftype == null ? sfc.name : ftype.toInternal(sfc.name);
sfc.termNum = termNum++; sfc.termNum = termNum++;
counts.put(name, sfc); counts.put(name, sfc);
} }
@ -617,7 +621,7 @@ class DistribFieldFacet extends FieldFacet {
ShardFacetCount[] arr = counts.values().toArray(new ShardFacetCount[counts.size()]); ShardFacetCount[] arr = counts.values().toArray(new ShardFacetCount[counts.size()]);
Arrays.sort(arr, new Comparator<ShardFacetCount>() { Arrays.sort(arr, new Comparator<ShardFacetCount>() {
public int compare(ShardFacetCount o1, ShardFacetCount o2) { public int compare(ShardFacetCount o1, ShardFacetCount o2) {
return o1.name.compareTo(o2.name); return o1.indexed.compareTo(o2.indexed);
} }
}); });
countSorted = arr; countSorted = arr;
@ -630,8 +634,7 @@ class DistribFieldFacet extends FieldFacet {
public int compare(ShardFacetCount o1, ShardFacetCount o2) { public int compare(ShardFacetCount o1, ShardFacetCount o2) {
if (o2.count < o1.count) return -1; if (o2.count < o1.count) return -1;
else if (o1.count < o2.count) return 1; else if (o1.count < o2.count) return 1;
// TODO: handle tiebreaks for types other than strings return o1.indexed.compareTo(o2.indexed);
return o1.name.compareTo(o2.name);
} }
}); });
countSorted = arr; countSorted = arr;
@ -650,6 +653,7 @@ class DistribFieldFacet extends FieldFacet {
class ShardFacetCount { class ShardFacetCount {
String name; String name;
String indexed; // the indexed form of the name... used for comparisons.
long count; long count;
int termNum; // term number starting at 0 (used in bit arrays) 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); boolean missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false);
// default to sorting if there is a limit. // 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); 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 /> * @return facet sort or default of true. <br />
* true corresponds to * 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. * @deprecated Use {@link #getFacetSortString()} instead.
*/ */
@ -241,11 +241,11 @@ public class SolrQuery extends ModifiableSolrParams
* @param sort sort facets * @param sort sort facets
* @return this * @return this
* @deprecated Use {@link #setFacetSort(String)} instead, true corresponds to * @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 @Deprecated
public SolrQuery setFacetSort(boolean sort) { 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; 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);
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","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.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.query","quick", "facet.query","all", "facet.query","*:*");
query("q","*:*", "rows",100, "facet","true", "facet.field",t1, "facet.offset",1); 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() { public void testFacetSort() {
SolrQuery q = new SolrQuery("dog"); SolrQuery q = new SolrQuery("dog");
assertEquals("count", q.getFacetSortString()); assertEquals("count", q.getFacetSortString());
q.setFacetSort("lex"); q.setFacetSort("index");
assertEquals("lex", q.getFacetSortString()); assertEquals("index", q.getFacetSortString());
} }
public void testFacetSortLegacy() { public void testFacetSortLegacy() {
@ -110,7 +110,7 @@ public class SolrQueryTest extends TestCase {
SolrQuery q = new SolrQuery("foo"); SolrQuery q = new SolrQuery("foo");
assertEquals(10, q.setFacetLimit(10).getFacetLimit()); assertEquals(10, q.setFacetLimit(10).getFacetLimit());
assertEquals(10, q.setFacetMinCount(10).getFacetMinCount()); 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.setHighlightSnippets(10).getHighlightSnippets());
assertEquals(10, q.setHighlightFragsize(10).getHighlightFragsize()); assertEquals(10, q.setHighlightFragsize(10).getHighlightFragsize());
assertEquals(true, q.setHighlightRequireFieldMatch(true).getHighlightRequireFieldMatch()); assertEquals(true, q.setHighlightRequireFieldMatch(true).getHighlightRequireFieldMatch());

View File

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