JsonObject body = message.body();
// Get indices to be searched
String index = body.getString(CONST_INDEX);
JsonArray indices = body.getArray(CONST_INDICES);
List<String> list = new ArrayList<>();
if (index != null) {
list.add(index);
}
if (indices != null) {
for (int i = 0; i < indices.size(); i++) {
list.add(indices.<String>get(i));
}
}
SearchRequestBuilder builder = client.prepareSearch(list.toArray(new String[list.size()]));
// Get types to be searched
String type = body.getString(CONST_TYPE);
JsonArray types = body.getArray("_types");
list.clear();
if (type != null) {
list.add(type);
}
if (types != null) {
for (int i = 0; i < types.size(); i++) {
list.add(types.<String>get(i));
}
}
if (!list.isEmpty()) {
builder.setTypes(list.toArray(new String[list.size()]));
}
// Set the query
JsonObject query = body.getObject("query");
if (query != null) {
builder.setQuery(query.encode());
}
// Set the filter
JsonObject filter = body.getObject("filter");
if (filter != null) {
builder.setPostFilter(filter.encode());
}
// Set facets
JsonObject facets = body.getObject("facets");
if (facets != null) {
builder.setFacets(facets.encode().getBytes(CHARSET_UTF8));
}
// Set search type
String searchType = body.getString("search_type");
if (searchType != null) {
builder.setSearchType(searchType);
}
// Set scroll keep alive time
String scroll = body.getString("scroll");
if (scroll != null) {
builder.setScroll(scroll);
}
// Set Size
Integer size = body.getInteger("size");
if (size != null) {
builder.setSize(size);
}
//Set requested fields
JsonArray fields = body.getArray("fields");
if (fields != null) {
for (int i = 0; i < fields.size(); i++) {
builder.addField(fields.<String>get(i));
}
}
//Set query timeout
Long queryTimeout = body.getLong("timeout");