Skip to content

Instantly share code, notes, and snippets.

@karussell
Created February 22, 2011 21:33
Show Gist options
  • Save karussell/839455 to your computer and use it in GitHub Desktop.
Save karussell/839455 to your computer and use it in GitHub Desktop.
different facet entries but only sort changed
a document with a number -> faceting over that
ComparatorType.TOTAL
====================
term:9 count:2 total:2.0
there should be 10 terms. there were:1
ComparatorType.COUNT
====================
term:9 count:2 total:2.0
term:8 count:2 total:2.0
term:7 count:2 total:2.0
term:6 count:2 total:2.0
term:5 count:2 total:2.0
term:4 count:2 total:2.0
term:3 count:2 total:2.0
term:2 count:2 total:2.0
term:1 count:2 total:2.0
term:0 count:2 total:2.0
there should be 10 terms. there were:10
private Client client;
private String indexType = "tweet";
private String NUM = "num";
private String TITLE = "title";
public void start() throws IOException, InterruptedException {
Node node = nodeBuilder().
// local(true).
settings(ImmutableSettings.settingsBuilder().
put("index.number_of_shards", 2).
put("index.number_of_replicas", 0).
put("gateway.type", "none").
build()).
build().
start();
client = node.client();
// create indices
String index1 = "index1";
createIndex(index1);
waitForYellow(index1);
for (int i = 0; i < 20; i++) {
feedDoc(index1, createDoc(i%10, "test this again"), "" + i);
}
String facetField = NUM;
AbstractFacetBuilder fb = FacetBuilders.termsStats(facetField).keyField(facetField).
valueScript("doc.score").order(ComparatorType.COUNT);
SearchResponse rsp = client.prepareSearch(index1).
setQuery(QueryBuilders.matchAllQuery()).
addFacet(fb).setExplain(true).
execute().actionGet();
TermsStatsFacet f = (TermsStatsFacet) rsp.facets().facet(facetField);
for (TermsStatsFacet.Entry e : f.entries()) {
System.out.println("term:"+e.term() + "\t count:" + e.count() + "\t total:" + e.total());// always 1? + "\t" + e.mean());
}
System.out.println("there should be 10 terms. there were:" + f.entries().size());
node.stop();
}
public XContentBuilder createDoc(int num, String title) throws IOException {
XContentBuilder docBuilder = JsonXContent.unCachedContentBuilder().startObject();
docBuilder.field(NUM, num);
docBuilder.field(TITLE, title);
docBuilder.endObject();
return docBuilder;
}
public void feedDoc(String indexName, XContentBuilder b, String id) {
IndexRequestBuilder irb = client.prepareIndex(indexName, indexType, id).
setConsistencyLevel(WriteConsistencyLevel.DEFAULT).setRefresh(true).
setSource(b);
irb.execute().actionGet();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment