Example usage for org.apache.solr.search SolrIndexSearcher getDocList

List of usage examples for org.apache.solr.search SolrIndexSearcher getDocList

Introduction

In this page you can find the example usage for org.apache.solr.search SolrIndexSearcher getDocList.

Prototype

public DocList getDocList(Query query, DocSet filter, Sort lsort, int offset, int len) throws IOException 

Source Link

Document

Returns documents matching both query and filter and sorted by sort.

Usage

From source file:net.semanticmetadata.lire.solr.LireRequestHandler.java

License:Open Source License

/**
 * Actual search implementation based on (i) hash based retrieval and (ii) feature based re-ranking.
 *
 * @param req           the SolrQueryRequest
 * @param rsp           the response to write the data to
 * @param searcher      the actual index searcher object to search the index
 * @param hashFieldName the name of the field the hashes can be found
 * @param maximumHits   the maximum number of hits, the smaller the faster
 * @param filterQuery   can be null/*from w  w w . ja  v  a  2 s.c  om*/
 * @param query         the (Boolean) query for querying the candidates from the IndexSearcher
 * @param queryFeature  the image feature used for re-ranking the results
 * @throws IOException
 * @throws IllegalAccessException
 * @throws InstantiationException
 */
private void doSearch(SolrQueryRequest req, SolrQueryResponse rsp, SolrIndexSearcher searcher,
        String hashFieldName, int maximumHits, Query filterQuery, Query query, GlobalFeature queryFeature)
        throws IOException, IllegalAccessException, InstantiationException {
    // temp feature instance
    GlobalFeature tmpFeature = queryFeature.getClass().newInstance();
    // Taking the time of search for statistical purposes.
    time = System.currentTimeMillis();

    String featureFieldName = FeatureRegistry.getFeatureFieldName(hashFieldName);
    BinaryDocValues binaryValues = MultiDocValues.getBinaryValues(searcher.getIndexReader(), featureFieldName);

    time = System.currentTimeMillis() - time;
    rsp.add("DocValuesOpenTime", time + "");

    Iterator<Integer> docIterator;
    int numberOfResults = 0;
    time = System.currentTimeMillis();
    if (filterQuery != null) {
        DocList docList = searcher.getDocList(query, filterQuery, Sort.RELEVANCE, 0, numberOfCandidateResults);
        numberOfResults = docList.size();
        docIterator = docList.iterator();
    } else {
        TopDocs docs = searcher.search(query, numberOfCandidateResults);
        numberOfResults = docs.totalHits;
        docIterator = new TopDocsIterator(docs);
    }
    time = System.currentTimeMillis() - time;
    rsp.add("RawDocsCount", numberOfResults + "");
    rsp.add("RawDocsSearchTime", time + "");
    time = System.currentTimeMillis();
    TreeSet<CachingSimpleResult> resultScoreDocs = getReRankedResults(docIterator, binaryValues, queryFeature,
            tmpFeature, maximumHits, searcher);

    // Creating response ...
    time = System.currentTimeMillis() - time;
    rsp.add("ReRankSearchTime", time + "");
    LinkedList list = new LinkedList();
    for (Iterator<CachingSimpleResult> it = resultScoreDocs.iterator(); it.hasNext();) {
        CachingSimpleResult result = it.next();
        HashMap m = new HashMap(2);
        m.put("d", result.getDistance());
        // add fields as requested:
        if (req.getParams().get("fl") == null) {
            m.put("id", result.getDocument().get("id"));
            if (result.getDocument().get("title") != null)
                m.put("title", result.getDocument().get("title"));
        } else {
            String fieldsRequested = req.getParams().get("fl");
            if (fieldsRequested.contains("score")) {
                m.put("score", result.getDistance());
            }
            if (fieldsRequested.contains("*")) {
                // all fields
                for (IndexableField field : result.getDocument().getFields()) {
                    String tmpField = field.name();

                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            } else {
                StringTokenizer st;
                if (fieldsRequested.contains(","))
                    st = new StringTokenizer(fieldsRequested, ",");
                else
                    st = new StringTokenizer(fieldsRequested, " ");
                while (st.hasMoreElements()) {
                    String tmpField = st.nextToken();
                    if (result.getDocument().getFields(tmpField).length > 1) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getValues(tmpField));
                    } else if (result.getDocument().getFields(tmpField).length > 0) {
                        m.put(result.getDocument().getFields(tmpField)[0].name(),
                                result.getDocument().getFields(tmpField)[0].stringValue());
                    }
                }
            }
        }
        //            m.put(field, result.getDocument().get(field));
        //            m.put(field.replace("_ha", "_hi"), result.getDocument().getBinaryValue(field));
        list.add(m);
    }
    rsp.add("docs", list);
    // rsp.add("Test-name", "Test-val");
}