Terms API: add the ability to return terms as array (termsAsArray flag), defaulting to false

This commit is contained in:
kimchy 2010-02-22 01:48:29 +02:00
parent 7cda7eca54
commit 38edf96271
1 changed files with 17 additions and 5 deletions

View File

@ -107,6 +107,7 @@ public class RestTermsAction extends BaseRestHandler {
return; return;
} }
final boolean termsAsArray = request.paramAsBoolean("termsAsArray", false);
client.execTerms(termsRequest, new ActionListener<TermsResponse>() { client.execTerms(termsRequest, new ActionListener<TermsResponse>() {
@Override public void onResponse(TermsResponse response) { @Override public void onResponse(TermsResponse response) {
try { try {
@ -125,13 +126,24 @@ public class RestTermsAction extends BaseRestHandler {
for (FieldTermsFreq fieldTermsFreq : response.fields()) { for (FieldTermsFreq fieldTermsFreq : response.fields()) {
builder.startObject(fieldTermsFreq.fieldName()); builder.startObject(fieldTermsFreq.fieldName());
builder.startObject("terms"); if (!termsAsArray) {
for (TermFreq termFreq : fieldTermsFreq.termsFreqs()) { builder.startObject("terms");
builder.startObject(termFreq.term()); for (TermFreq termFreq : fieldTermsFreq.termsFreqs()) {
builder.field("docFreq", termFreq.docFreq()); builder.startObject(termFreq.term());
builder.field("docFreq", termFreq.docFreq());
builder.endObject();
}
builder.endObject(); builder.endObject();
} else {
builder.startArray("terms");
for (TermFreq termFreq : fieldTermsFreq.termsFreqs()) {
builder.startObject();
builder.field("term", termFreq.term());
builder.field("docFreq", termFreq.docFreq());
builder.endObject();
}
builder.endArray();
} }
builder.endObject();
builder.endObject(); builder.endObject();
} }