Example usage for org.apache.solr.util DateMathParser parseMath

List of usage examples for org.apache.solr.util DateMathParser parseMath

Introduction

In this page you can find the example usage for org.apache.solr.util DateMathParser parseMath.

Prototype


public static Date parseMath(Date now, String val) 

Source Link

Document

Parses a String which may be a date (in the standard ISO-8601 format) followed by an optional math expression.

Usage

From source file:edu.harvard.gis.hhypermap.bop.solrplugins.DateShardRoutingSearchHandler.java

License:Apache License

private void addShardsParamIfWeCan(SolrQueryRequest req) {
    CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
    CoreContainer coreContainer = coreDescriptor.getCoreContainer();
    if (!coreContainer.isZooKeeperAware()) {
        return;//w  ww .  j av  a2 s  . c o  m
    }
    if (!req.getParams().getBool("distrib", true)) {
        return;
    }
    final String shards = req.getParams().get(ShardParams.SHARDS);
    if (shards != null) {
        return; // we already have the shards
    }

    String startStrParam = req.getParams().get(START_PARAM);
    String endStrParam = req.getParams().get(END_PARAM);

    Instant startInst = // null means open-ended ('*')
            startStrParam == null ? null : DateMathParser.parseMath(null, startStrParam).toInstant();
    Instant endInst = endStrParam == null ? null : DateMathParser.parseMath(null, endStrParam).toInstant();

    if (startInst == null && endInst == null) {
        return;
    }

    ZkController zkController = coreContainer.getZkController();
    String collection = req.getParams().get("collection", coreDescriptor.getCollectionName());
    List<Slice> slices = getOrderedSlices(zkController, collection);
    if (slices.size() <= 1) {
        return;
    }

    List<String> routeShardNames = new ArrayList<>(); // the result
    boolean findingStart = (startInst != null);
    String prevShardName = null;
    Instant prevShardStartKey = null;

    for (Slice slice : slices) {
        String name = slice.getName();
        Instant shardStartKey = parseStartKeyFromShardName(name);
        if (prevShardStartKey != null && prevShardStartKey.isAfter(shardStartKey)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "shards not in order? " + slice);
        }
        if (findingStart) {
            // As we advance shards, is this one finally > the 'start'? If so, accept it.
            if (shardStartKey.isAfter(startInst)) {
                if (prevShardName != null) {
                    routeShardNames.add(prevShardName);
                }
                findingStart = false; // thus findingEnd
            }
        }
        if (!findingStart && endInst == null) { // take all the remainder since 'end' is null
            routeShardNames.add(name);
        } else if (!findingStart) { // findingEnd
            if (shardStartKey.isAfter(endInst)) {
                break;
            }
            routeShardNames.add(name);
        }

        prevShardName = name;
        prevShardStartKey = shardStartKey;
    }
    if (findingStart) {
        routeShardNames.add(prevShardName);
    }

    if (routeShardNames.size() == slices.size()) {
        return;
    }

    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
    String shardsValue = StrUtils.join(routeShardNames, ',');
    params.set(ShardParams.SHARDS, shardsValue);
    req.setParams(params);
    log.debug("Set shards: {}", shardsValue);
}