diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacet.java b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacet.java index 9066067d952..d9c1cc5821d 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacet.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/search/facets/terms/TermsFacet.java @@ -37,7 +37,7 @@ public interface TermsFacet extends Facet, Iterable { */ public static enum ComparatorType { /** - * Order by the count of each term. + * Order by the (higher) count of each term. */ COUNT((byte) 0, new Comparator() { @@ -53,9 +53,18 @@ public interface TermsFacet extends Facet, Iterable { } }), /** - * Order by the count of each term. + * Order by the (lower) count of each term. */ - TERM((byte) 1, new Comparator() { + REVERSE_COUNT((byte) 1, new Comparator() { + + @Override public int compare(Entry o1, Entry o2) { + return -COUNT.comparator().compare(o1, o2); + } + }), + /** + * Order by the terms. + */ + TERM((byte) 2, new Comparator() { @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 { } return i; } + }), + /** + * Order by the terms. + */ + REVERSE_TERM((byte) 3, new Comparator() { + + @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 { } 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 { 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 + "]"); } diff --git a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java index be7a3b4f5ca..9cfe4f7da95 100644 --- a/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java +++ b/modules/test/integration/src/test/java/org/elasticsearch/test/integration/search/facets/SimpleFacetsTests.java @@ -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()