Search in sources :

Example 76 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class SimpleFacets method getTermCounts.

/**
   * Term counts for use in field faceting that resepcts the specified mincount - 
   * if mincount is null, the "zeros" param is consulted for the appropriate backcompat 
   * default
   *
   * @see FacetParams#FACET_ZEROS
   */
private NamedList<Integer> getTermCounts(String field, Integer mincount, ParsedParams parsed) throws IOException {
    final SolrParams params = parsed.params;
    final DocSet docs = parsed.docs;
    final int threads = parsed.threads;
    int offset = params.getFieldInt(field, FacetParams.FACET_OFFSET, 0);
    int limit = params.getFieldInt(field, FacetParams.FACET_LIMIT, 100);
    if (limit == 0)
        return new NamedList<>();
    if (mincount == null) {
        Boolean zeros = params.getFieldBool(field, FacetParams.FACET_ZEROS);
        // mincount = (zeros!=null && zeros) ? 0 : 1;
        mincount = (zeros != null && !zeros) ? 1 : 0;
    // current default is to include zeros.
    }
    boolean missing = params.getFieldBool(field, FacetParams.FACET_MISSING, false);
    // default to sorting if there is a limit.
    String sort = params.getFieldParam(field, FacetParams.FACET_SORT, limit > 0 ? FacetParams.FACET_SORT_COUNT : FacetParams.FACET_SORT_INDEX);
    String prefix = params.getFieldParam(field, FacetParams.FACET_PREFIX);
    final Predicate<BytesRef> termFilter = newBytesRefFilter(field, params);
    boolean exists = params.getFieldBool(field, FacetParams.FACET_EXISTS, false);
    NamedList<Integer> counts;
    SchemaField sf = searcher.getSchema().getField(field);
    if (sf.getType().isPointField() && !sf.hasDocValues()) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't facet on a PointField without docValues");
    }
    FieldType ft = sf.getType();
    // determine what type of faceting method to use
    final String methodStr = params.getFieldParam(field, FacetParams.FACET_METHOD);
    final FacetMethod requestedMethod;
    if (FacetParams.FACET_METHOD_enum.equals(methodStr)) {
        requestedMethod = FacetMethod.ENUM;
    } else if (FacetParams.FACET_METHOD_fcs.equals(methodStr)) {
        requestedMethod = FacetMethod.FCS;
    } else if (FacetParams.FACET_METHOD_fc.equals(methodStr)) {
        requestedMethod = FacetMethod.FC;
    } else if (FacetParams.FACET_METHOD_uif.equals(methodStr)) {
        requestedMethod = FacetMethod.UIF;
    } else {
        requestedMethod = null;
    }
    final boolean multiToken = sf.multiValued() || ft.multiValuedFieldCache();
    FacetMethod appliedFacetMethod = selectFacetMethod(field, sf, requestedMethod, mincount, exists);
    RTimer timer = null;
    if (fdebug != null) {
        fdebug.putInfoItem("requestedMethod", requestedMethod == null ? "not specified" : requestedMethod.name());
        fdebug.putInfoItem("appliedMethod", appliedFacetMethod.name());
        fdebug.putInfoItem("inputDocSetSize", docs.size());
        fdebug.putInfoItem("field", field);
        timer = new RTimer();
    }
    if (params.getFieldBool(field, GroupParams.GROUP_FACET, false)) {
        counts = getGroupedCounts(searcher, docs, field, multiToken, offset, limit, mincount, missing, sort, prefix, termFilter);
    } else {
        assert appliedFacetMethod != null;
        switch(appliedFacetMethod) {
            case ENUM:
                assert TrieField.getMainValuePrefix(ft) == null;
                counts = getFacetTermEnumCounts(searcher, docs, field, offset, limit, mincount, missing, sort, prefix, termFilter, exists);
                break;
            case FCS:
                assert ft.isPointField() || !multiToken;
                if (ft.isPointField() || (ft.getNumberType() != null && !sf.multiValued())) {
                    if (prefix != null) {
                        throw new SolrException(ErrorCode.BAD_REQUEST, FacetParams.FACET_PREFIX + " is not supported on numeric types");
                    }
                    if (termFilter != null) {
                        throw new SolrException(ErrorCode.BAD_REQUEST, "BytesRef term filters (" + FacetParams.FACET_CONTAINS + ", " + FacetParams.FACET_EXCLUDETERMS + ") are not supported on numeric types");
                    }
                    //            We should do this, but mincount=0 is currently the default
                    //            if (ft.isPointField() && mincount <= 0) {
                    //              throw new SolrException(ErrorCode.BAD_REQUEST, FacetParams.FACET_MINCOUNT + " <= 0 is not supported on point types");
                    //            }
                    counts = NumericFacets.getCounts(searcher, docs, field, offset, limit, mincount, missing, sort);
                } else {
                    PerSegmentSingleValuedFaceting ps = new PerSegmentSingleValuedFaceting(searcher, docs, field, offset, limit, mincount, missing, sort, prefix, termFilter);
                    Executor executor = threads == 0 ? directExecutor : facetExecutor;
                    ps.setNumThreads(threads);
                    counts = ps.getFacetCounts(executor);
                }
                break;
            case UIF:
                //Emulate the JSON Faceting structure so we can use the same parsing classes
                Map<String, Object> jsonFacet = new HashMap<>(13);
                jsonFacet.put("type", "terms");
                jsonFacet.put("field", field);
                jsonFacet.put("offset", offset);
                jsonFacet.put("limit", limit);
                jsonFacet.put("mincount", mincount);
                jsonFacet.put("missing", missing);
                jsonFacet.put("prefix", prefix);
                jsonFacet.put("numBuckets", params.getFieldBool(field, "numBuckets", false));
                jsonFacet.put("allBuckets", params.getFieldBool(field, "allBuckets", false));
                jsonFacet.put("method", "uif");
                jsonFacet.put("cacheDf", 0);
                jsonFacet.put("perSeg", false);
                final String sortVal;
                switch(sort) {
                    case FacetParams.FACET_SORT_COUNT_LEGACY:
                        sortVal = FacetParams.FACET_SORT_COUNT;
                        break;
                    case FacetParams.FACET_SORT_INDEX_LEGACY:
                        sortVal = FacetParams.FACET_SORT_INDEX;
                        break;
                    default:
                        sortVal = sort;
                }
                jsonFacet.put(SORT, sortVal);
                Map<String, Object> topLevel = new HashMap<>();
                topLevel.put(field, jsonFacet);
                topLevel.put("processEmpty", true);
                FacetProcessor fproc = // rb.getResults().docSet
                FacetProcessor.createProcessor(// rb.getResults().docSet
                rb.req, // rb.getResults().docSet
                topLevel, docs);
                //TODO do we handle debug?  Should probably already be handled by the legacy code
                fproc.process();
                //Go through the response to build the expected output for SimpleFacets
                Object res = fproc.getResponse();
                counts = new NamedList<Integer>();
                if (res != null) {
                    SimpleOrderedMap<Object> som = (SimpleOrderedMap<Object>) res;
                    SimpleOrderedMap<Object> asdf = (SimpleOrderedMap<Object>) som.get(field);
                    List<SimpleOrderedMap<Object>> buckets = (List<SimpleOrderedMap<Object>>) asdf.get("buckets");
                    for (SimpleOrderedMap<Object> b : buckets) {
                        counts.add(b.get("val").toString(), (Integer) b.get("count"));
                    }
                    if (missing) {
                        SimpleOrderedMap<Object> missingCounts = (SimpleOrderedMap<Object>) asdf.get("missing");
                        counts.add(null, (Integer) missingCounts.get("count"));
                    }
                }
                break;
            case FC:
                counts = DocValuesFacets.getCounts(searcher, docs, field, offset, limit, mincount, missing, sort, prefix, termFilter, fdebug);
                break;
            default:
                throw new AssertionError();
        }
    }
    if (fdebug != null) {
        long timeElapsed = (long) timer.getTime();
        fdebug.setElapse(timeElapsed);
    }
    return counts;
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) FacetProcessor(org.apache.solr.search.facet.FacetProcessor) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Executor(java.util.concurrent.Executor) List(java.util.List) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) BytesRef(org.apache.lucene.util.BytesRef) SolrException(org.apache.solr.common.SolrException) RTimer(org.apache.solr.util.RTimer) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) RequiredSolrParams(org.apache.solr.common.params.RequiredSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) HashDocSet(org.apache.solr.search.HashDocSet) DocSet(org.apache.solr.search.DocSet) SortedIntDocSet(org.apache.solr.search.SortedIntDocSet) BitDocSet(org.apache.solr.search.BitDocSet)

Example 77 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class OverseerConfigSetMessageHandler method processMessage.

@Override
public SolrResponse processMessage(ZkNodeProps message, String operation) {
    NamedList results = new NamedList();
    try {
        if (!operation.startsWith(CONFIGSETS_ACTION_PREFIX)) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Operation does not contain proper prefix: " + operation + " expected: " + CONFIGSETS_ACTION_PREFIX);
        }
        operation = operation.substring(CONFIGSETS_ACTION_PREFIX.length());
        log.info("OverseerConfigSetMessageHandler.processMessage : " + operation + " , " + message.toString());
        ConfigSetParams.ConfigSetAction action = ConfigSetParams.ConfigSetAction.get(operation);
        if (action == null) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown operation:" + operation);
        }
        switch(action) {
            case CREATE:
                createConfigSet(message);
                break;
            case DELETE:
                deleteConfigSet(message);
                break;
            default:
                throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown operation:" + operation);
        }
    } catch (Exception e) {
        String configSetName = message.getStr(NAME);
        if (configSetName == null) {
            SolrException.log(log, "Operation " + operation + " failed", e);
        } else {
            SolrException.log(log, "ConfigSet: " + configSetName + " operation: " + operation + " failed", e);
        }
        results.add("Operation " + operation + " caused exception:", e);
        SimpleOrderedMap nl = new SimpleOrderedMap();
        nl.add("msg", e.getMessage());
        nl.add("rspCode", e instanceof SolrException ? ((SolrException) e).code() : -1);
        results.add("exception", nl);
    }
    return new OverseerSolrResponse(results);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ConfigSetParams(org.apache.solr.common.params.ConfigSetParams) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 78 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class OverseerCollectionMessageHandler method processResponse.

@SuppressWarnings("unchecked")
private void processResponse(NamedList results, Throwable e, String nodeName, SolrResponse solrResponse, String shard, Set<String> okayExceptions) {
    String rootThrowable = null;
    if (e instanceof RemoteSolrException) {
        rootThrowable = ((RemoteSolrException) e).getRootThrowable();
    }
    if (e != null && (rootThrowable == null || !okayExceptions.contains(rootThrowable))) {
        log.error("Error from shard: " + shard, e);
        SimpleOrderedMap failure = (SimpleOrderedMap) results.get("failure");
        if (failure == null) {
            failure = new SimpleOrderedMap();
            results.add("failure", failure);
        }
        failure.add(nodeName, e.getClass().getName() + ":" + e.getMessage());
    } else {
        SimpleOrderedMap success = (SimpleOrderedMap) results.get("success");
        if (success == null) {
            success = new SimpleOrderedMap();
            results.add("success", success);
        }
        success.add(nodeName, solrResponse.getResponse());
    }
}
Also used : RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 79 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class SegmentTerminateEarlyTestState method queryTimestampDescendingSegmentTerminateEarlyNo.

void queryTimestampDescendingSegmentTerminateEarlyNo(CloudSolrClient cloudSolrClient) throws Exception {
    TestMiniSolrCloudCluster.assertFalse(maxTimestampDocKeys.isEmpty());
    TestMiniSolrCloudCluster.assertTrue("numDocs=" + numDocs + " is not even", (numDocs % 2) == 0);
    final Long oddFieldValue = new Long(maxTimestampDocKeys.iterator().next().intValue() % 2);
    final SolrQuery query = new SolrQuery(ODD_FIELD + ":" + oddFieldValue);
    query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
    query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
    query.setRows(1);
    final Boolean shardsInfoWanted = (rand.nextBoolean() ? null : new Boolean(rand.nextBoolean()));
    if (shardsInfoWanted != null) {
        query.set(ShardParams.SHARDS_INFO, shardsInfoWanted.booleanValue());
    }
    query.set(CommonParams.SEGMENT_TERMINATE_EARLY, false);
    final QueryResponse rsp = cloudSolrClient.query(query);
    // check correctness of the results count
    TestMiniSolrCloudCluster.assertEquals("numFound", numDocs / 2, rsp.getResults().getNumFound());
    // check correctness of the first result
    if (rsp.getResults().getNumFound() > 0) {
        final SolrDocument solrDocument0 = rsp.getResults().get(0);
        TestMiniSolrCloudCluster.assertTrue(KEY_FIELD + " of (" + solrDocument0 + ") is not in maxTimestampDocKeys(" + maxTimestampDocKeys + ")", maxTimestampDocKeys.contains(solrDocument0.getFieldValue(KEY_FIELD)));
        TestMiniSolrCloudCluster.assertEquals(ODD_FIELD, oddFieldValue, rsp.getResults().get(0).getFieldValue(ODD_FIELD));
    }
    // check segmentTerminatedEarly flag
    TestMiniSolrCloudCluster.assertNull("responseHeader.segmentTerminatedEarly present in " + rsp.getResponseHeader(), rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
    TestMiniSolrCloudCluster.assertFalse("responseHeader.segmentTerminatedEarly present/true in " + rsp.getResponseHeader(), Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
    // check shards info
    final Object shardsInfo = rsp.getResponse().get(ShardParams.SHARDS_INFO);
    if (!Boolean.TRUE.equals(shardsInfoWanted)) {
        TestMiniSolrCloudCluster.assertNull(ShardParams.SHARDS_INFO, shardsInfo);
    } else {
        TestMiniSolrCloudCluster.assertNotNull(ShardParams.SHARDS_INFO, shardsInfo);
        int segmentTerminatedEarlyShardsCount = 0;
        for (Map.Entry<String, ?> si : (SimpleOrderedMap<?>) shardsInfo) {
            if (Boolean.TRUE.equals(((SimpleOrderedMap) si.getValue()).get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY))) {
                segmentTerminatedEarlyShardsCount += 1;
            }
        }
        TestMiniSolrCloudCluster.assertEquals("shards reporting " + SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY, 0, segmentTerminatedEarlyShardsCount);
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 80 with SimpleOrderedMap

use of org.apache.solr.common.util.SimpleOrderedMap in project lucene-solr by apache.

the class SegmentTerminateEarlyTestState method queryTimestampDescendingSegmentTerminateEarlyYes.

void queryTimestampDescendingSegmentTerminateEarlyYes(CloudSolrClient cloudSolrClient) throws Exception {
    TestMiniSolrCloudCluster.assertFalse(maxTimestampDocKeys.isEmpty());
    TestMiniSolrCloudCluster.assertTrue("numDocs=" + numDocs + " is not even", (numDocs % 2) == 0);
    final Long oddFieldValue = new Long(maxTimestampDocKeys.iterator().next().intValue() % 2);
    final SolrQuery query = new SolrQuery(ODD_FIELD + ":" + oddFieldValue);
    query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
    query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
    final int rowsWanted = 1;
    query.setRows(rowsWanted);
    final Boolean shardsInfoWanted = (rand.nextBoolean() ? null : new Boolean(rand.nextBoolean()));
    if (shardsInfoWanted != null) {
        query.set(ShardParams.SHARDS_INFO, shardsInfoWanted.booleanValue());
    }
    query.set(CommonParams.SEGMENT_TERMINATE_EARLY, true);
    final QueryResponse rsp = cloudSolrClient.query(query);
    // check correctness of the results count
    TestMiniSolrCloudCluster.assertTrue("numFound", rowsWanted <= rsp.getResults().getNumFound());
    TestMiniSolrCloudCluster.assertTrue("numFound", rsp.getResults().getNumFound() <= numDocs / 2);
    // check correctness of the first result
    if (rsp.getResults().getNumFound() > 0) {
        final SolrDocument solrDocument0 = rsp.getResults().get(0);
        TestMiniSolrCloudCluster.assertTrue(KEY_FIELD + " of (" + solrDocument0 + ") is not in maxTimestampDocKeys(" + maxTimestampDocKeys + ")", maxTimestampDocKeys.contains(solrDocument0.getFieldValue(KEY_FIELD)));
        TestMiniSolrCloudCluster.assertEquals(ODD_FIELD, oddFieldValue, rsp.getResults().get(0).getFieldValue(ODD_FIELD));
    }
    // check segmentTerminatedEarly flag
    TestMiniSolrCloudCluster.assertNotNull("responseHeader.segmentTerminatedEarly missing in " + rsp.getResponseHeader(), rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
    TestMiniSolrCloudCluster.assertTrue("responseHeader.segmentTerminatedEarly missing/false in " + rsp.getResponseHeader(), Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
    // check shards info
    final Object shardsInfo = rsp.getResponse().get(ShardParams.SHARDS_INFO);
    if (!Boolean.TRUE.equals(shardsInfoWanted)) {
        TestMiniSolrCloudCluster.assertNull(ShardParams.SHARDS_INFO, shardsInfo);
    } else {
        TestMiniSolrCloudCluster.assertNotNull(ShardParams.SHARDS_INFO, shardsInfo);
        int segmentTerminatedEarlyShardsCount = 0;
        for (Map.Entry<String, ?> si : (SimpleOrderedMap<?>) shardsInfo) {
            if (Boolean.TRUE.equals(((SimpleOrderedMap) si.getValue()).get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY))) {
                segmentTerminatedEarlyShardsCount += 1;
            }
        }
        // check segmentTerminatedEarly flag within shards info
        TestMiniSolrCloudCluster.assertTrue(segmentTerminatedEarlyShardsCount + " shards reported " + SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY, (0 < segmentTerminatedEarlyShardsCount));
    }
}
Also used : SolrDocument(org.apache.solr.common.SolrDocument) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Aggregations

SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)131 NamedList (org.apache.solr.common.util.NamedList)69 ArrayList (java.util.ArrayList)36 SolrException (org.apache.solr.common.SolrException)32 Map (java.util.Map)27 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)22 IOException (java.io.IOException)19 List (java.util.List)19 HashMap (java.util.HashMap)18 SolrParams (org.apache.solr.common.params.SolrParams)16 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)16 SchemaField (org.apache.solr.schema.SchemaField)16 FieldType (org.apache.solr.schema.FieldType)14 BytesRef (org.apache.lucene.util.BytesRef)13 Test (org.junit.Test)13 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)12 SolrDocumentList (org.apache.solr.common.SolrDocumentList)11 HashSet (java.util.HashSet)10 SolrCore (org.apache.solr.core.SolrCore)10 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)9