Search in sources :

Example 6 with ReturnFields

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

the class JSONWriterTest method testJSONSolrDocument.

@Test
public void testJSONSolrDocument() throws IOException {
    SolrQueryRequest req = req(CommonParams.WT, "json", CommonParams.FL, "id,score");
    SolrQueryResponse rsp = new SolrQueryResponse();
    JSONResponseWriter w = new JSONResponseWriter();
    ReturnFields returnFields = new SolrReturnFields(req);
    rsp.setReturnFields(returnFields);
    StringWriter buf = new StringWriter();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField("id", "1");
    solrDoc.addField("subject", "hello2");
    solrDoc.addField("title", "hello3");
    solrDoc.addField("score", "0.7");
    SolrDocumentList list = new SolrDocumentList();
    list.setNumFound(1);
    list.setStart(0);
    list.setMaxScore(0.7f);
    list.add(solrDoc);
    rsp.addResponse(list);
    w.write(buf, req, rsp);
    String result = buf.toString();
    assertFalse("response contains unexpected fields: " + result, result.contains("hello") || result.contains("\"subject\"") || result.contains("\"title\""));
    assertTrue("response doesn't contain expected fields: " + result, result.contains("\"id\"") && result.contains("\"score\""));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) StringWriter(java.io.StringWriter) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

Example 7 with ReturnFields

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

the class SmileWriterTest method testJSONSolrDocument.

@Test
public void testJSONSolrDocument() throws IOException {
    SolrQueryRequest req = req(CommonParams.WT, "json", CommonParams.FL, "id,score");
    SolrQueryResponse rsp = new SolrQueryResponse();
    SmileResponseWriter w = new SmileResponseWriter();
    ReturnFields returnFields = new SolrReturnFields(req);
    rsp.setReturnFields(returnFields);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    SolrDocument solrDoc = new SolrDocument();
    solrDoc.addField("id", "1");
    solrDoc.addField("subject", "hello2");
    solrDoc.addField("title", "hello3");
    solrDoc.addField("score", "0.7");
    SolrDocumentList list = new SolrDocumentList();
    list.setNumFound(1);
    list.setStart(0);
    list.setMaxScore(0.7f);
    list.add(solrDoc);
    rsp.addResponse(list);
    w.write(buf, req, rsp);
    byte[] bytes = buf.toByteArray();
    Map m = (Map) decodeSmile(new ByteArrayInputStream(bytes));
    m = (Map) m.get("response");
    List l = (List) m.get("docs");
    Map doc = (Map) l.get(0);
    assertFalse(doc.containsKey("subject"));
    assertFalse(doc.containsKey("title"));
    assertTrue(doc.containsKey("id"));
    assertTrue(doc.containsKey("score"));
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrDocument(org.apache.solr.common.SolrDocument) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SolrDocumentList(org.apache.solr.common.SolrDocumentList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

Example 8 with ReturnFields

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

the class FieldOptions method getFields.

/**
   * Helper method for determining the list of fields that we should 
   * try to find term vectors on.  
   * <p>
   * Does simple (non-glob-supporting) parsing on the 
   * {@link TermVectorParams#FIELDS} param if specified, otherwise it returns 
   * the concrete field values specified in {@link CommonParams#FL} -- 
   * ignoring functions, transformers, or literals.  
   * </p>
   * <p>
   * If "fl=*" is used, or neither param is specified, then <code>null</code> 
   * will be returned.  If the empty set is returned, it means the "fl" 
   * specified consisted entirely of things that are not real fields 
   * (ie: functions, transformers, partial-globs, score, etc...) and not 
   * supported by this component. 
   * </p>
   */
private Set<String> getFields(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    String[] fldLst = params.getParams(TermVectorParams.FIELDS);
    if (null == fldLst || 0 == fldLst.length || (1 == fldLst.length && 0 == fldLst[0].length())) {
        // no tv.fl, parse the main fl
        ReturnFields rf = new SolrReturnFields(params.getParams(CommonParams.FL), rb.req);
        if (rf.wantsAllFields()) {
            return null;
        }
        Set<String> fieldNames = rf.getLuceneFieldNames();
        return (null != fieldNames) ? fieldNames : // return empty set indicating no fields should be used
        Collections.<String>emptySet();
    }
    // otherwise us the raw fldList as is, no special parsing or globs
    Set<String> fieldNames = new LinkedHashSet<>();
    for (String fl : fldLst) {
        fieldNames.addAll(Arrays.asList(SolrPluginUtils.split(fl)));
    }
    return fieldNames;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) SolrParams(org.apache.solr.common.params.SolrParams) SolrReturnFields(org.apache.solr.search.SolrReturnFields)

Example 9 with ReturnFields

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

the class SolrPluginUtils method optimizePreFetchDocs.

/**
   * Pre-fetch documents into the index searcher's document cache.
   *
   * This is an entirely optional step which you might want to perform for
   * the following reasons:
   *
   * <ul>
   *     <li>Locates the document-retrieval costs in one spot, which helps
   *     detailed performance measurement</li>
   *
   *     <li>Determines a priori what fields will be needed to be fetched by
   *     various subtasks, like response writing and highlighting.  This
   *     minimizes the chance that many needed fields will be loaded lazily.
   *     (it is more efficient to load all the field we require normally).</li>
   * </ul>
   *
   * If lazy field loading is disabled, this method does nothing.
   */
public static void optimizePreFetchDocs(ResponseBuilder rb, DocList docs, Query query, SolrQueryRequest req, SolrQueryResponse res) throws IOException {
    SolrIndexSearcher searcher = req.getSearcher();
    if (!searcher.getDocFetcher().isLazyFieldLoadingEnabled()) {
        // nothing to do
        return;
    }
    ReturnFields returnFields = res.getReturnFields();
    if (returnFields.getLuceneFieldNames() != null) {
        Set<String> fieldFilter = returnFields.getLuceneFieldNames();
        if (rb.doHighlights) {
            // copy return fields list
            fieldFilter = new HashSet<>(fieldFilter);
            // add highlight fields
            SolrHighlighter highlighter = HighlightComponent.getHighlighter(req.getCore());
            for (String field : highlighter.getHighlightFields(query, req, null)) fieldFilter.add(field);
            // fetch unique key if one exists.
            SchemaField keyField = searcher.getSchema().getUniqueKeyField();
            if (null != keyField)
                fieldFilter.add(keyField.getName());
        }
        // get documents
        DocIterator iter = docs.iterator();
        for (int i = 0; i < docs.size(); i++) {
            searcher.doc(iter.nextDoc(), fieldFilter);
        }
    }
}
Also used : SolrHighlighter(org.apache.solr.highlight.SolrHighlighter) SchemaField(org.apache.solr.schema.SchemaField) DocIterator(org.apache.solr.search.DocIterator) ReturnFields(org.apache.solr.search.ReturnFields) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher)

Example 10 with ReturnFields

use of org.apache.solr.search.ReturnFields 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)

Aggregations

ReturnFields (org.apache.solr.search.ReturnFields)10 SolrReturnFields (org.apache.solr.search.SolrReturnFields)9 SolrParams (org.apache.solr.common.params.SolrParams)4 ArrayList (java.util.ArrayList)3 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)3 NamedList (org.apache.solr.common.util.NamedList)3 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)3 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)3 Test (org.junit.Test)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 Query (org.apache.lucene.search.Query)2 SolrDocument (org.apache.solr.common.SolrDocument)2 SolrDocumentList (org.apache.solr.common.SolrDocumentList)2 SolrException (org.apache.solr.common.SolrException)2 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)2 DocIterator (org.apache.solr.search.DocIterator)2 DocList (org.apache.solr.search.DocList)2 QParser (org.apache.solr.search.QParser)2 SyntaxError (org.apache.solr.search.SyntaxError)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1