Example usage for org.apache.lucene.analysis TokenStream reflectWith

List of usage examples for org.apache.lucene.analysis TokenStream reflectWith

Introduction

In this page you can find the example usage for org.apache.lucene.analysis TokenStream reflectWith.

Prototype

public final void reflectWith(AttributeReflector reflector) 

Source Link

Document

This method is for introspection of attributes, it should simply add the key/values this AttributeSource holds to the given AttributeReflector .

Usage

From source file:info.johtani.elasticsearch.action.admin.indices.extended.analyze.TransportExtendedAnalyzeAction.java

License:Apache License

/**
 * other attribute extract object.<br/>
 * Extracted object group by AttributeClassName
 *
 * @param stream current TokenStream//from ww  w. j ava 2  s  . co m
 * @param includeAttributes filtering attributes
 * @param shortAttrName if true, return short attribute name
 * @return Nested Object : Map<attrClass, Map<key, value>>
 */
private Map<String, Map<String, Object>> extractExtendedAttributes(TokenStream stream,
        final Set<String> includeAttributes, final boolean shortAttrName) {
    final Map<String, Map<String, Object>> extendedAttributes = new TreeMap<>();

    stream.reflectWith(new AttributeReflector() {
        @Override
        public void reflect(Class<? extends Attribute> attClass, String key, Object value) {
            if (CharTermAttribute.class.isAssignableFrom(attClass))
                return;
            if (PositionIncrementAttribute.class.isAssignableFrom(attClass))
                return;
            if (OffsetAttribute.class.isAssignableFrom(attClass))
                return;
            if (TypeAttribute.class.isAssignableFrom(attClass))
                return;
            if (includeAttributes == null || includeAttributes.isEmpty()
                    || includeAttributes.contains(attClass.getSimpleName().toLowerCase())) {
                Map<String, Object> currentAttributes = extendedAttributes.get(attClass.getName());
                if (currentAttributes == null) {
                    currentAttributes = new HashMap<>();
                }

                if (value instanceof BytesRef) {
                    final BytesRef p = (BytesRef) value;
                    value = p.toString();
                }
                currentAttributes.put(key, value);
                if (shortAttrName) {
                    extendedAttributes.put(
                            attClass.getName().substring(attClass.getName().lastIndexOf(".") + 1),
                            currentAttributes);
                } else {
                    extendedAttributes.put(attClass.getName(), currentAttributes);
                }
            }
        }
    });

    return extendedAttributes;
}

From source file:org.apache.solr.handler.AnalysisRequestHandler.java

License:Apache License

static NamedList<NamedList<Object>> getTokens(TokenStream tstream) throws IOException {
    // outer is namedList since order of tokens is important
    NamedList<NamedList<Object>> tokens = new NamedList<NamedList<Object>>();

    while (tstream.incrementToken()) {
        final NamedList<Object> token = new SimpleOrderedMap<Object>();
        tokens.add("token", token);
        tstream.reflectWith(new AttributeReflector() {
            public void reflect(Class<? extends Attribute> attClass, String key, Object value) {
                String k = attClass.getName() + '#' + key;
                // map keys for "standard attributes":
                if (ATTRIBUTE_MAPPING.containsKey(k)) {
                    k = ATTRIBUTE_MAPPING.get(k);
                }/*from  w w w.j av  a2  s.  c  o  m*/
                token.add(k, value);
            }
        });
    }
    return tokens;
}