com.scaleset.search.mongo.ResultsConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaleset-search Show documentation
Show all versions of scaleset-search Show documentation
Java object model for representing query requests in REST protocols.
package com.scaleset.search.mongo;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.scaleset.search.AggregationResults;
import com.scaleset.search.Query;
import com.scaleset.search.Results;
import com.scaleset.utils.Coerce;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ResultsConverter {
private Query query;
private DBCursor cursor;
private int totalItems;
private List items = new ArrayList<>();
private Map aggregationResults = new HashMap<>();
SearchMapping searchMapping;
public ResultsConverter(Query query, DBCursor cursor, SearchMapping searchMapping) {
this.query = query;
this.cursor = cursor;
this.searchMapping = searchMapping;
}
protected void initialize() throws IOException {
totalItems = cursor.count();
while (cursor.hasNext()) {
DBObject obj = cursor.next();
String id = Coerce.toString(obj.get("_id"));
try {
T item = searchMapping.fromDocument(id, obj);
items.add(item);
} catch (Exception e) {
e.printStackTrace();
}
}
cursor.close();
}
protected void addAggResults() {
// aggs not yet supported
}
public Results convert() throws Exception {
initialize();
addAggResults();
Results results = new Results(query, aggregationResults, items, totalItems);
return results;
}
}