Term Facets: Add `reverse_cout` and `reverse_term` to `order` options, closes #314.

This commit is contained in:
kimchy 2010-08-12 15:26:26 +03:00
parent b1d1f1ff94
commit e29925684a
2 changed files with 48 additions and 5 deletions

View File

@ -37,7 +37,7 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
*/
public static enum ComparatorType {
/**
* Order by the count of each term.
* Order by the (higher) count of each term.
*/
COUNT((byte) 0, new Comparator<Entry>() {
@ -53,9 +53,18 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
}
}),
/**
* Order by the count of each term.
* Order by the (lower) count of each term.
*/
TERM((byte) 1, new Comparator<Entry>() {
REVERSE_COUNT((byte) 1, new Comparator<Entry>() {
@Override public int compare(Entry o1, Entry o2) {
return -COUNT.comparator().compare(o1, o2);
}
}),
/**
* Order by the terms.
*/
TERM((byte) 2, new Comparator<Entry>() {
@Override public int compare(Entry o1, Entry o2) {
int i = o1.term().compareTo(o2.term());
@ -67,6 +76,15 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
}
return i;
}
}),
/**
* Order by the terms.
*/
REVERSE_TERM((byte) 3, new Comparator<Entry>() {
@Override public int compare(Entry o1, Entry o2) {
return -TERM.comparator().compare(o1, o2);
}
});
private final byte id;
@ -87,10 +105,14 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
}
public static ComparatorType fromId(byte id) {
if (id == 0) {
if (id == COUNT.id()) {
return COUNT;
} else if (id == 1) {
} else if (id == REVERSE_COUNT.id()) {
return REVERSE_COUNT;
} else if (id == TERM.id()) {
return TERM;
} else if (id == REVERSE_TERM.id()) {
return REVERSE_TERM;
}
throw new ElasticSearchIllegalArgumentException("No type argument match for terms facet comparator [" + id + "]");
}
@ -100,6 +122,10 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
return COUNT;
} else if ("term".equals(type)) {
return TERM;
} else if ("reverse_count".equals(type) || "reverseCount".equals(type)) {
return REVERSE_COUNT;
} else if ("reverse_term".equals(type) || "reverseTerm".equals(type)) {
return REVERSE_TERM;
}
throw new ElasticSearchIllegalArgumentException("No type argument match for terms facet comparator [" + type + "]");
}

View File

@ -232,6 +232,8 @@ public class SimpleFacetsTests extends AbstractNodesTests {
assertThat(facet.entries().get(2).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
assertThat(facet.entries().get(2).count(), equalTo(1));
// Bounded Size
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(2))
@ -277,6 +279,21 @@ public class SimpleFacetsTests extends AbstractNodesTests {
assertThat(facet.entries().get(2).term(), equalTo("zzz"));
assertThat(facet.entries().get(2).count(), equalTo(1));
searchResponse = client.prepareSearch()
.setQuery(matchAllQuery())
.addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.REVERSE_TERM))
.execute().actionGet();
facet = searchResponse.facets().facet("facet1");
assertThat(facet.name(), equalTo("facet1"));
assertThat(facet.entries().size(), equalTo(3));
assertThat(facet.entries().get(2).term(), equalTo("xxx"));
assertThat(facet.entries().get(2).count(), equalTo(1));
assertThat(facet.entries().get(1).term(), equalTo("yyy"));
assertThat(facet.entries().get(1).count(), equalTo(2));
assertThat(facet.entries().get(0).term(), equalTo("zzz"));
assertThat(facet.entries().get(0).count(), equalTo(1));
// Script
searchResponse = client.prepareSearch()