Search in sources :

Example 1 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class QueryComponent method prepareGrouping.

protected void prepareGrouping(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    if (null != rb.getCursorMark()) {
        // grouping with a cursor - so for now we just don't allow the combination at all
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can not use Grouping with " + CursorMarkParams.CURSOR_MARK_PARAM);
    }
    SolrIndexSearcher searcher = rb.req.getSearcher();
    GroupingSpecification groupingSpec = new GroupingSpecification();
    rb.setGroupingSpec(groupingSpec);
    final SortSpec sortSpec = rb.getSortSpec();
    //TODO: move weighting of sort
    final SortSpec groupSortSpec = searcher.weightSortSpec(sortSpec, Sort.RELEVANCE);
    String withinGroupSortStr = params.get(GroupParams.GROUP_SORT);
    //TODO: move weighting of sort
    final SortSpec withinGroupSortSpec;
    if (withinGroupSortStr != null) {
        SortSpec parsedWithinGroupSortSpec = SortSpecParsing.parseSortSpec(withinGroupSortStr, req);
        withinGroupSortSpec = searcher.weightSortSpec(parsedWithinGroupSortSpec, Sort.RELEVANCE);
    } else {
        withinGroupSortSpec = new SortSpec(groupSortSpec.getSort(), groupSortSpec.getSchemaFields(), groupSortSpec.getCount(), groupSortSpec.getOffset());
    }
    withinGroupSortSpec.setOffset(params.getInt(GroupParams.GROUP_OFFSET, 0));
    withinGroupSortSpec.setCount(params.getInt(GroupParams.GROUP_LIMIT, 1));
    groupingSpec.setWithinGroupSortSpec(withinGroupSortSpec);
    groupingSpec.setGroupSortSpec(groupSortSpec);
    String formatStr = params.get(GroupParams.GROUP_FORMAT, Grouping.Format.grouped.name());
    Grouping.Format responseFormat;
    try {
        responseFormat = Grouping.Format.valueOf(formatStr);
    } catch (IllegalArgumentException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, String.format(Locale.ROOT, "Illegal %s parameter", GroupParams.GROUP_FORMAT));
    }
    groupingSpec.setResponseFormat(responseFormat);
    groupingSpec.setFields(params.getParams(GroupParams.GROUP_FIELD));
    groupingSpec.setQueries(params.getParams(GroupParams.GROUP_QUERY));
    groupingSpec.setFunctions(params.getParams(GroupParams.GROUP_FUNC));
    groupingSpec.setIncludeGroupCount(params.getBool(GroupParams.GROUP_TOTAL_COUNT, false));
    groupingSpec.setMain(params.getBool(GroupParams.GROUP_MAIN, false));
    groupingSpec.setNeedScore((rb.getFieldFlags() & SolrIndexSearcher.GET_SCORES) != 0);
    groupingSpec.setTruncateGroups(params.getBool(GroupParams.GROUP_TRUNCATE, false));
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Grouping(org.apache.solr.search.Grouping) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) GroupingSpecification(org.apache.solr.search.grouping.GroupingSpecification) SolrException(org.apache.solr.common.SolrException) SortSpec(org.apache.solr.search.SortSpec)

Example 2 with SolrParams

use of org.apache.solr.common.params.SolrParams 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 SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class SpellCheckComponent method maxResultsForSuggest.

private Integer maxResultsForSuggest(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    float maxResultsForSuggestParamValue = params.getFloat(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST, 0.0f);
    Integer maxResultsForSuggest = null;
    if (maxResultsForSuggestParamValue > 0.0f) {
        if (maxResultsForSuggestParamValue == (int) maxResultsForSuggestParamValue) {
            // If a whole number was passed in, this is a discrete number of documents
            maxResultsForSuggest = (int) maxResultsForSuggestParamValue;
        } else {
            // If a fractional value was passed in, this is the % of documents returned by the specified filter
            // If no specified filter, we use the most restrictive filter of the fq parameters
            String maxResultsFilterQueryString = params.get(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST_FQ);
            int maxResultsByFilters = Integer.MAX_VALUE;
            SolrIndexSearcher searcher = rb.req.getSearcher();
            try {
                if (maxResultsFilterQueryString != null) {
                    // Get the default Lucene query parser
                    QParser parser = QParser.getParser(maxResultsFilterQueryString, rb.req);
                    DocSet s = searcher.getDocSet(parser.getQuery());
                    maxResultsByFilters = s.size();
                } else {
                    List<Query> filters = rb.getFilters();
                    // Get the maximum possible hits within these filters (size of most restrictive filter). 
                    if (filters != null) {
                        for (Query query : filters) {
                            DocSet s = searcher.getDocSet(query);
                            if (s != null) {
                                maxResultsByFilters = Math.min(s.size(), maxResultsByFilters);
                            }
                        }
                    }
                }
            } catch (IOException e) {
                LOG.error(e.toString());
                return null;
            } catch (SyntaxError e) {
                LOG.error(e.toString());
                return null;
            }
            // Recalculate maxResultsForSuggest if filters were specified
            if (maxResultsByFilters != Integer.MAX_VALUE) {
                maxResultsForSuggest = Math.round(maxResultsByFilters * maxResultsForSuggestParamValue);
            }
        }
    }
    return maxResultsForSuggest;
}
Also used : Query(org.apache.lucene.search.Query) 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) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IOException(java.io.IOException) DocSet(org.apache.solr.search.DocSet)

Example 4 with SolrParams

use of org.apache.solr.common.params.SolrParams in project lucene-solr by apache.

the class HttpShardHandlerFactory method getReplicaListTransformer.

protected ReplicaListTransformer getReplicaListTransformer(final SolrQueryRequest req) {
    final SolrParams params = req.getParams();
    if (params.getBool(CommonParams.PREFER_LOCAL_SHARDS, false)) {
        final CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
        final ZkController zkController = req.getCore().getCoreContainer().getZkController();
        final String preferredHostAddress = (zkController != null) ? zkController.getBaseUrl() : null;
        if (preferredHostAddress == null) {
            log.warn("Couldn't determine current host address to prefer local shards");
        } else {
            return new ShufflingReplicaListTransformer(r) {

                @Override
                public void transform(List<?> choices) {
                    if (choices.size() > 1) {
                        super.transform(choices);
                        if (log.isDebugEnabled()) {
                            log.debug("Trying to prefer local shard on {} among the choices: {}", preferredHostAddress, Arrays.toString(choices.toArray()));
                        }
                        choices.sort(new IsOnPreferredHostComparator(preferredHostAddress));
                        if (log.isDebugEnabled()) {
                            log.debug("Applied local shard preference for choices: {}", Arrays.toString(choices.toArray()));
                        }
                    }
                }
            };
        }
    }
    return shufflingReplicaListTransformer;
}
Also used : CoreDescriptor(org.apache.solr.core.CoreDescriptor) ZkController(org.apache.solr.cloud.ZkController) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List)

Example 5 with SolrParams

use of org.apache.solr.common.params.SolrParams 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)

Aggregations

SolrParams (org.apache.solr.common.params.SolrParams)310 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)179 SolrException (org.apache.solr.common.SolrException)78 Test (org.junit.Test)45 Tuple (org.apache.solr.client.solrj.io.Tuple)43 SolrDocument (org.apache.solr.common.SolrDocument)42 ArrayList (java.util.ArrayList)41 NamedList (org.apache.solr.common.util.NamedList)40 MapSolrParams (org.apache.solr.common.params.MapSolrParams)37 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)37 IOException (java.io.IOException)35 SolrDocumentList (org.apache.solr.common.SolrDocumentList)34 HashMap (java.util.HashMap)33 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)30 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)27 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)26 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)24 Map (java.util.Map)22 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)22 SolrCore (org.apache.solr.core.SolrCore)20