Search in sources :

Example 11 with DocList

use of org.apache.solr.search.DocList in project lucene-solr by apache.

the class MoreLikeThisComponent method getMoreLikeThese.

NamedList<DocList> getMoreLikeThese(ResponseBuilder rb, SolrIndexSearcher searcher, DocList docs, int flags) throws IOException {
    SolrParams p = rb.req.getParams();
    IndexSchema schema = searcher.getSchema();
    MoreLikeThisHandler.MoreLikeThisHelper mltHelper = new MoreLikeThisHandler.MoreLikeThisHelper(p, searcher);
    NamedList<DocList> mlt = new SimpleOrderedMap<>();
    DocIterator iterator = docs.iterator();
    SimpleOrderedMap<Object> dbg = null;
    if (rb.isDebug()) {
        dbg = new SimpleOrderedMap<>();
    }
    while (iterator.hasNext()) {
        int id = iterator.nextDoc();
        int rows = p.getInt(MoreLikeThisParams.DOC_COUNT, 5);
        DocListAndSet sim = mltHelper.getMoreLikeThis(id, 0, rows, null, null, flags);
        String name = schema.printableUniqueKey(searcher.doc(id));
        mlt.add(name, sim.docList);
        if (dbg != null) {
            SimpleOrderedMap<Object> docDbg = new SimpleOrderedMap<>();
            docDbg.add("rawMLTQuery", mltHelper.getRawMLTQuery().toString());
            docDbg.add("boostedMLTQuery", mltHelper.getBoostedMLTQuery().toString());
            docDbg.add("realMLTQuery", mltHelper.getRealMLTQuery().toString());
            SimpleOrderedMap<Object> explains = new SimpleOrderedMap<>();
            DocIterator mltIte = sim.docList.iterator();
            while (mltIte.hasNext()) {
                int mltid = mltIte.nextDoc();
                String key = schema.printableUniqueKey(searcher.doc(mltid));
                explains.add(key, searcher.explain(mltHelper.getRealMLTQuery(), mltid));
            }
            docDbg.add("explain", explains);
            dbg.add(name, docDbg);
        }
    }
    // add debug information
    if (dbg != null) {
        rb.addDebugInfo("moreLikeThis", dbg);
    }
    return mlt;
}
Also used : DocIterator(org.apache.solr.search.DocIterator) MoreLikeThisHandler(org.apache.solr.handler.MoreLikeThisHandler) DocListAndSet(org.apache.solr.search.DocListAndSet) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IndexSchema(org.apache.solr.schema.IndexSchema) DocList(org.apache.solr.search.DocList)

Example 12 with DocList

use of org.apache.solr.search.DocList in project Solbase by Photobucket.

the class SolbaseComponent method process.

public void process(ResponseBuilder rb) throws IOException {
    DocList list = rb.getResults().docList;
    DocIterator it = list.iterator();
    List<Integer> docIds = new ArrayList<Integer>(list.size());
    while (it.hasNext()) docIds.add(it.next());
    IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader()).getWrappedReader();
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    String ids = params.get(ShardParams.IDS);
    // first phase of sharding only tries to fetch docids and scores which are already in tv
    if (SolbaseShardUtil.getNumShard() != 0) {
        if (ids != null) {
            IndexReader.firstPhase.set(false);
        } else {
            IndexReader.firstPhase.set(true);
        }
    } else {
        // it's always false in case of stand alone
        IndexReader.firstPhase.set(false);
    }
    logger.debug(reader.getIndexName() + " : Fetching " + docIds.size() + " Docs");
    if (docIds.size() > 0) {
        List<byte[]> fieldFilter = null;
        Set<String> returnFields = rb.rsp.getReturnFields();
        if (returnFields != null) {
            // copy return fields list
            fieldFilter = new ArrayList<byte[]>(returnFields.size());
            for (String field : returnFields) {
                fieldFilter.add(Bytes.toBytes(field));
            }
            // add highlight fields
            SolrHighlighter highligher = rb.req.getCore().getHighlighter();
            if (highligher.isHighlightingEnabled(rb.req.getParams())) {
                for (String field : highligher.getHighlightFields(rb.getQuery(), rb.req, null)) if (!returnFields.contains(field))
                    fieldFilter.add(Bytes.toBytes(field));
            }
            // fetch unique key if one exists.
            SchemaField keyField = rb.req.getSearcher().getSchema().getUniqueKeyField();
            if (null != keyField)
                if (!returnFields.contains(keyField))
                    fieldFilter.add(Bytes.toBytes(keyField.getName()));
        }
        FieldSelector selector = new SolbaseFieldSelector(docIds, fieldFilter);
        // This will bulk load these docs
        rb.req.getSearcher().getReader().document(docIds.get(0), selector);
    }
    ReaderCache.flushThreadLocalCaches(reader.getIndexName());
}
Also used : DocIterator(org.apache.solr.search.DocIterator) ArrayList(java.util.ArrayList) SolrHighlighter(org.apache.solr.highlight.SolrHighlighter) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrIndexReader(org.apache.solr.search.SolrIndexReader) IndexReader(org.solbase.lucenehbase.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) FieldSelector(org.apache.lucene.document.FieldSelector) DocList(org.apache.solr.search.DocList)

Example 13 with DocList

use of org.apache.solr.search.DocList in project lucene-solr by apache.

the class MoreLikeThisHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    SolrParams params = req.getParams();
    long timeAllowed = (long) params.getInt(CommonParams.TIME_ALLOWED, -1);
    if (timeAllowed > 0) {
        SolrQueryTimeoutImpl.set(timeAllowed);
    }
    try {
        // Set field flags
        ReturnFields returnFields = new SolrReturnFields(req);
        rsp.setReturnFields(returnFields);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }
        String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
        String q = params.get(CommonParams.Q);
        Query query = null;
        SortSpec sortSpec = null;
        List<Query> filters = null;
        try {
            if (q != null) {
                QParser parser = QParser.getParser(q, defType, req);
                query = parser.getQuery();
                sortSpec = parser.getSortSpec(true);
            }
            String[] fqs = req.getParams().getParams(CommonParams.FQ);
            if (fqs != null && fqs.length != 0) {
                filters = new ArrayList<>();
                for (String fq : fqs) {
                    if (fq != null && fq.trim().length() != 0) {
                        QParser fqp = QParser.getParser(fq, req);
                        filters.add(fqp.getQuery());
                    }
                }
            }
        } catch (SyntaxError e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }
        SolrIndexSearcher searcher = req.getSearcher();
        MoreLikeThisHelper mlt = new MoreLikeThisHelper(params, searcher);
        // Hold on to the interesting terms if relevant
        TermStyle termStyle = TermStyle.get(params.get(MoreLikeThisParams.INTERESTING_TERMS));
        List<InterestingTerm> interesting = (termStyle == TermStyle.NONE) ? null : new ArrayList<>(mlt.mlt.getMaxQueryTerms());
        DocListAndSet mltDocs = null;
        // Parse Required Params
        // This will either have a single Reader or valid query
        Reader reader = null;
        try {
            if (q == null || q.trim().length() < 1) {
                Iterable<ContentStream> streams = req.getContentStreams();
                if (streams != null) {
                    Iterator<ContentStream> iter = streams.iterator();
                    if (iter.hasNext()) {
                        reader = iter.next().getReader();
                    }
                    if (iter.hasNext()) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis does not support multiple ContentStreams");
                    }
                }
            }
            int start = params.getInt(CommonParams.START, CommonParams.START_DEFAULT);
            int rows = params.getInt(CommonParams.ROWS, CommonParams.ROWS_DEFAULT);
            // --------------------------------------------------------------------------------
            if (reader != null) {
                mltDocs = mlt.getMoreLikeThis(reader, start, rows, filters, interesting, flags);
            } else if (q != null) {
                // Matching options
                boolean includeMatch = params.getBool(MoreLikeThisParams.MATCH_INCLUDE, true);
                int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0);
                // Find the base match
                DocList match = searcher.getDocList(query, null, null, matchOffset, 1, // only get the first one...
                flags);
                if (includeMatch) {
                    rsp.add("match", match);
                }
                // This is an iterator, but we only handle the first match
                DocIterator iterator = match.iterator();
                if (iterator.hasNext()) {
                    // do a MoreLikeThis query for each document in results
                    int id = iterator.nextDoc();
                    mltDocs = mlt.getMoreLikeThis(id, start, rows, filters, interesting, flags);
                }
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis requires either a query (?q=) or text to find similar documents.");
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        if (mltDocs == null) {
            // avoid NPE
            mltDocs = new DocListAndSet();
        }
        rsp.addResponse(mltDocs.docList);
        if (interesting != null) {
            if (termStyle == TermStyle.DETAILS) {
                NamedList<Float> it = new NamedList<>();
                for (InterestingTerm t : interesting) {
                    it.add(t.term.toString(), t.boost);
                }
                rsp.add("interestingTerms", it);
            } else {
                List<String> it = new ArrayList<>(interesting.size());
                for (InterestingTerm t : interesting) {
                    it.add(t.term.text());
                }
                rsp.add("interestingTerms", it);
            }
        }
        // maybe facet the results
        if (params.getBool(FacetParams.FACET, false)) {
            if (mltDocs.docSet == null) {
                rsp.add("facet_counts", null);
            } else {
                SimpleFacets f = new SimpleFacets(req, mltDocs.docSet, params);
                rsp.add("facet_counts", FacetComponent.getFacetCounts(f));
            }
        }
        boolean dbg = req.getParams().getBool(CommonParams.DEBUG_QUERY, false);
        boolean dbgQuery = false, dbgResults = false;
        if (dbg == false) {
            //if it's true, we are doing everything anyway.
            String[] dbgParams = req.getParams().getParams(CommonParams.DEBUG);
            if (dbgParams != null) {
                for (String dbgParam : dbgParams) {
                    if (dbgParam.equals(CommonParams.QUERY)) {
                        dbgQuery = true;
                    } else if (dbgParam.equals(CommonParams.RESULTS)) {
                        dbgResults = true;
                    }
                }
            }
        } else {
            dbgQuery = true;
            dbgResults = true;
        }
        // Copied from StandardRequestHandler... perhaps it should be added to doStandardDebug?
        if (dbg == true) {
            try {
                NamedList<Object> dbgInfo = SolrPluginUtils.doStandardDebug(req, q, mlt.getRawMLTQuery(), mltDocs.docList, dbgQuery, dbgResults);
                if (null != dbgInfo) {
                    if (null != filters) {
                        dbgInfo.add("filter_queries", req.getParams().getParams(CommonParams.FQ));
                        List<String> fqs = new ArrayList<>(filters.size());
                        for (Query fq : filters) {
                            fqs.add(QueryParsing.toString(fq, req.getSchema()));
                        }
                        dbgInfo.add("parsed_filter_queries", fqs);
                    }
                    rsp.add("debug", dbgInfo);
                }
            } catch (Exception e) {
                SolrException.log(log, "Exception during debug", e);
                rsp.add("exception_during_debug", SolrException.toStr(e));
            }
        }
    } catch (ExitableDirectoryReader.ExitingReaderException ex) {
        log.warn("Query: " + req.getParamString() + "; " + ex.getMessage());
    } finally {
        SolrQueryTimeoutImpl.reset();
    }
}
Also used : DocIterator(org.apache.solr.search.DocIterator) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DocListAndSet(org.apache.solr.search.DocListAndSet) ArrayList(java.util.ArrayList) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) Reader(java.io.Reader) IndexReader(org.apache.lucene.index.IndexReader) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ContentStream(org.apache.solr.common.util.ContentStream) SyntaxError(org.apache.solr.search.SyntaxError) TermStyle(org.apache.solr.common.params.MoreLikeThisParams.TermStyle) SolrException(org.apache.solr.common.SolrException) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) NamedList(org.apache.solr.common.util.NamedList) SimpleFacets(org.apache.solr.request.SimpleFacets) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) SortSpec(org.apache.solr.search.SortSpec) DocList(org.apache.solr.search.DocList)

Example 14 with DocList

use of org.apache.solr.search.DocList in project lucene-solr by apache.

the class CarrotClusteringEngineTest method checkEngine.

private List<NamedList<Object>> checkEngine(CarrotClusteringEngine engine, int expectedNumDocs, int expectedNumClusters, Query query, SolrParams clusteringParams) throws IOException {
    // Get all documents to cluster
    RefCounted<SolrIndexSearcher> ref = h.getCore().getSearcher();
    DocList docList;
    try {
        SolrIndexSearcher searcher = ref.get();
        docList = searcher.getDocList(query, (Query) null, new Sort(), 0, numberOfDocs);
        assertEquals("docList size", expectedNumDocs, docList.matches());
        ModifiableSolrParams solrParams = new ModifiableSolrParams();
        solrParams.add(clusteringParams);
        // Perform clustering
        LocalSolrQueryRequest req = new LocalSolrQueryRequest(h.getCore(), solrParams);
        Map<SolrDocument, Integer> docIds = new HashMap<>(docList.size());
        SolrDocumentList solrDocList = ClusteringComponent.docListToSolrDocumentList(docList, searcher, engine.getFieldsToLoad(req), docIds);
        @SuppressWarnings("unchecked") List<NamedList<Object>> results = (List<NamedList<Object>>) engine.cluster(query, solrDocList, docIds, req);
        req.close();
        assertEquals("number of clusters: " + results, expectedNumClusters, results.size());
        checkClusters(results, false);
        return results;
    } finally {
        ref.decref();
    }
}
Also used : Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) HashMap(java.util.HashMap) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) Sort(org.apache.lucene.search.Sort) SolrDocumentList(org.apache.solr.common.SolrDocumentList) DocList(org.apache.solr.search.DocList) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) DocList(org.apache.solr.search.DocList)

Example 15 with DocList

use of org.apache.solr.search.DocList in project lucene-solr by apache.

the class ClusteringComponentTest method testDocListConversion.

// tests ClusteringComponent.docListToSolrDocumentList
@Test
public void testDocListConversion() throws Exception {
    assertU("", adoc("id", "3234", "url", "ignoreme", "val_i", "1", "val_dynamic", "quick red fox"));
    assertU("", adoc("id", "3235", "url", "ignoreme", "val_i", "1", "val_dynamic", "quick green fox"));
    assertU("", adoc("id", "3236", "url", "ignoreme", "val_i", "1", "val_dynamic", "quick brown fox"));
    assertU("", commit());
    RefCounted<SolrIndexSearcher> holder = h.getCore().getSearcher();
    try {
        SolrIndexSearcher srchr = holder.get();
        QueryResult qr = new QueryResult();
        QueryCommand cmd = new QueryCommand();
        cmd.setQuery(new MatchAllDocsQuery());
        cmd.setLen(10);
        qr = srchr.search(qr, cmd);
        DocList docs = qr.getDocList();
        assertEquals("wrong docs size", 3, docs.size());
        Set<String> fields = new HashSet<>();
        fields.add("val_dynamic");
        fields.add("dynamic_val");
        // copied from id
        fields.add("range_facet_l");
        SolrDocumentList list = ClusteringComponent.docListToSolrDocumentList(docs, srchr, fields, null);
        assertEquals("wrong list Size", docs.size(), list.size());
        for (SolrDocument document : list) {
            assertTrue("unexpected field", !document.containsKey("val_i"));
            assertTrue("unexpected id field", !document.containsKey("id"));
            assertTrue("original field", document.containsKey("val_dynamic"));
            assertTrue("dyn copy field", document.containsKey("dynamic_val"));
            assertTrue("copy field", document.containsKey("range_facet_l"));
            assertNotNull("original field null", document.get("val_dynamic"));
            assertNotNull("dyn copy field null", document.get("dynamic_val"));
            assertNotNull("copy field null", document.get("range_facet_l"));
        }
    } finally {
        if (null != holder)
            holder.decref();
    }
}
Also used : QueryResult(org.apache.solr.search.QueryResult) SolrDocument(org.apache.solr.common.SolrDocument) QueryCommand(org.apache.solr.search.QueryCommand) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrDocumentList(org.apache.solr.common.SolrDocumentList) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DocList(org.apache.solr.search.DocList) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

DocList (org.apache.solr.search.DocList)23 SolrParams (org.apache.solr.common.params.SolrParams)11 NamedList (org.apache.solr.common.util.NamedList)11 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)11 SolrDocument (org.apache.solr.common.SolrDocument)9 ArrayList (java.util.ArrayList)8 Query (org.apache.lucene.search.Query)8 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)8 SchemaField (org.apache.solr.schema.SchemaField)8 DocIterator (org.apache.solr.search.DocIterator)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)6 ResultContext (org.apache.solr.response.ResultContext)6 FieldType (org.apache.solr.schema.FieldType)6 Document (org.apache.lucene.document.Document)5 Sort (org.apache.lucene.search.Sort)5 SolrException (org.apache.solr.common.SolrException)5 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)5 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4