Search API: Allow to pass `&fields=` without any fields to cause only id and type to be returned, closes #572.

This commit is contained in:
kimchy 2010-12-21 12:01:51 +02:00
parent a96b294de0
commit 3035254885
3 changed files with 25 additions and 4 deletions

View File

@ -245,6 +245,14 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
return this;
}
/**
* Sets no fields to be loaded, resulting in only id and type to be returned per field.
*/
public SearchRequestBuilder setNoFields() {
sourceBuilder().noFields();
return this;
}
/**
* Adds a field to load and return (note, it must be stored) as part of the search request.
* If none are specified, the source of the document will be return.

View File

@ -179,10 +179,14 @@ public class RestSearchAction extends BaseRestHandler {
String sField = request.param("fields");
if (sField != null) {
String[] sFields = fieldsPattern.split(sField);
if (sFields != null) {
for (String field : sFields) {
searchSourceBuilder.field(field);
if (sField.length() == 0) {
searchSourceBuilder.noFields();
} else {
String[] sFields = fieldsPattern.split(sField);
if (sFields != null) {
for (String field : sFields) {
searchSourceBuilder.field(field);
}
}
}
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.builder;
import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Unicode;
import org.elasticsearch.common.collect.ImmutableList;
import org.elasticsearch.common.collect.Lists;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import org.elasticsearch.common.trove.TObjectFloatHashMap;
@ -210,6 +211,14 @@ public class SearchSourceBuilder implements ToXContent {
return this;
}
/**
* Sets no fields to be loaded, resulting in only id and type to be returned per field.
*/
public SearchSourceBuilder noFields() {
this.fieldNames = ImmutableList.of();
return this;
}
/**
* Sets the fields to load and return as part of the search request. If none are specified,
* the source of the document will be returned.