Example usage for org.apache.solr.handler.component ResponseBuilder getNumberDocumentsFound

List of usage examples for org.apache.solr.handler.component ResponseBuilder getNumberDocumentsFound

Introduction

In this page you can find the example usage for org.apache.solr.handler.component ResponseBuilder getNumberDocumentsFound.

Prototype

public long getNumberDocumentsFound() 

Source Link

Usage

From source file:com.doculibre.constellio.solr.handler.component.SearchLogComponent.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    // Date startTime =new Date();

    String event = params.get("event");
    String ids = params.get("ids");
    String shardUrl = params.get("shard.url");

    // filter the query warming clause and di request
    if (!((event != null && event.equals("firstSearcher")) || (ids != null))) {
        String[] shardUrlStrs = shardUrl.split("\\\\|")[0].split("/");
        String collectionName = shardUrlStrs[shardUrlStrs.length - 1];
        String queryText = rb.getQueryString();
        String queryTextAnalyzed = queryText;
        // String queryTextAnalyzed =
        // AnalyzerUtils.analyze(queryText,collection);
        int searchPage = params.getInt("page", 0);
        String simpleSearchStr = getSimpleSearchStr(params);
        String simpleSearchId = getSimpleSearchId(simpleSearchStr);
        String searchLogDocId = generateSearchLogDocId(simpleSearchId);

        String simpleSearchQueryAnalyzedStr = params.toString();
        // String simpleSearchQueryAnalyzedStr
        // =getSimpleSearchStr(simpleSearch, true);
        long timeCost = rb.rsp.getEndTime() - rb.req.getStartTime();

        SolrInputDocument doc = new SolrInputDocument();
        doc.setField(StatsConstants.INDEX_FIELD_ID, searchLogDocId);
        doc.setField(StatsConstants.INDEX_FIELD_COLLECTION_NAME, collectionName);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_ID, simpleSearchId);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH, simpleSearchStr);
        doc.setField(StatsConstants.INDEX_FIELD_SIMPLE_SEARCH_QUERY_ANALYZED, simpleSearchQueryAnalyzedStr);
        doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT, queryText);
        doc.setField(StatsConstants.INDEX_FIELD_QUERY_TEXT_ANALYZED, queryTextAnalyzed);
        doc.setField(StatsConstants.INDEX_FIELD_NUM_FOUND, rb.getNumberDocumentsFound());
        doc.setField(StatsConstants.INDEX_FIELD_RESPONSE_TIME, timeCost);
        doc.setField(StatsConstants.INDEX_FIELD_SEARCH_DATE, new Date());
        doc.setField(StatsConstants.INDEX_FIELD_SEARCH_PAGE, searchPage);

        try {//from   ww  w  .j  a  va  2  s.c  o  m
            //            searchLogCache.add(doc);
            //            if (searchLogCache.size() >= commitThreshold) {
            int port = Integer.parseInt(shardUrl.substring(shardUrl.indexOf(":") + 1, shardUrl.indexOf("/")));
            if (searchLogServer == null || localPort != port) {
                localPort = port;
                searchLogServer = new HttpSolrServer(
                        "http://localhost:" + localPort + "/solr/" + searchLogCoreName);
            }
            //               searchLogServer.add(searchLogCache);
            searchLogServer.add(doc);
            searchLogServer.commit();
            //               searchLogCache.clear();
            //            }
        } catch (SolrServerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // System.out.println("premier phase:"+timeCost);
    }
    // Date endTime =new Date();
    // System.out.println("total time:"+(endTime.getTime()-startTime.getTime()));
}

From source file:org.dice.solrenhancements.spellchecker.DiceSpellCheckComponent.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void process(ResponseBuilder rb) throws IOException {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false) || spellCheckers.isEmpty()) {
        return;/* ww w. j  a va  2 s.co m*/
    }
    boolean shardRequest = "true".equals(params.get(ShardParams.IS_SHARD));
    String q = params.get(SPELLCHECK_Q);
    SolrSpellChecker spellChecker = getSpellChecker(params);
    Collection<Token> tokens = null;

    if (q == null) {
        // enforce useage of the spellcheck.q parameter - i.e. a query we can tokenize with a regular tokenizer and not
        // a solr query for the spell checking. Useage of the SolrQueryConverter is buggy and breaks frequently
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "The spellcheck.q parameter is required.");
    } else {
        //we have a spell check param, tokenize it with the query analyzer applicable for this spellchecker
        tokens = getTokens(q, spellChecker.getQueryAnalyzer());
    }
    if (tokens != null && tokens.isEmpty() == false) {
        if (spellChecker != null) {
            int count = params.getInt(SPELLCHECK_COUNT, 1);
            boolean onlyMorePopular = params.getBool(SPELLCHECK_ONLY_MORE_POPULAR, DEFAULT_ONLY_MORE_POPULAR);
            boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
            boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
            float accuracy = params.getFloat(SPELLCHECK_ACCURACY, Float.MIN_VALUE);
            Integer alternativeTermCount = params.getInt(SpellingParams.SPELLCHECK_ALTERNATIVE_TERM_COUNT);
            Integer maxResultsForSuggest = params.getInt(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST);
            ModifiableSolrParams customParams = new ModifiableSolrParams();
            for (String checkerName : getDictionaryNames(params)) {
                customParams.add(getCustomParams(checkerName, params));
            }

            Integer hitsInteger = (Integer) rb.rsp.getToLog().get("hits");
            long hits = 0;
            if (hitsInteger == null) {
                hits = rb.getNumberDocumentsFound();
            } else {
                hits = hitsInteger.longValue();
            }
            SpellingResult spellingResult = null;
            if (maxResultsForSuggest == null || hits <= maxResultsForSuggest) {
                SuggestMode suggestMode = SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX;
                if (onlyMorePopular) {
                    suggestMode = SuggestMode.SUGGEST_MORE_POPULAR;
                } else if (alternativeTermCount != null) {
                    suggestMode = SuggestMode.SUGGEST_ALWAYS;
                }

                IndexReader reader = rb.req.getSearcher().getIndexReader();
                SpellingOptions options = new SpellingOptions(tokens, reader, count, alternativeTermCount,
                        suggestMode, extendedResults, accuracy, customParams);
                spellingResult = spellChecker.getSuggestions(options);
            } else {
                spellingResult = new SpellingResult();
            }
            boolean isCorrectlySpelled = hits > (maxResultsForSuggest == null ? 0 : maxResultsForSuggest);
            NamedList suggestions = toNamedList(shardRequest, spellingResult, q, extendedResults, collate,
                    isCorrectlySpelled);
            if (collate) {
                ModifiableSolrParams modParams = new ModifiableSolrParams(params);
                // SH: having both spellcheck.q and q set screws up collations for some queries, such as "java develope"
                modParams.remove(CommonParams.Q);

                //SH: Note that the collator runs a query against the DF specified field. Ideally it should
                //run the query against the spellchecker field but that's inaccessible here
                addCollationsToResponse(modParams, spellingResult, rb, q, suggestions,
                        spellChecker.isSuggestionsMayOverlap());
            }
            NamedList response = new SimpleOrderedMap();
            response.add("suggestions", suggestions);
            rb.rsp.add("spellcheck", response);

        } else {
            throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Specified dictionaries do not exist: "
                    + getDictionaryNameAsSingleString(getDictionaryNames(params)));
        }
    }
}

From source file:org.dice.solrenhancements.spellchecker.DiceSpellCheckComponent.java

License:Apache License

@Override
@SuppressWarnings({ "unchecked", "deprecation" })
public void finishStage(ResponseBuilder rb) {
    SolrParams params = rb.req.getParams();
    if (!params.getBool(COMPONENT_NAME, false) || rb.stage != ResponseBuilder.STAGE_GET_FIELDS)
        return;/*from  w w w. java2 s  .com*/

    boolean extendedResults = params.getBool(SPELLCHECK_EXTENDED_RESULTS, false);
    boolean collate = params.getBool(SPELLCHECK_COLLATE, false);
    boolean collationExtendedResults = params.getBool(SPELLCHECK_COLLATE_EXTENDED_RESULTS, false);
    int maxCollationTries = params.getInt(SPELLCHECK_MAX_COLLATION_TRIES, 0);
    int maxCollations = params.getInt(SPELLCHECK_MAX_COLLATIONS, 1);
    Integer maxResultsForSuggest = params.getInt(SpellingParams.SPELLCHECK_MAX_RESULTS_FOR_SUGGEST);
    int count = rb.req.getParams().getInt(SPELLCHECK_COUNT, 1);
    int numSug = Math.max(count, AbstractLuceneSpellChecker.DEFAULT_SUGGESTION_COUNT);

    String origQuery = params.get(SPELLCHECK_Q);
    if (origQuery == null) {
        origQuery = rb.getQueryString();
        if (origQuery == null) {
            origQuery = params.get(CommonParams.Q);
        }
    }

    long hits = rb.grouping() ? rb.totalHitCount : rb.getNumberDocumentsFound();
    boolean isCorrectlySpelled = hits > (maxResultsForSuggest == null ? 0 : maxResultsForSuggest);

    SpellCheckMergeData mergeData = new SpellCheckMergeData();
    if (maxResultsForSuggest == null || !isCorrectlySpelled) {
        for (ShardRequest sreq : rb.finished) {
            for (ShardResponse srsp : sreq.responses) {
                NamedList nl = (NamedList) srsp.getSolrResponse().getResponse().get("spellcheck");
                LOG.info(srsp.getShard() + " " + nl);
                if (nl != null) {
                    mergeData.totalNumberShardResponses++;
                    collectShardSuggestions(nl, mergeData);
                    collectShardCollations(mergeData, nl, maxCollationTries);
                }
            }
        }
    }

    // all shard responses have been collected
    // create token and get top suggestions
    SolrSpellChecker checker = getSpellChecker(rb.req.getParams());
    SpellingResult result = checker.mergeSuggestions(mergeData, numSug, count, extendedResults);

    NamedList response = new SimpleOrderedMap();
    NamedList suggestions = toNamedList(false, result, origQuery, extendedResults, collate, isCorrectlySpelled);
    if (collate) {
        SpellCheckCollation[] sortedCollations = mergeData.collations.values()
                .toArray(new SpellCheckCollation[mergeData.collations.size()]);
        Arrays.sort(sortedCollations);
        int i = 0;
        while (i < maxCollations && i < sortedCollations.length) {
            SpellCheckCollation collation = sortedCollations[i];
            i++;
            if (collationExtendedResults) {
                NamedList extendedResult = new NamedList();
                extendedResult.add("collationQuery", collation.getCollationQuery());
                extendedResult.add("hits", collation.getHits());
                extendedResult.add("misspellingsAndCorrections", collation.getMisspellingsAndCorrections());
                suggestions.add("collation", extendedResult);
            } else {
                suggestions.add("collation", collation.getCollationQuery());
            }
        }
    }

    response.add("suggestions", suggestions);
    rb.rsp.add("spellcheck", response);
}