Example usage for org.apache.solr.common.params ShardParams IS_SHARD

List of usage examples for org.apache.solr.common.params ShardParams IS_SHARD

Introduction

In this page you can find the example usage for org.apache.solr.common.params ShardParams IS_SHARD.

Prototype

String IS_SHARD

To view the source code for org.apache.solr.common.params ShardParams IS_SHARD.

Click Source Link

Document

whether the request goes to a shard

Usage

From source file:org.alfresco.solr.component.ConsistencyComponent.java

License:Open Source License

@Override
public void process(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;/* w ww . j a  v  a 2s  .  co m*/
    AlfrescoCoreAdminHandler adminHandler = (AlfrescoCoreAdminHandler) req.getCore().getCoreContainer()
            .getMultiCoreHandler();

    boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);
    MetadataTracker metaTrkr = adminHandler.getTrackerRegistry().getTrackerForCore(req.getCore().getName(),
            MetadataTracker.class);
    if (metaTrkr != null && !isShard) {
        TrackerState metadataTrkrState = metaTrkr.getTrackerState();

        long lastIndexedTx = metadataTrkrState.getLastIndexedTxId();
        long lastIndexTxCommitTime = metadataTrkrState.getLastIndexedTxCommitTime();
        long lastTxIdOnServer = metadataTrkrState.getLastTxIdOnServer();
        long transactionsToDo = lastTxIdOnServer - lastIndexedTx;
        if (transactionsToDo < 0) {
            transactionsToDo = 0;
        }
        rb.rsp.add("lastIndexedTx", lastIndexedTx);
        rb.rsp.add("lastIndexedTxTime", lastIndexTxCommitTime);
        rb.rsp.add("txRemaining", transactionsToDo);
    }
}

From source file:org.alfresco.solr.component.QueryLoggingComponent.java

License:Open Source License

private void log(ResponseBuilder rb) throws IOException {
    boolean isShard = rb.req.getParams().getBool(ShardParams.IS_SHARD, false);
    if (!isShard) {
        CoreContainer container = rb.req.getCore().getCoreContainer();
        SolrCore logCore = container.getCore(rb.req.getCore().getName() + "_qlog");
        if (logCore != null) {
            JSONObject json = (JSONObject) rb.req.getContext().get(AbstractQParser.ALFRESCO_JSON);

            SolrQueryRequest request = null;
            UpdateRequestProcessor processor = null;
            try {
                request = new LocalSolrQueryRequest(logCore, new NamedList<>());
                processor = logCore.getUpdateProcessingChain(null).createProcessor(request,
                        new SolrQueryResponse());

                AddUpdateCommand cmd = new AddUpdateCommand(request);
                cmd.overwrite = true;//from w  w w.ja v a2 s.  com
                SolrInputDocument input = new SolrInputDocument();
                input.addField("id", GUID.generate());
                input.addField("_version_", "1");

                input.addField("timestamp", DateTimeFormatter.ISO_INSTANT.format(Instant.now()));

                if (json != null) {
                    try {
                        ArrayList<String> authorityList = new ArrayList<String>(1);
                        JSONArray authorities = json.getJSONArray("authorities");
                        for (int i = 0; i < authorities.length(); i++) {
                            String authorityString = authorities.getString(i);
                            authorityList.add(authorityString);
                        }

                        for (String authority : authorityList) {
                            if (AuthorityType.getAuthorityType(authority) == AuthorityType.USER) {
                                input.addField("user", authority);
                                break;
                            }
                        }
                    } catch (JSONException e) {
                        input.addField("user", "<UNKNOWN>");
                    }
                } else {
                    input.addField("user", "<UNKNOWN>");
                }

                String userQuery = rb.req.getParams().get(SpellingParams.SPELLCHECK_Q);
                if (userQuery == null) {
                    if (json != null) {
                        try {
                            userQuery = json.getString("query");
                        } catch (JSONException e) {
                        }
                    }
                }
                if (userQuery == null) {
                    userQuery = rb.req.getParams().get(CommonParams.Q);
                }

                if (userQuery != null) {
                    input.addField("user_query", userQuery);
                }

                Query query = rb.getQuery();
                input.addField("query", query.toString());

                if (rb.getResults().docList != null) {
                    input.addField("found", rb.getResults().docList.matches());
                }
                input.addField("time", rb.req.getRequestTimer().getTime());

                cmd.solrDoc = input;
                processor.processAdd(cmd);
            }

            finally {
                if (processor != null) {
                    processor.finish();
                }
                if (request != null) {
                    request.close();
                }
            }
        }
    }
}

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;//from  ww w . j ava 2 s. c  om
    }
    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

@SuppressWarnings("unchecked")
protected void addCollationsToResponse(SolrParams params, SpellingResult spellingResult, ResponseBuilder rb,
        String q, NamedList response, boolean suggestionsMayOverlap) {
    int maxCollations = params.getInt(SPELLCHECK_MAX_COLLATIONS, 1);
    int maxCollationTries = params.getInt(SPELLCHECK_MAX_COLLATION_TRIES, 0);
    int maxCollationEvaluations = params.getInt(SPELLCHECK_MAX_COLLATION_EVALUATIONS, 10000);
    boolean collationExtendedResults = params.getBool(SPELLCHECK_COLLATE_EXTENDED_RESULTS, false);
    int maxCollationCollectDocs = params.getInt(SPELLCHECK_COLLATE_MAX_COLLECT_DOCS, 0);
    // If not reporting hits counts, don't bother collecting more than 1 document per try.
    if (!collationExtendedResults) {
        maxCollationCollectDocs = 1;/*from  w w  w  . ja  v  a  2  s  . com*/
    }
    boolean shard = params.getBool(ShardParams.IS_SHARD, false);
    SpellCheckCollator collator = new SpellCheckCollator().setMaxCollations(maxCollations)
            .setMaxCollationTries(maxCollationTries).setMaxCollationEvaluations(maxCollationEvaluations)
            .setSuggestionsMayOverlap(suggestionsMayOverlap).setDocCollectionLimit(maxCollationCollectDocs);
    List<SpellCheckCollation> collations = collator.collate(spellingResult, q, rb);
    //by sorting here we guarantee a non-distributed request returns all
    //results in the same order as a distributed request would,
    //even in cases when the internal rank is the same.
    Collections.sort(collations);

    for (SpellCheckCollation collation : collations) {
        if (collationExtendedResults) {
            NamedList extendedResult = new NamedList();
            extendedResult.add("collationQuery", collation.getCollationQuery());
            extendedResult.add("hits", collation.getHits());
            extendedResult.add("misspellingsAndCorrections", collation.getMisspellingsAndCorrections());
            if (maxCollationTries > 0 && shard) {
                extendedResult.add("collationInternalRank", collation.getInternalRank());
            }
            response.add("collation", extendedResult);
        } else {
            response.add("collation", collation.getCollationQuery());
            if (maxCollationTries > 0 && shard) {
                response.add("collationInternalRank", collation.getInternalRank());
            }
        }
    }
}

From source file:org.solbase.SolbaseComponent.java

License:Apache License

public void prepare(ResponseBuilder rb) throws IOException {

    // Only applies to my lucandra index readers
    if (rb.req.getSearcher().getIndexReader().getVersion() != Long.MAX_VALUE)
        return;// www  . j a  v  a  2 s .c o  m

    if (!hasSolbaseSchema.get()) {
        hasSolbaseSchema.set(true);
    }

    // If this is a shard request then no need to do anything
    if (rb.req.getParams().getBool(ShardParams.IS_SHARD, false)) {
        String indexName = (String) rb.req.getContext().get("solbase-index");

        if (indexName == null)
            throw new IOException("Missing core name");

        logger.debug(indexName);
        IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader())
                .getWrappedReader();

        reader.setIndexName(indexName);

        return;
    }

    String indexName = rb.req.getCore().getName();

    if (indexName.equals("")) {
        return; //
    } else {
        logger.debug("core: " + indexName);
    }

    if (rb.shards == null) {
        // find number of shards
        // this is current max doc id
        // for real time, we'd have to fetch max doc id from table. maybe put some caching around this number
        //int docId = SolbaseUtil.getSequenceId();

        int numShards = SolbaseShardUtil.getNumShard();

        //run local
        if (numShards == 0) {
            IndexReader reader = (IndexReader) ((SolrIndexReader) rb.req.getSearcher().getIndexReader())
                    .getWrappedReader();

            String subIndex = indexName;
            reader.setIndexName(subIndex);

            return;
        }

        String[] shards = new String[numShards];
        // assign shards
        List<String> hosts = SolbaseShardUtil.getShardHosts();
        for (int i = 0; i < hosts.size(); i++) {
            String host = hosts.get(i);
            String shard = host + "/solbase/" + indexName + "~" + i;
            if (logger.isDebugEnabled())
                logger.debug("Adding shard(" + indexName + "): " + shard);

            shards[i] = shard;
        }

        // assign to shards
        rb.shards = shards;

        return;

    }
}