mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-01 16:39:11 +00:00
Term Facets: Add reverse_cout
and reverse_term
to order
options, closes #314.
This commit is contained in:
parent
b1d1f1ff94
commit
e29925684a
@ -37,7 +37,7 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
|
|||||||
*/
|
*/
|
||||||
public static enum ComparatorType {
|
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>() {
|
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) {
|
@Override public int compare(Entry o1, Entry o2) {
|
||||||
int i = o1.term().compareTo(o2.term());
|
int i = o1.term().compareTo(o2.term());
|
||||||
@ -67,6 +76,15 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
|
|||||||
}
|
}
|
||||||
return i;
|
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;
|
private final byte id;
|
||||||
@ -87,10 +105,14 @@ public interface TermsFacet extends Facet, Iterable<TermsFacet.Entry> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ComparatorType fromId(byte id) {
|
public static ComparatorType fromId(byte id) {
|
||||||
if (id == 0) {
|
if (id == COUNT.id()) {
|
||||||
return COUNT;
|
return COUNT;
|
||||||
} else if (id == 1) {
|
} else if (id == REVERSE_COUNT.id()) {
|
||||||
|
return REVERSE_COUNT;
|
||||||
|
} else if (id == TERM.id()) {
|
||||||
return TERM;
|
return TERM;
|
||||||
|
} else if (id == REVERSE_TERM.id()) {
|
||||||
|
return REVERSE_TERM;
|
||||||
}
|
}
|
||||||
throw new ElasticSearchIllegalArgumentException("No type argument match for terms facet comparator [" + id + "]");
|
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;
|
return COUNT;
|
||||||
} else if ("term".equals(type)) {
|
} else if ("term".equals(type)) {
|
||||||
return TERM;
|
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 + "]");
|
throw new ElasticSearchIllegalArgumentException("No type argument match for terms facet comparator [" + type + "]");
|
||||||
}
|
}
|
||||||
|
@ -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).term(), anyOf(equalTo("xxx"), equalTo("zzz")));
|
||||||
assertThat(facet.entries().get(2).count(), equalTo(1));
|
assertThat(facet.entries().get(2).count(), equalTo(1));
|
||||||
|
|
||||||
|
// Bounded Size
|
||||||
|
|
||||||
searchResponse = client.prepareSearch()
|
searchResponse = client.prepareSearch()
|
||||||
.setQuery(matchAllQuery())
|
.setQuery(matchAllQuery())
|
||||||
.addFacet(termsFacet("facet1").field("tag").size(2))
|
.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).term(), equalTo("zzz"));
|
||||||
assertThat(facet.entries().get(2).count(), equalTo(1));
|
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
|
// Script
|
||||||
|
|
||||||
searchResponse = client.prepareSearch()
|
searchResponse = client.prepareSearch()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user