Search in sources :

Example 1 with FieldComparator

use of org.apache.solr.client.solrj.io.comp.FieldComparator in project lucene-solr by apache.

the class FacetStream method open.

public void open() throws IOException {
    if (cache != null) {
        cloudSolrClient = cache.getCloudSolrClient(zkHost);
    } else {
        cloudSolrClient = new Builder().withZkHost(zkHost).build();
    }
    FieldComparator[] adjustedSorts = adjustSorts(buckets, bucketSorts);
    String json = getJsonFacetString(buckets, metrics, adjustedSorts, bucketSizeLimit);
    ModifiableSolrParams paramsLoc = new ModifiableSolrParams(params);
    paramsLoc.set("json.facet", json);
    paramsLoc.set("rows", "0");
    QueryRequest request = new QueryRequest(paramsLoc);
    try {
        NamedList response = cloudSolrClient.request(request, collection);
        getTuples(response, buckets, metrics);
        Collections.sort(tuples, getStreamSort());
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) Builder(org.apache.solr.client.solrj.impl.CloudSolrClient.Builder) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) IOException(java.io.IOException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException)

Example 2 with FieldComparator

use of org.apache.solr.client.solrj.io.comp.FieldComparator in project lucene-solr by apache.

the class FacetStream method init.

private void init(String collection, SolrParams params, Bucket[] buckets, FieldComparator[] bucketSorts, Metric[] metrics, int bucketSizeLimit, String zkHost) throws IOException {
    this.zkHost = zkHost;
    this.params = params;
    this.buckets = buckets;
    this.metrics = metrics;
    this.bucketSizeLimit = bucketSizeLimit;
    this.collection = collection;
    this.bucketSorts = bucketSorts;
    // that they are the same
    for (FieldComparator sort : bucketSorts) {
        if (sort.hasDifferentFieldNames()) {
            throw new IOException("Invalid FacetStream - all sorts must be constructed with a single field name.");
        }
    }
}
Also used : FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) IOException(java.io.IOException)

Example 3 with FieldComparator

use of org.apache.solr.client.solrj.io.comp.FieldComparator in project lucene-solr by apache.

the class BiJoinStream method createIterationComparator.

private StreamComparator createIterationComparator(StreamEqualitor eq, StreamComparator comp) throws IOException {
    if (eq instanceof MultipleFieldEqualitor && comp instanceof MultipleFieldComparator) {
        // we know the comp is at least as long as the eq because we've already validated the tuple order
        StreamComparator[] compoundComps = new StreamComparator[((MultipleFieldEqualitor) eq).getEqs().length];
        for (int idx = 0; idx < compoundComps.length; ++idx) {
            StreamEqualitor sourceEqualitor = ((MultipleFieldEqualitor) eq).getEqs()[idx];
            StreamComparator sourceComparator = ((MultipleFieldComparator) comp).getComps()[idx];
            if (sourceEqualitor instanceof FieldEqualitor && sourceComparator instanceof FieldComparator) {
                FieldEqualitor fieldEqualitor = (FieldEqualitor) sourceEqualitor;
                FieldComparator fieldComparator = (FieldComparator) sourceComparator;
                compoundComps[idx] = new FieldComparator(fieldEqualitor.getLeftFieldName(), fieldEqualitor.getRightFieldName(), fieldComparator.getOrder());
            } else {
                throw new IOException("Failed to create an iteration comparator");
            }
        }
        return new MultipleFieldComparator(compoundComps);
    } else if (comp instanceof MultipleFieldComparator) {
        StreamEqualitor sourceEqualitor = eq;
        StreamComparator sourceComparator = ((MultipleFieldComparator) comp).getComps()[0];
        if (sourceEqualitor instanceof FieldEqualitor && sourceComparator instanceof FieldComparator) {
            FieldEqualitor fieldEqualitor = (FieldEqualitor) sourceEqualitor;
            FieldComparator fieldComparator = (FieldComparator) sourceComparator;
            return new FieldComparator(fieldEqualitor.getLeftFieldName(), fieldEqualitor.getRightFieldName(), fieldComparator.getOrder());
        } else {
            throw new IOException("Failed to create an iteration comparator");
        }
    } else {
        StreamEqualitor sourceEqualitor = eq;
        StreamComparator sourceComparator = comp;
        if (sourceEqualitor instanceof FieldEqualitor && sourceComparator instanceof FieldComparator) {
            FieldEqualitor fieldEqualitor = (FieldEqualitor) sourceEqualitor;
            FieldComparator fieldComparator = (FieldComparator) sourceComparator;
            return new FieldComparator(fieldEqualitor.getLeftFieldName(), fieldEqualitor.getRightFieldName(), fieldComparator.getOrder());
        } else {
            throw new IOException("Failed to create an iteration comparator");
        }
    }
}
Also used : MultipleFieldEqualitor(org.apache.solr.client.solrj.io.eq.MultipleFieldEqualitor) StreamComparator(org.apache.solr.client.solrj.io.comp.StreamComparator) FieldEqualitor(org.apache.solr.client.solrj.io.eq.FieldEqualitor) MultipleFieldEqualitor(org.apache.solr.client.solrj.io.eq.MultipleFieldEqualitor) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) StreamEqualitor(org.apache.solr.client.solrj.io.eq.StreamEqualitor) IOException(java.io.IOException)

Example 4 with FieldComparator

use of org.apache.solr.client.solrj.io.comp.FieldComparator in project lucene-solr by apache.

the class CloudSolrStream method parseComp.

private StreamComparator parseComp(String sort, String fl) throws IOException {
    String[] fls = fl.split(",");
    HashSet fieldSet = new HashSet();
    for (String f : fls) {
        //Handle spaces in the field list.
        fieldSet.add(f.trim());
    }
    String[] sorts = sort.split(",");
    StreamComparator[] comps = new StreamComparator[sorts.length];
    for (int i = 0; i < sorts.length; i++) {
        String s = sorts[i];
        //This should take into account spaces in the sort spec.
        String[] spec = s.trim().split("\\s+");
        if (spec.length != 2) {
            throw new IOException("Invalid sort spec:" + s);
        }
        String fieldName = spec[0].trim();
        String order = spec[1].trim();
        if (!fieldSet.contains(spec[0])) {
            throw new IOException("Fields in the sort spec must be included in the field list:" + spec[0]);
        }
        // if there's an alias for the field then use the alias
        if (null != fieldMappings && fieldMappings.containsKey(fieldName)) {
            fieldName = fieldMappings.get(fieldName);
        }
        comps[i] = new FieldComparator(fieldName, order.equalsIgnoreCase("asc") ? ComparatorOrder.ASCENDING : ComparatorOrder.DESCENDING);
    }
    if (comps.length > 1) {
        return new MultipleFieldComparator(comps);
    } else {
        return comps[0];
    }
}
Also used : StreamComparator(org.apache.solr.client.solrj.io.comp.StreamComparator) FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 5 with FieldComparator

use of org.apache.solr.client.solrj.io.comp.FieldComparator in project lucene-solr by apache.

the class FacetStream method parseBucketSorts.

private FieldComparator[] parseBucketSorts(String bucketSortString) throws IOException {
    String[] sorts = bucketSortString.split(",");
    FieldComparator[] comps = new FieldComparator[sorts.length];
    for (int i = 0; i < sorts.length; i++) {
        String s = sorts[i];
        //This should take into account spaces in the sort spec.
        String[] spec = s.trim().split("\\s+");
        if (2 != spec.length) {
            throw new IOException(String.format(Locale.ROOT, "invalid expression - bad bucketSort '%s'. Expected form 'field order'", bucketSortString));
        }
        String fieldName = spec[0].trim();
        String order = spec[1].trim();
        comps[i] = new FieldComparator(fieldName, order.equalsIgnoreCase("asc") ? ComparatorOrder.ASCENDING : ComparatorOrder.DESCENDING);
    }
    return comps;
}
Also used : FieldComparator(org.apache.solr.client.solrj.io.comp.FieldComparator) MultipleFieldComparator(org.apache.solr.client.solrj.io.comp.MultipleFieldComparator) IOException(java.io.IOException)

Aggregations

FieldComparator (org.apache.solr.client.solrj.io.comp.FieldComparator)39 MultipleFieldComparator (org.apache.solr.client.solrj.io.comp.MultipleFieldComparator)30 Tuple (org.apache.solr.client.solrj.io.Tuple)23 Test (org.junit.Test)23 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)21 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)21 SolrClientCache (org.apache.solr.client.solrj.io.SolrClientCache)20 SolrParams (org.apache.solr.common.params.SolrParams)15 FieldEqualitor (org.apache.solr.client.solrj.io.eq.FieldEqualitor)8 StreamFactory (org.apache.solr.client.solrj.io.stream.expr.StreamFactory)8 IOException (java.io.IOException)7 ComparatorOrder (org.apache.solr.client.solrj.io.comp.ComparatorOrder)5 MeanMetric (org.apache.solr.client.solrj.io.stream.metrics.MeanMetric)5 MinMetric (org.apache.solr.client.solrj.io.stream.metrics.MinMetric)5 StreamComparator (org.apache.solr.client.solrj.io.comp.StreamComparator)4 MultipleFieldEqualitor (org.apache.solr.client.solrj.io.eq.MultipleFieldEqualitor)4 GroupOperation (org.apache.solr.client.solrj.io.ops.GroupOperation)4 StreamExpression (org.apache.solr.client.solrj.io.stream.expr.StreamExpression)4 Bucket (org.apache.solr.client.solrj.io.stream.metrics.Bucket)4 CountMetric (org.apache.solr.client.solrj.io.stream.metrics.CountMetric)4