Search in sources :

Example 81 with SimpleOrderedMap

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

the class StatsInfo method convertToResponse.

/**
   * Given a map of {@link StatsValues} using the appropriate response key,
   * builds up the necessary "stats" data structure for including in the response -- 
   * including the esoteric "stats_fields" wrapper.
   */
public static NamedList<NamedList<NamedList<?>>> convertToResponse(Map<String, StatsValues> statsValues) {
    NamedList<NamedList<NamedList<?>>> stats = new SimpleOrderedMap<>();
    NamedList<NamedList<?>> stats_fields = new SimpleOrderedMap<>();
    stats.add("stats_fields", stats_fields);
    for (Map.Entry<String, StatsValues> entry : statsValues.entrySet()) {
        String key = entry.getKey();
        NamedList stv = entry.getValue().getStatsValues();
        stats_fields.add(key, stv);
    }
    return stats;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 82 with SimpleOrderedMap

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

the class SolrPluginUtils method doStandardDebug.

/**
   * <p>
   * Returns a NamedList containing many "standard" pieces of debugging
   * information.
   * </p>
   *
   * <ul>
   * <li>rawquerystring - the 'q' param exactly as specified by the client
   * </li>
   * <li>querystring - the 'q' param after any preprocessing done by the plugin
   * </li>
   * <li>parsedquery - the main query executed formated by the Solr
   *     QueryParsing utils class (which knows about field types)
   * </li>
   * <li>parsedquery_toString - the main query executed formatted by its
   *     own toString method (in case it has internal state Solr
   *     doesn't know about)
   * </li>
   * <li>explain - the list of score explanations for each document in
   *     results against query.
   * </li>
   * <li>otherQuery - the query string specified in 'explainOther' query param.
   * </li>
   * <li>explainOther - the list of score explanations for each document in
   *     results against 'otherQuery'
   * </li>
   * </ul>
   *
   * @param req the request we are dealing with
   * @param userQuery the users query as a string, after any basic
   *                  preprocessing has been done
   * @param query the query built from the userQuery
   *              (and perhaps other clauses) that identifies the main
   *              result set of the response.
   * @param results the main result set of the response
   * @return The debug info
   * @throws java.io.IOException if there was an IO error
   */
public static NamedList doStandardDebug(SolrQueryRequest req, String userQuery, Query query, DocList results, boolean dbgQuery, boolean dbgResults) throws IOException {
    NamedList dbg = new SimpleOrderedMap();
    doStandardQueryDebug(req, userQuery, query, dbgQuery, dbg);
    doStandardResultsDebug(req, query, results, dbgResults, dbg);
    return dbg;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 83 with SimpleOrderedMap

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

the class ClusterStatus method getClusterStatus.

@SuppressWarnings("unchecked")
public void getClusterStatus(NamedList results) throws KeeperException, InterruptedException {
    // read aliases
    Aliases aliases = zkStateReader.getAliases();
    Map<String, List<String>> collectionVsAliases = new HashMap<>();
    Map<String, String> aliasVsCollections = aliases.getCollectionAliasMap();
    if (aliasVsCollections != null) {
        for (Map.Entry<String, String> entry : aliasVsCollections.entrySet()) {
            List<String> colls = StrUtils.splitSmart(entry.getValue(), ',');
            String alias = entry.getKey();
            for (String coll : colls) {
                if (collection == null || collection.equals(coll)) {
                    List<String> list = collectionVsAliases.get(coll);
                    if (list == null) {
                        list = new ArrayList<>();
                        collectionVsAliases.put(coll, list);
                    }
                    list.add(alias);
                }
            }
        }
    }
    Map roles = null;
    if (zkStateReader.getZkClient().exists(ZkStateReader.ROLES, true)) {
        roles = (Map) Utils.fromJSON(zkStateReader.getZkClient().getData(ZkStateReader.ROLES, null, null, true));
    }
    ClusterState clusterState = zkStateReader.getClusterState();
    // convert cluster state into a map of writable types
    byte[] bytes = Utils.toJSON(clusterState);
    Map<String, Object> stateMap = (Map<String, Object>) Utils.fromJSON(bytes);
    String routeKey = message.getStr(ShardParams._ROUTE_);
    String shard = message.getStr(ZkStateReader.SHARD_ID_PROP);
    Map<String, DocCollection> collectionsMap = null;
    if (collection == null) {
        collectionsMap = clusterState.getCollectionsMap();
    } else {
        collectionsMap = Collections.singletonMap(collection, clusterState.getCollectionOrNull(collection));
    }
    NamedList<Object> collectionProps = new SimpleOrderedMap<>();
    for (Map.Entry<String, DocCollection> entry : collectionsMap.entrySet()) {
        Map<String, Object> collectionStatus;
        String name = entry.getKey();
        DocCollection clusterStateCollection = entry.getValue();
        if (clusterStateCollection == null) {
            if (collection != null) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Collection: " + name + " not found");
            } else {
                //collection might have got deleted at the same time
                continue;
            }
        }
        Set<String> requestedShards = new HashSet<>();
        if (routeKey != null) {
            DocRouter router = clusterStateCollection.getRouter();
            Collection<Slice> slices = router.getSearchSlices(routeKey, null, clusterStateCollection);
            for (Slice slice : slices) {
                requestedShards.add(slice.getName());
            }
        }
        if (shard != null) {
            requestedShards.add(shard);
        }
        if (clusterStateCollection.getStateFormat() > 1) {
            bytes = Utils.toJSON(clusterStateCollection);
            Map<String, Object> docCollection = (Map<String, Object>) Utils.fromJSON(bytes);
            collectionStatus = getCollectionStatus(docCollection, name, requestedShards);
        } else {
            collectionStatus = getCollectionStatus((Map<String, Object>) stateMap.get(name), name, requestedShards);
        }
        collectionStatus.put("znodeVersion", clusterStateCollection.getZNodeVersion());
        if (collectionVsAliases.containsKey(name) && !collectionVsAliases.get(name).isEmpty()) {
            collectionStatus.put("aliases", collectionVsAliases.get(name));
        }
        String configName = zkStateReader.readConfigName(name);
        collectionStatus.put("configName", configName);
        collectionProps.add(name, collectionStatus);
    }
    List<String> liveNodes = zkStateReader.getZkClient().getChildren(ZkStateReader.LIVE_NODES_ZKNODE, null, true);
    // now we need to walk the collectionProps tree to cross-check replica state with live nodes
    crossCheckReplicaStateWithLiveNodes(liveNodes, collectionProps);
    NamedList<Object> clusterStatus = new SimpleOrderedMap<>();
    clusterStatus.add("collections", collectionProps);
    // read cluster properties
    Map clusterProps = zkStateReader.getClusterProperties();
    if (clusterProps != null && !clusterProps.isEmpty()) {
        clusterStatus.add("properties", clusterProps);
    }
    // add the alias map too
    if (aliasVsCollections != null && !aliasVsCollections.isEmpty()) {
        clusterStatus.add("aliases", aliasVsCollections);
    }
    // add the roles map
    if (roles != null) {
        clusterStatus.add("roles", roles);
    }
    // add live_nodes
    clusterStatus.add("live_nodes", liveNodes);
    results.add("cluster", clusterStatus);
}
Also used : HashMap(java.util.HashMap) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) DocRouter(org.apache.solr.common.cloud.DocRouter) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet) ClusterState(org.apache.solr.common.cloud.ClusterState) Aliases(org.apache.solr.common.cloud.Aliases) Slice(org.apache.solr.common.cloud.Slice) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 84 with SimpleOrderedMap

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

the class LoggingHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    // Don't do anything if the framework is unknown
    if (watcher == null) {
        rsp.add("error", "Logging Not Initialized");
        return;
    }
    rsp.add("watcher", watcher.getName());
    SolrParams params = req.getParams();
    if (params.get("threshold") != null) {
        watcher.setThreshold(params.get("threshold"));
    }
    // Write something at each level
    if (params.get("test") != null) {
        log.trace("trace message");
        log.debug("debug message");
        log.info("info (with exception)", new RuntimeException("test"));
        log.warn("warn (with exception)", new RuntimeException("test"));
        log.error("error (with exception)", new RuntimeException("test"));
    }
    String[] set = params.getParams("set");
    if (set != null) {
        for (String pair : set) {
            String[] split = pair.split(":");
            if (split.length != 2) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid format, expected level:value, got " + pair);
            }
            String category = split[0];
            String level = split[1];
            watcher.setLogLevel(category, level);
        }
    }
    String since = req.getParams().get("since");
    if (since != null) {
        long time = -1;
        try {
            time = Long.parseLong(since);
        } catch (Exception ex) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "invalid timestamp: " + since);
        }
        AtomicBoolean found = new AtomicBoolean(false);
        SolrDocumentList docs = watcher.getHistory(time, found);
        if (docs == null) {
            rsp.add("error", "History not enabled");
            return;
        } else {
            SimpleOrderedMap<Object> info = new SimpleOrderedMap<>();
            if (time > 0) {
                info.add("since", time);
                info.add("found", found.get());
            } else {
                // show for the first request
                info.add("levels", watcher.getAllLevels());
            }
            info.add("last", watcher.getLastEvent());
            info.add("buffer", watcher.getHistorySize());
            info.add("threshold", watcher.getThreshold());
            rsp.add("info", info);
            rsp.add("history", docs);
        }
    } else {
        rsp.add("levels", watcher.getAllLevels());
        List<LoggerInfo> loggers = new ArrayList<>(watcher.getAllLoggers());
        Collections.sort(loggers);
        List<SimpleOrderedMap<?>> info = new ArrayList<>();
        for (LoggerInfo wrap : loggers) {
            info.add(wrap.getInfo());
        }
        rsp.add("loggers", info);
    }
    rsp.setHttpCaching(false);
}
Also used : ArrayList(java.util.ArrayList) LoggerInfo(org.apache.solr.logging.LoggerInfo) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SolrParams(org.apache.solr.common.params.SolrParams) SolrException(org.apache.solr.common.SolrException)

Example 85 with SimpleOrderedMap

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

the class LukeRequestHandler method getIndexedFieldsInfo.

private static SimpleOrderedMap<Object> getIndexedFieldsInfo(SolrQueryRequest req) throws Exception {
    SolrIndexSearcher searcher = req.getSearcher();
    SolrParams params = req.getParams();
    Set<String> fields = null;
    String fl = params.get(CommonParams.FL);
    if (fl != null) {
        fields = new TreeSet<>(Arrays.asList(fl.split("[,\\s]+")));
    }
    LeafReader reader = searcher.getSlowAtomicReader();
    IndexSchema schema = searcher.getSchema();
    // Don't be tempted to put this in the loop below, the whole point here is to alphabetize the fields!
    Set<String> fieldNames = new TreeSet<>();
    for (FieldInfo fieldInfo : reader.getFieldInfos()) {
        fieldNames.add(fieldInfo.name);
    }
    // Walk the term enum and keep a priority queue for each map in our set
    SimpleOrderedMap<Object> finfo = new SimpleOrderedMap<>();
    for (String fieldName : fieldNames) {
        if (fields != null && !fields.contains(fieldName) && !fields.contains("*")) {
            //we're not interested in this field Still an issue here
            continue;
        }
        SimpleOrderedMap<Object> fieldMap = new SimpleOrderedMap<>();
        SchemaField sfield = schema.getFieldOrNull(fieldName);
        FieldType ftype = (sfield == null) ? null : sfield.getType();
        fieldMap.add("type", (ftype == null) ? null : ftype.getTypeName());
        fieldMap.add("schema", getFieldFlags(sfield));
        if (sfield != null && schema.isDynamicField(sfield.getName()) && schema.getDynamicPattern(sfield.getName()) != null) {
            fieldMap.add("dynamicBase", schema.getDynamicPattern(sfield.getName()));
        }
        Terms terms = reader.fields().terms(fieldName);
        if (terms == null) {
            // Not indexed, so we need to report what we can (it made it through the fl param if specified)
            finfo.add(fieldName, fieldMap);
            continue;
        }
        if (sfield != null && sfield.indexed()) {
            if (params.getBool(INCLUDE_INDEX_FIELD_FLAGS, true)) {
                Document doc = getFirstLiveDoc(terms, reader);
                if (doc != null) {
                    // Found a document with this field
                    try {
                        IndexableField fld = doc.getField(fieldName);
                        if (fld != null) {
                            fieldMap.add("index", getFieldFlags(fld));
                        } else {
                            // it is a non-stored field...
                            fieldMap.add("index", "(unstored field)");
                        }
                    } catch (Exception ex) {
                        log.warn("error reading field: " + fieldName);
                    }
                }
            }
            fieldMap.add("docs", terms.getDocCount());
        }
        if (fields != null && (fields.contains(fieldName) || fields.contains("*"))) {
            getDetailedFieldInfo(req, fieldName, fieldMap);
        }
        // Add the field
        finfo.add(fieldName, fieldMap);
    }
    return finfo;
}
Also used : LeafReader(org.apache.lucene.index.LeafReader) Terms(org.apache.lucene.index.Terms) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Document(org.apache.lucene.document.Document) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) IndexableField(org.apache.lucene.index.IndexableField) TreeSet(java.util.TreeSet) SolrParams(org.apache.solr.common.params.SolrParams) IndexSchema(org.apache.solr.schema.IndexSchema) FieldInfo(org.apache.lucene.index.FieldInfo)

Aggregations

SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)132 NamedList (org.apache.solr.common.util.NamedList)70 ArrayList (java.util.ArrayList)37 SolrException (org.apache.solr.common.SolrException)32 Map (java.util.Map)28 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)22 List (java.util.List)20 IOException (java.io.IOException)19 HashMap (java.util.HashMap)19 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