Search in sources :

Example 26 with SimpleOrderedMap

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

the class DumpRequestHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    // Show params
    rsp.add("params", req.getParams().toNamedList());
    String[] parts = req.getParams().getParams("urlTemplateValues");
    if (parts != null && parts.length > 0) {
        Map map = new LinkedHashMap<>();
        rsp.getValues().add("urlTemplateValues", map);
        for (String part : parts) {
            map.put(part, req.getPathTemplateValues().get(part));
        }
    }
    String[] returnParams = req.getParams().getParams("param");
    if (returnParams != null) {
        NamedList params = (NamedList) rsp.getValues().get("params");
        for (String returnParam : returnParams) {
            String[] vals = req.getParams().getParams(returnParam);
            if (vals != null) {
                if (vals.length == 1) {
                    params.add(returnParam, vals[0]);
                } else {
                    params.add(returnParam, vals);
                }
            }
        }
    }
    if (req.getParams().getBool("getdefaults", false)) {
        NamedList def = (NamedList) initArgs.get(PluginInfo.DEFAULTS);
        rsp.add("getdefaults", def);
    }
    if (req.getParams().getBool("initArgs", false)) {
        rsp.add("initArgs", initArgs);
    }
    // Write the streams...
    if (req.getContentStreams() != null) {
        ArrayList<NamedList<Object>> streams = new ArrayList<>();
        // Cycle through each stream
        for (ContentStream content : req.getContentStreams()) {
            NamedList<Object> stream = new SimpleOrderedMap<>();
            stream.add(NAME, content.getName());
            stream.add("sourceInfo", content.getSourceInfo());
            stream.add("size", content.getSize());
            stream.add("contentType", content.getContentType());
            Reader reader = content.getReader();
            try {
                stream.add("stream", IOUtils.toString(reader));
            } finally {
                reader.close();
            }
            streams.add(stream);
        }
        rsp.add("streams", streams);
    }
    rsp.add("context", req.getContext());
}
Also used : ContentStream(org.apache.solr.common.util.ContentStream) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) Reader(java.io.Reader) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) LinkedHashMap(java.util.LinkedHashMap)

Example 27 with SimpleOrderedMap

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

the class SuggestComponent method toNamedList.

/** Convert {@link SuggesterResult} to NamedList for constructing responses */
private void toNamedList(SuggesterResult suggesterResult, Map<String, SimpleOrderedMap<NamedList<Object>>> resultObj) {
    for (String suggesterName : suggesterResult.getSuggesterNames()) {
        SimpleOrderedMap<NamedList<Object>> results = new SimpleOrderedMap<>();
        for (String token : suggesterResult.getTokens(suggesterName)) {
            SimpleOrderedMap<Object> suggestionBody = new SimpleOrderedMap<>();
            List<LookupResult> lookupResults = suggesterResult.getLookupResult(suggesterName, token);
            suggestionBody.add(SuggesterResultLabels.SUGGESTION_NUM_FOUND, lookupResults.size());
            List<SimpleOrderedMap<Object>> suggestEntriesNamedList = new ArrayList<>();
            for (LookupResult lookupResult : lookupResults) {
                String suggestionString = lookupResult.key.toString();
                long weight = lookupResult.value;
                String payload = (lookupResult.payload != null) ? lookupResult.payload.utf8ToString() : "";
                SimpleOrderedMap<Object> suggestEntryNamedList = new SimpleOrderedMap<>();
                suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_TERM, suggestionString);
                suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_WEIGHT, weight);
                suggestEntryNamedList.add(SuggesterResultLabels.SUGGESTION_PAYLOAD, payload);
                suggestEntriesNamedList.add(suggestEntryNamedList);
            }
            suggestionBody.add(SuggesterResultLabels.SUGGESTIONS, suggestEntriesNamedList);
            results.add(token, suggestionBody);
        }
        resultObj.put(suggesterName, results);
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) LookupResult(org.apache.lucene.search.suggest.Lookup.LookupResult) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap)

Example 28 with SimpleOrderedMap

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

the class TermsComponent method fetchTerms.

private static void fetchTerms(SolrIndexSearcher indexSearcher, String[] fields, String termList, boolean includeTotalTermFreq, NamedList<Object> result) throws IOException {
    String[] splitTerms = termList.split(",");
    for (int i = 0; i < splitTerms.length; i++) {
        splitTerms[i] = splitTerms[i].trim();
    }
    // Sort the terms once
    Arrays.sort(splitTerms);
    IndexReaderContext topReaderContext = indexSearcher.getTopReaderContext();
    for (String field : fields) {
        FieldType fieldType = indexSearcher.getSchema().getField(field).getType();
        // Since splitTerms is already sorted, this array will also be sorted
        Term[] terms = new Term[splitTerms.length];
        for (int i = 0; i < splitTerms.length; i++) {
            terms[i] = new Term(field, fieldType.readableToIndexed(splitTerms[i]));
        }
        TermContext[] termContexts = new TermContext[terms.length];
        collectTermContext(topReaderContext, termContexts, terms);
        NamedList<Object> termsMap = new SimpleOrderedMap<>();
        for (int i = 0; i < terms.length; i++) {
            if (termContexts[i] != null) {
                String outTerm = fieldType.indexedToReadable(terms[i].bytes().utf8ToString());
                int docFreq = termContexts[i].docFreq();
                if (!includeTotalTermFreq) {
                    termsMap.add(outTerm, docFreq);
                } else {
                    long totalTermFreq = termContexts[i].totalTermFreq();
                    NamedList<Long> termStats = new SimpleOrderedMap<>();
                    termStats.add("df", (long) docFreq);
                    termStats.add("ttf", totalTermFreq);
                    termsMap.add(outTerm, termStats);
                }
            }
        }
        result.add(field, termsMap);
    }
}
Also used : SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType)

Example 29 with SimpleOrderedMap

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

the class SimpleFacets method getFacetIntervalCounts.

/**
   * Returns a <code>NamedList</code> with each entry having the "key" of the interval as name and the count of docs 
   * in that interval as value. All intervals added in the request are included in the returned 
   * <code>NamedList</code> (included those with 0 count), and it's required that the order of the intervals
   * is deterministic and equals in all shards of a distributed request, otherwise the collation of results
   * will fail. 
   * 
   */
public NamedList<Object> getFacetIntervalCounts() throws IOException, SyntaxError {
    NamedList<Object> res = new SimpleOrderedMap<Object>();
    String[] fields = global.getParams(FacetParams.FACET_INTERVAL);
    if (fields == null || fields.length == 0)
        return res;
    for (String field : fields) {
        final ParsedParams parsed = parseParams(FacetParams.FACET_INTERVAL, field);
        String[] intervalStrs = parsed.required.getFieldParams(parsed.facetValue, FacetParams.FACET_INTERVAL_SET);
        SchemaField schemaField = searcher.getCore().getLatestSchema().getField(parsed.facetValue);
        if (parsed.params.getBool(GroupParams.GROUP_FACET, false)) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Interval Faceting can't be used with " + GroupParams.GROUP_FACET);
        }
        if (schemaField.getType().isPointField() && !schemaField.hasDocValues()) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't use interval faceting on a PointField without docValues");
        }
        SimpleOrderedMap<Integer> fieldResults = new SimpleOrderedMap<Integer>();
        res.add(parsed.key, fieldResults);
        IntervalFacets intervalFacets = new IntervalFacets(schemaField, searcher, parsed.docs, intervalStrs, parsed.params);
        for (FacetInterval interval : intervalFacets) {
            fieldResults.add(interval.getKey(), interval.getCount());
        }
    }
    return res;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) FacetInterval(org.apache.solr.request.IntervalFacets.FacetInterval) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException)

Example 30 with SimpleOrderedMap

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

the class BaseSolrResource method handleException.

/**
   * If there is an exception on the SolrResponse:
   * <ul>
   *   <li>error info is added to the SolrResponse;</li>
   *   <li>the response status code is set to the error code from the exception; and</li>
   *   <li>the exception message is added to the list of things to be logged.</li>
   * </ul>
   */
protected void handleException(Logger log) {
    Exception exception = getSolrResponse().getException();
    if (null != exception) {
        NamedList info = new SimpleOrderedMap();
        int code = ResponseUtils.getErrorInfo(exception, info, log);
        setStatus(Status.valueOf(code));
        getSolrResponse().add("error", info);
        String message = (String) info.get("msg");
        if (null != message && !message.trim().isEmpty()) {
            getSolrResponse().getToLog().add("msg", "{" + message.trim() + "}");
        }
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) IOException(java.io.IOException) ResourceException(org.restlet.resource.ResourceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

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