Search in sources :

Example 1 with ReturnFields

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

the class QueryComponent method createMainQuery.

protected void createMainQuery(ResponseBuilder rb) {
    ShardRequest sreq = new ShardRequest();
    sreq.purpose = ShardRequest.PURPOSE_GET_TOP_IDS;
    String keyFieldName = rb.req.getSchema().getUniqueKeyField().getName();
    // one-pass algorithm if only id and score fields are requested, but not if fl=score since that's the same as fl=*,score
    ReturnFields fields = rb.rsp.getReturnFields();
    // distrib.singlePass=true forces a one-pass query regardless of requested fields
    boolean distribSinglePass = rb.req.getParams().getBool(ShardParams.DISTRIB_SINGLE_PASS, false);
    if (distribSinglePass || (fields != null && fields.wantsField(keyFieldName) && fields.getRequestedFieldNames() != null && (!fields.hasPatternMatching() && Arrays.asList(keyFieldName, "score").containsAll(fields.getRequestedFieldNames())))) {
        sreq.purpose |= ShardRequest.PURPOSE_GET_FIELDS;
        rb.onePassDistributedQuery = true;
    }
    sreq.params = new ModifiableSolrParams(rb.req.getParams());
    // TODO: base on current params or original params?
    // don't pass through any shards param
    sreq.params.remove(ShardParams.SHARDS);
    // results from the start.
    if (rb.shards_start > -1) {
        // if the client set shards.start set this explicitly
        sreq.params.set(CommonParams.START, rb.shards_start);
    } else {
        sreq.params.set(CommonParams.START, "0");
    }
    // we could just specify that this is a shard request.
    if (rb.shards_rows > -1) {
        // if the client set shards.rows set this explicity
        sreq.params.set(CommonParams.ROWS, rb.shards_rows);
    } else {
        sreq.params.set(CommonParams.ROWS, rb.getSortSpec().getOffset() + rb.getSortSpec().getCount());
    }
    sreq.params.set(ResponseBuilder.FIELD_SORT_VALUES, "true");
    boolean shardQueryIncludeScore = (rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0 || rb.getSortSpec().includesScore();
    StringBuilder additionalFL = new StringBuilder();
    boolean additionalAdded = false;
    if (distribSinglePass) {
        String[] fls = rb.req.getParams().getParams(CommonParams.FL);
        if (fls != null && fls.length > 0 && (fls.length != 1 || !fls[0].isEmpty())) {
            // If the outer request contains actual FL's use them...
            sreq.params.set(CommonParams.FL, fls);
            if (!fields.wantsField(keyFieldName)) {
                additionalAdded = addFL(additionalFL, keyFieldName, additionalAdded);
            }
        } else {
            // ... else we need to explicitly ask for all fields, because we are going to add
            // additional fields below
            sreq.params.set(CommonParams.FL, "*");
        }
        if (!fields.wantsScore() && shardQueryIncludeScore) {
            additionalAdded = addFL(additionalFL, "score", additionalAdded);
        }
    } else {
        // reset so that only unique key is requested in shard requests
        sreq.params.set(CommonParams.FL, rb.req.getSchema().getUniqueKeyField().getName());
        if (shardQueryIncludeScore) {
            additionalAdded = addFL(additionalFL, "score", additionalAdded);
        }
    }
    if (shardQueryIncludeScore) {
        StatsCache statsCache = rb.req.getCore().getStatsCache();
        statsCache.sendGlobalStats(rb, sreq);
    }
    if (additionalAdded)
        sreq.params.add(CommonParams.FL, additionalFL.toString());
    rb.addRequest(this, sreq);
}
Also used : StatsCache(org.apache.solr.search.stats.StatsCache) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 2 with ReturnFields

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

the class QueryComponent method prepare.

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    if (!params.getBool(COMPONENT_NAME, true)) {
        return;
    }
    SolrQueryResponse rsp = rb.rsp;
    // Set field flags    
    ReturnFields returnFields = new SolrReturnFields(req);
    rsp.setReturnFields(returnFields);
    int flags = 0;
    if (returnFields.wantsScore()) {
        flags |= SolrIndexSearcher.GET_SCORES;
    }
    rb.setFieldFlags(flags);
    String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
    // get it from the response builder to give a different component a chance
    // to set it.
    String queryString = rb.getQueryString();
    if (queryString == null) {
        // this is the normal way it's set.
        queryString = params.get(CommonParams.Q);
        rb.setQueryString(queryString);
    }
    try {
        QParser parser = QParser.getParser(rb.getQueryString(), defType, req);
        Query q = parser.getQuery();
        if (q == null) {
            // normalize a null query to a query that matches nothing
            q = new MatchNoDocsQuery();
        }
        rb.setQuery(q);
        String rankQueryString = rb.req.getParams().get(CommonParams.RQ);
        if (rankQueryString != null) {
            QParser rqparser = QParser.getParser(rankQueryString, defType, req);
            Query rq = rqparser.getQuery();
            if (rq instanceof RankQuery) {
                RankQuery rankQuery = (RankQuery) rq;
                rb.setRankQuery(rankQuery);
                MergeStrategy mergeStrategy = rankQuery.getMergeStrategy();
                if (mergeStrategy != null) {
                    rb.addMergeStrategy(mergeStrategy);
                    if (mergeStrategy.handlesMergeFields()) {
                        rb.mergeFieldHandler = mergeStrategy;
                    }
                }
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "rq parameter must be a RankQuery");
            }
        }
        rb.setSortSpec(parser.getSortSpec(true));
        rb.setQparser(parser);
        final String cursorStr = rb.req.getParams().get(CursorMarkParams.CURSOR_MARK_PARAM);
        if (null != cursorStr) {
            final CursorMark cursorMark = new CursorMark(rb.req.getSchema(), rb.getSortSpec());
            cursorMark.parseSerializedTotem(cursorStr);
            rb.setCursorMark(cursorMark);
        }
        String[] fqs = req.getParams().getParams(CommonParams.FQ);
        if (fqs != null && fqs.length != 0) {
            List<Query> filters = rb.getFilters();
            // if filters already exists, make a copy instead of modifying the original
            filters = filters == null ? new ArrayList<>(fqs.length) : new ArrayList<>(filters);
            for (String fq : fqs) {
                if (fq != null && fq.trim().length() != 0) {
                    QParser fqp = QParser.getParser(fq, req);
                    fqp.setIsFilter(true);
                    filters.add(fqp.getQuery());
                }
            }
            // if filter cache is disabled
            if (!filters.isEmpty()) {
                rb.setFilters(filters);
            }
        }
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
    }
    if (params.getBool(GroupParams.GROUP, false)) {
        prepareGrouping(rb);
    } else {
        //Validate only in case of non-grouping search.
        if (rb.getSortSpec().getCount() < 0) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'rows' parameter cannot be negative");
        }
    }
    //Input validation.
    if (rb.getSortSpec().getOffset() < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'start' parameter cannot be negative");
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) RankQuery(org.apache.solr.search.RankQuery) CursorMark(org.apache.solr.search.CursorMark) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ArrayList(java.util.ArrayList) SolrReturnFields(org.apache.solr.search.SolrReturnFields) RankQuery(org.apache.solr.search.RankQuery) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SyntaxError(org.apache.solr.search.SyntaxError) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException)

Example 3 with ReturnFields

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

the class MoreLikeThisComponent method process.

@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (params.getBool(MoreLikeThisParams.MLT, false)) {
        ReturnFields returnFields = new SolrReturnFields(rb.req);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }
        rb.setFieldFlags(flags);
        log.debug("Starting MoreLikeThis.Process.  isShard: " + params.getBool(ShardParams.IS_SHARD));
        SolrIndexSearcher searcher = rb.req.getSearcher();
        if (params.getBool(ShardParams.IS_SHARD, false)) {
            if (params.get(MoreLikeThisComponent.DIST_DOC_ID) == null) {
                if (rb.getResults().docList.size() == 0) {
                    // return empty response
                    rb.rsp.add("moreLikeThis", new NamedList<DocList>());
                    return;
                }
                MoreLikeThisHandler.MoreLikeThisHelper mlt = new MoreLikeThisHandler.MoreLikeThisHelper(params, searcher);
                NamedList<BooleanQuery> bQuery = mlt.getMoreLikeTheseQuery(rb.getResults().docList);
                NamedList<String> temp = new NamedList<>();
                Iterator<Entry<String, BooleanQuery>> idToQueryIt = bQuery.iterator();
                while (idToQueryIt.hasNext()) {
                    Entry<String, BooleanQuery> idToQuery = idToQueryIt.next();
                    String s = idToQuery.getValue().toString();
                    log.debug("MLT Query:" + s);
                    temp.add(idToQuery.getKey(), idToQuery.getValue().toString());
                }
                rb.rsp.add("moreLikeThis", temp);
            } else {
                NamedList<DocList> sim = getMoreLikeThese(rb, rb.req.getSearcher(), rb.getResults().docList, flags);
                rb.rsp.add("moreLikeThis", sim);
            }
        } else {
            // non distrib case
            NamedList<DocList> sim = getMoreLikeThese(rb, rb.req.getSearcher(), rb.getResults().docList, flags);
            rb.rsp.add("moreLikeThis", sim);
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) MoreLikeThisHandler(org.apache.solr.handler.MoreLikeThisHandler) NamedList(org.apache.solr.common.util.NamedList) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Entry(java.util.Map.Entry) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) DocList(org.apache.solr.search.DocList)

Example 4 with ReturnFields

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

the class RealTimeGetComponent method prepare.

@Override
public void prepare(ResponseBuilder rb) throws IOException {
    // Set field flags
    ReturnFields returnFields = new SolrReturnFields(rb.req);
    rb.rsp.setReturnFields(returnFields);
}
Also used : SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) SolrReturnFields(org.apache.solr.search.SolrReturnFields)

Example 5 with ReturnFields

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

the class TestSolrQueryResponse method testReturnFields.

@Test
public void testReturnFields() throws Exception {
    final SolrQueryResponse response = new SolrQueryResponse();
    final ReturnFields defaultReturnFields = new SolrReturnFields();
    assertEquals("returnFields initial value", defaultReturnFields.toString(), response.getReturnFields().toString());
    final SolrReturnFields newValue = new SolrReturnFields((random().nextBoolean() ? SolrReturnFields.SCORE : "value"), null);
    response.setReturnFields(newValue);
    assertEquals("returnFields new value", newValue.toString(), response.getReturnFields().toString());
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) SolrReturnFields(org.apache.solr.search.SolrReturnFields) Test(org.junit.Test)

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