Search in sources :

Example 21 with SimpleOrderedMap

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

the class OverseerCollectionMessageHandler method processMessage.

@Override
@SuppressWarnings("unchecked")
public SolrResponse processMessage(ZkNodeProps message, String operation) {
    log.debug("OverseerCollectionMessageHandler.processMessage : " + operation + " , " + message.toString());
    NamedList results = new NamedList();
    try {
        CollectionAction action = getCollectionAction(operation);
        Cmd command = commandMap.get(action);
        if (command != null) {
            command.call(zkStateReader.getClusterState(), message, results);
        } else {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Unknown operation:" + operation);
        }
    } catch (Exception e) {
        String collName = message.getStr("collection");
        if (collName == null)
            collName = message.getStr(NAME);
        if (collName == null) {
            SolrException.log(log, "Operation " + operation + " failed", e);
        } else {
            SolrException.log(log, "Collection: " + collName + " 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) CollectionAction(org.apache.solr.common.params.CollectionParams.CollectionAction) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) SolrException(org.apache.solr.common.SolrException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException)

Example 22 with SimpleOrderedMap

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

the class FieldAnalysisRequestHandler method analyzeValues.

/**
   * Analyzes the index value (if it exists) and the query value (if it exists) in the given AnalysisRequest, using
   * the Analyzers of the given field type.
   *
   * @param analysisRequest AnalysisRequest from where the index and query values will be taken
   * @param fieldType       Type of field whose analyzers will be used
   * @param fieldName       Name of the field to be analyzed.  Can be {@code null}
   *
   * @return NamedList containing the tokens produced by the analyzers of the given field, separated into an index and
   *         a query group
   */
// package access for testing
NamedList<NamedList> analyzeValues(FieldAnalysisRequest analysisRequest, FieldType fieldType, String fieldName) {
    final String queryValue = analysisRequest.getQuery();
    final Set<BytesRef> termsToMatch = (queryValue != null && analysisRequest.isShowMatch()) ? getQueryTokenSet(queryValue, fieldType.getQueryAnalyzer()) : EMPTY_BYTES_SET;
    NamedList<NamedList> analyzeResults = new SimpleOrderedMap<>();
    if (analysisRequest.getFieldValue() != null) {
        AnalysisContext context = new AnalysisContext(fieldName, fieldType, fieldType.getIndexAnalyzer(), termsToMatch);
        NamedList analyzedTokens = analyzeValue(analysisRequest.getFieldValue(), context);
        analyzeResults.add("index", analyzedTokens);
    }
    if (analysisRequest.getQuery() != null) {
        AnalysisContext context = new AnalysisContext(fieldName, fieldType, fieldType.getQueryAnalyzer());
        NamedList analyzedTokens = analyzeValue(analysisRequest.getQuery(), context);
        analyzeResults.add("query", analyzedTokens);
    }
    return analyzeResults;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) BytesRef(org.apache.lucene.util.BytesRef)

Example 23 with SimpleOrderedMap

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

the class FieldAnalysisRequestHandler method handleAnalysisRequest.

/**
   * Handles the resolved analysis request and returns the analysis breakdown response as a named list.
   *
   * @param request The request to handle.
   * @param schema  The index schema.
   *
   * @return The analysis breakdown as a named list.
   */
protected NamedList<NamedList> handleAnalysisRequest(FieldAnalysisRequest request, IndexSchema schema) {
    NamedList<NamedList> analysisResults = new SimpleOrderedMap<>();
    NamedList<NamedList> fieldTypeAnalysisResults = new SimpleOrderedMap<>();
    if (request.getFieldTypes() != null) {
        for (String fieldTypeName : request.getFieldTypes()) {
            FieldType fieldType = schema.getFieldTypes().get(fieldTypeName);
            fieldTypeAnalysisResults.add(fieldTypeName, analyzeValues(request, fieldType, null));
        }
    }
    NamedList<NamedList> fieldNameAnalysisResults = new SimpleOrderedMap<>();
    if (request.getFieldNames() != null) {
        for (String fieldName : request.getFieldNames()) {
            FieldType fieldType = schema.getFieldType(fieldName);
            fieldNameAnalysisResults.add(fieldName, analyzeValues(request, fieldType, fieldName));
        }
    }
    analysisResults.add("field_types", fieldTypeAnalysisResults);
    analysisResults.add("field_names", fieldNameAnalysisResults);
    return analysisResults;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType)

Example 24 with SimpleOrderedMap

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

the class AnalysisRequestHandlerBase method convertTokensToNamedLists.

/**
   * Converts the list of Tokens to a list of NamedLists representing the tokens.
   *
   * @param tokenList  Tokens to convert
   * @param context The analysis context
   *
   * @return List of NamedLists containing the relevant information taken from the tokens
   */
private List<NamedList> convertTokensToNamedLists(final List<AttributeSource> tokenList, AnalysisContext context) {
    final List<NamedList> tokensNamedLists = new ArrayList<>();
    final FieldType fieldType = context.getFieldType();
    final AttributeSource[] tokens = tokenList.toArray(new AttributeSource[tokenList.size()]);
    // sort the tokens by absolute position
    ArrayUtil.timSort(tokens, new Comparator<AttributeSource>() {

        @Override
        public int compare(AttributeSource a, AttributeSource b) {
            return arrayCompare(a.getAttribute(TokenTrackingAttribute.class).getPositions(), b.getAttribute(TokenTrackingAttribute.class).getPositions());
        }

        private int arrayCompare(int[] a, int[] b) {
            int p = 0;
            final int stop = Math.min(a.length, b.length);
            while (p < stop) {
                int diff = a[p] - b[p];
                if (diff != 0)
                    return diff;
                p++;
            }
            // One is a prefix of the other, or, they are equal:
            return a.length - b.length;
        }
    });
    for (int i = 0; i < tokens.length; i++) {
        AttributeSource token = tokens[i];
        final NamedList<Object> tokenNamedList = new SimpleOrderedMap<>();
        final TermToBytesRefAttribute termAtt = token.getAttribute(TermToBytesRefAttribute.class);
        BytesRef rawBytes = termAtt.getBytesRef();
        final String text = fieldType.indexedToReadable(rawBytes, new CharsRefBuilder()).toString();
        tokenNamedList.add("text", text);
        if (token.hasAttribute(CharTermAttribute.class)) {
            final String rawText = token.getAttribute(CharTermAttribute.class).toString();
            if (!rawText.equals(text)) {
                tokenNamedList.add("raw_text", rawText);
            }
        }
        tokenNamedList.add("raw_bytes", rawBytes.toString());
        if (context.getTermsToMatch().contains(rawBytes)) {
            tokenNamedList.add("match", true);
        }
        token.reflectWith(new AttributeReflector() {

            @Override
            public void reflect(Class<? extends Attribute> attClass, String key, Object value) {
                // leave out position and bytes term
                if (TermToBytesRefAttribute.class.isAssignableFrom(attClass))
                    return;
                if (CharTermAttribute.class.isAssignableFrom(attClass))
                    return;
                if (PositionIncrementAttribute.class.isAssignableFrom(attClass))
                    return;
                String k = attClass.getName() + '#' + key;
                // map keys for "standard attributes":
                if (ATTRIBUTE_MAPPING.containsKey(k)) {
                    k = ATTRIBUTE_MAPPING.get(k);
                }
                if (value instanceof BytesRef) {
                    final BytesRef p = (BytesRef) value;
                    value = p.toString();
                }
                tokenNamedList.add(k, value);
            }
        });
        tokensNamedLists.add(tokenNamedList);
    }
    return tokensNamedLists;
}
Also used : AttributeSource(org.apache.lucene.util.AttributeSource) NamedList(org.apache.solr.common.util.NamedList) AttributeReflector(org.apache.lucene.util.AttributeReflector) ArrayList(java.util.ArrayList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType) PositionIncrementAttribute(org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) TermToBytesRefAttribute(org.apache.lucene.analysis.tokenattributes.TermToBytesRefAttribute) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 25 with SimpleOrderedMap

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

the class DocumentAnalysisRequestHandler method handleAnalysisRequest.

/**
   * Handles the resolved {@link DocumentAnalysisRequest} and returns the analysis response as a named list.
   *
   * @param request The {@link DocumentAnalysisRequest} to be handled.
   * @param schema  The index schema.
   *
   * @return The analysis response as a named list.
   */
NamedList<Object> handleAnalysisRequest(DocumentAnalysisRequest request, IndexSchema schema) {
    SchemaField uniqueKeyField = schema.getUniqueKeyField();
    NamedList<Object> result = new SimpleOrderedMap<>();
    for (SolrInputDocument document : request.getDocuments()) {
        NamedList<NamedList> theTokens = new SimpleOrderedMap<>();
        result.add(document.getFieldValue(uniqueKeyField.getName()).toString(), theTokens);
        for (String name : document.getFieldNames()) {
            // there's no point of providing analysis to unindexed fields.
            SchemaField field = schema.getField(name);
            if (!field.indexed()) {
                continue;
            }
            NamedList<Object> fieldTokens = new SimpleOrderedMap<>();
            theTokens.add(name, fieldTokens);
            FieldType fieldType = schema.getFieldType(name);
            final String queryValue = request.getQuery();
            Set<BytesRef> termsToMatch;
            try {
                termsToMatch = (queryValue != null && request.isShowMatch()) ? getQueryTokenSet(queryValue, fieldType.getQueryAnalyzer()) : EMPTY_BYTES_SET;
            } catch (Exception e) {
                // ignore analysis exceptions since we are applying arbitrary text to all fields
                termsToMatch = EMPTY_BYTES_SET;
            }
            if (request.getQuery() != null) {
                try {
                    AnalysisContext analysisContext = new AnalysisContext(fieldType, fieldType.getQueryAnalyzer(), EMPTY_BYTES_SET);
                    fieldTokens.add("query", analyzeValue(request.getQuery(), analysisContext));
                } catch (Exception e) {
                // ignore analysis exceptions since we are applying arbitrary text to all fields
                }
            }
            Analyzer analyzer = fieldType.getIndexAnalyzer();
            AnalysisContext analysisContext = new AnalysisContext(fieldType, analyzer, termsToMatch);
            Collection<Object> fieldValues = document.getFieldValues(name);
            NamedList<NamedList<? extends Object>> indexTokens = new SimpleOrderedMap<>();
            for (Object fieldValue : fieldValues) {
                indexTokens.add(String.valueOf(fieldValue), analyzeValue(fieldValue.toString(), analysisContext));
            }
            fieldTokens.add("index", indexTokens);
        }
    }
    return result;
}
Also used : NamedList(org.apache.solr.common.util.NamedList) Analyzer(org.apache.lucene.analysis.Analyzer) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrException(org.apache.solr.common.SolrException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BytesRef(org.apache.lucene.util.BytesRef)

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