Example usage for org.apache.solr.common.util StrUtils splitSmart

List of usage examples for org.apache.solr.common.util StrUtils splitSmart

Introduction

In this page you can find the example usage for org.apache.solr.common.util StrUtils splitSmart.

Prototype

public static List<String> splitSmart(String s, String separator, boolean decode) 

Source Link

Document

Splits a backslash escaped string on the separator.

Usage

From source file:com.cloudera.cdk.morphline.solr.ZooKeeperDownloader.java

License:Apache License

/**
 * Returns config value given collection name
 * Borrowed heavily from Solr's ZKController.
 *///from   w  w  w .jav  a2s.c om
public String readConfigName(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    if (collection == null) {
        throw new IllegalArgumentException("collection must not be null");
    }
    String configName = null;

    // first check for alias
    byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
    Aliases aliases = ClusterState.load(aliasData);
    String alias = aliases.getCollectionAlias(collection);
    if (alias != null) {
        List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
        if (aliasList.size() > 1) {
            throw new IllegalArgumentException(
                    "collection cannot be an alias that maps to multiple collections");
        }
        collection = aliasList.get(0);
    }

    String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
    if (LOG.isInfoEnabled()) {
        LOG.info("Load collection config from:" + path);
    }
    byte[] data = zkClient.getData(path, null, null, true);

    if (data != null) {
        ZkNodeProps props = ZkNodeProps.load(data);
        configName = props.getStr(ZkController.CONFIGNAME_PROP);
    }

    if (configName != null && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
        LOG.error("Specified config does not exist in ZooKeeper:" + configName);
        throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:" + configName);
    }

    return configName;
}

From source file:lux.solr.XQueryComponent.java

License:Mozilla Public License

public ArrayList<String> getShardURLs(boolean includeSelf) {
    // String[] urls = new String[shards.length + (includeSelf ? 0 : -1)];
    ArrayList<String> urls = new ArrayList<String>();
    String shardId = core.getCoreDescriptor().getCloudDescriptor().getShardId();
    for (int i = 0; i < shards.length; i++) {
        if (!includeSelf) {
            if (shardId.equals(slices[i])) {
                // exclude this shard
                continue;
            }/*from   w  w w .  j  a  v  a 2 s  .com*/
        }
        List<String> replicas = StrUtils.splitSmart(shards[i], "|", true);
        for (String replica : replicas) {
            urls.add("http://" + replica);
        }
    }
    return urls;
}

From source file:org.jahia.services.search.facets.SimpleJahiaJcrFacets.java

License:Open Source License

private NamedList<Object> getListedTermCounts(String field, ExtendedPropertyDefinition epd,
        String fieldNameInIndex, String locale, String termList) throws IOException {
    FieldType ft = getType(epd);/* w  ww. j  a v  a 2 s. co m*/
    List<String> terms = StrUtils.splitSmart(termList, ",", true);
    NamedList<Object> res = new NamedList<Object>();
    Term t = new Term(field);
    for (String term : terms) {
        String internal = ft.toInternal(term);
        int count = (int) OpenBitSet.intersectionCount(getDocIdSet(new TermQuery(t.createTerm(internal)), ""),
                base);
        res.add(term, count);
    }
    return res;
}

From source file:org.vootoo.server.RequestProcesser.java

License:Apache License

public void handleRequest(RequestGetter requestGetter) {
    MDCLoggingContext.reset();/*from   ww w .  j  av a 2s.  c  o  m*/
    MDCLoggingContext.setNode(cores);

    String path = requestGetter.getPath();
    solrParams = requestGetter.getSolrParams();
    SolrRequestHandler handler = null;
    String corename = "";
    String origCorename = null;
    try {
        // set a request timer which can be reused by requests if needed
        //req.setAttribute(SolrRequestParsers.REQUEST_TIMER_SERVLET_ATTRIBUTE, new RTimer());
        // put the core container in request attribute
        //req.setAttribute("org.apache.solr.CoreContainer", cores);
        // check for management path
        String alternate = cores.getManagementPath();
        if (alternate != null && path.startsWith(alternate)) {
            path = path.substring(0, alternate.length());
        }
        // unused feature ?
        int idx = path.indexOf(':');
        if (idx > 0) {
            // save the portion after the ':' for a 'handler' path parameter
            path = path.substring(0, idx);
        }

        boolean usingAliases = false;
        List<String> collectionsList = null;

        // Check for container handlers
        handler = cores.getRequestHandler(path);
        if (handler != null) {
            solrReq = parseSolrQueryRequest(SolrRequestParsers.DEFAULT, requestGetter);
            handleAdminRequest(handler, solrReq);
            return;
        } else {
            //otherwise, we should find a core from the path
            idx = path.indexOf("/", 1);
            if (idx > 1) {
                // try to get the corename as a request parameter first
                corename = path.substring(1, idx);

                // look at aliases
                if (cores.isZooKeeperAware()) {
                    origCorename = corename;
                    ZkStateReader reader = cores.getZkController().getZkStateReader();
                    aliases = reader.getAliases();
                    if (aliases != null && aliases.collectionAliasSize() > 0) {
                        usingAliases = true;
                        String alias = aliases.getCollectionAlias(corename);
                        if (alias != null) {
                            collectionsList = StrUtils.splitSmart(alias, ",", true);
                            corename = collectionsList.get(0);
                        }
                    }
                }

                core = cores.getCore(corename);

                if (core != null) {
                    path = path.substring(idx);
                }
            }

            //add collection name
            if (core == null && StringUtils.isNotBlank(requestGetter.getCollection())) {
                corename = requestGetter.getCollection();
                core = cores.getCore(corename);
            }

            if (core == null) {
                if (!cores.isZooKeeperAware()) {
                    core = cores.getCore("");
                }
            }
        }

        if (core == null && cores.isZooKeeperAware()) {
            // we couldn't find the core - lets make sure a collection was not specified instead
            core = getCoreByCollection(cores, corename);

            if (core != null) {
                // we found a core, update the path
                path = path.substring(idx);
            }

            // try the default core
            if (core == null) {
                core = cores.getCore("");
                if (core != null) {
                }
            }
        }

        // With a valid core...
        if (core != null) {
            MDCLoggingContext.setCore(core);
            final SolrConfig config = core.getSolrConfig();
            // get or create/cache the parser for the core
            SolrRequestParsers parser = config.getRequestParsers();

            // Determine the handler from the url path if not set
            // (we might already have selected the cores handler)
            if (handler == null && path.length() > 1) { // don't match "" or "/" as valid path
                handler = core.getRequestHandler(path);

                if (handler == null) {
                    //may be a restlet path
                    // Handle /schema/* paths via Restlet
                    if (path.equals("/schema") || path.startsWith("/schema/")) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                "unsupport /schema/**, use http solr");
                    }

                }
                // no handler yet but allowed to handle select; let's check
                if (handler == null && parser.isHandleSelect()) {
                    if ("/select".equals(path) || "/select/".equals(path)) {
                        solrReq = parseSolrQueryRequest(parser, requestGetter);

                        invalidStates = checkStateIsValid(cores,
                                solrReq.getParams().get(CloudSolrClient.STATE_VERSION));
                        String qt = solrReq.getParams().get(CommonParams.QT);
                        handler = core.getRequestHandler(qt);
                        if (handler == null) {
                            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                    "unknown handler: " + qt);
                        }
                        if (qt != null && qt.startsWith("/") && (handler instanceof ContentStreamHandlerBase)) {
                            //For security reasons it's a bad idea to allow a leading '/', ex: /select?qt=/update see SOLR-3161
                            //There was no restriction from Solr 1.4 thru 3.5 and it's not supported for update handlers.
                            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                                    "Invalid Request Handler ('qt').  Do not use /select to access: " + qt);
                        }
                    }
                }
            }

            // With a valid handler and a valid core...
            if (handler != null) {
                // if not a /select, create the request
                if (solrReq == null) {
                    solrReq = parseSolrQueryRequest(parser, requestGetter);
                }

                if (usingAliases) {
                    processAliases(solrReq, aliases, collectionsList);
                }

                SolrQueryResponse solrRsp = new SolrQueryResponse();
                SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, solrRsp));
                this.execute(handler, solrReq, solrRsp);
                QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
                if (invalidStates != null)
                    solrReq.getContext().put(CloudSolrClient.STATE_VERSION, invalidStates);
                writeResponse(solrRsp, responseWriter, solrReq);

                return; // we are done with a valid handler
            }
        }
        logger.debug("no handler or core retrieved for {}, follow through...", path);
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
                "no handler or core retrieved for " + path);
    } catch (Throwable ex) {
        sendError(core, solrReq, ex);
        // walk the the entire cause chain to search for an Error
        Throwable t = ex;
        while (t != null) {
            if (t instanceof Error) {
                if (t != ex) {
                    logger.error(
                            "An Error was wrapped in another exception - please report complete stacktrace on SOLR-6161",
                            ex);
                }
                throw (Error) t;
            }
            t = t.getCause();
        }
        return;
    } finally {
        try {
            if (solrReq != null) {
                logger.debug("Closing out SolrRequest: {}", solrReq);
                solrReq.close();
            }
        } finally {
            try {
                if (core != null) {
                    core.close();
                }
            } finally {
                SolrRequestInfo.clearRequestInfo();
            }

        }
        MDCLoggingContext.clear();
    }
}

From source file:org.vootoo.server.Vootoo.java

License:Apache License

public static void processAliases(SolrQueryRequest solrReq, Aliases aliases, List<String> collectionsList) {
    String collection = solrReq.getParams().get("collection");
    if (collection != null) {
        collectionsList = StrUtils.splitSmart(collection, ",", true);
    }//from w  w  w.j a v  a 2 s.c o m
    if (collectionsList != null) {
        Set<String> newCollectionsList = new HashSet<>(collectionsList.size());
        for (String col : collectionsList) {
            String al = aliases.getCollectionAlias(col);
            if (al != null) {
                List<String> aliasList = StrUtils.splitSmart(al, ",", true);
                newCollectionsList.addAll(aliasList);
            } else {
                newCollectionsList.add(col);
            }
        }
        if (newCollectionsList.size() > 0) {
            StringBuilder collectionString = new StringBuilder();
            Iterator<String> it = newCollectionsList.iterator();
            int sz = newCollectionsList.size();
            for (int i = 0; i < sz; i++) {
                collectionString.append(it.next());
                if (i < newCollectionsList.size() - 1) {
                    collectionString.append(",");
                }
            }
            ModifiableSolrParams params = new ModifiableSolrParams(solrReq.getParams());
            params.set("collection", collectionString.toString());
            solrReq.setParams(params);
        }
    }
}

From source file:uk.bl.wa.apache.solr.hadoop.ZooKeeperInspector.java

License:Apache License

private String checkForAlias(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
    Aliases aliases = ClusterState.load(aliasData);
    String alias = aliases.getCollectionAlias(collection);
    if (alias != null) {
        List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
        if (aliasList.size() > 1) {
            throw new IllegalArgumentException(
                    "collection cannot be an alias that maps to multiple collections");
        }/*from   w w  w  . ja  va2s. c  om*/
        collection = aliasList.get(0);
    }
    return collection;
}