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,6 +126,7 @@ public class RestTermsAction extends BaseRestHandler {
for (FieldTermsFreq fieldTermsFreq : response.fields()) { for (FieldTermsFreq fieldTermsFreq : response.fields()) {
builder.startObject(fieldTermsFreq.fieldName()); builder.startObject(fieldTermsFreq.fieldName());
if (!termsAsArray) {
builder.startObject("terms"); builder.startObject("terms");
for (TermFreq termFreq : fieldTermsFreq.termsFreqs()) { for (TermFreq termFreq : fieldTermsFreq.termsFreqs()) {
builder.startObject(termFreq.term()); builder.startObject(termFreq.term());
@ -132,6 +134,16 @@ public class RestTermsAction extends BaseRestHandler {
builder.endObject(); 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();
} }