All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scaleset.search.mongo.ResultsConverter Maven / Gradle / Ivy

There is a newer version: 0.24.0
Show newest version
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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy