Example usage for org.apache.lucene.util BytesRef toString

List of usage examples for org.apache.lucene.util BytesRef toString

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns hex encoded bytes, eg [0x6c 0x75 0x63 0x65 0x6e 0x65]

Usage

From source file:com.rocana.lucene.codec.v1.RocanaBlockTreeTermsReader.java

License:Apache License

String brToString(BytesRef b) {
    if (b == null) {
        return "null";
    } else {//from w  w  w  .  j a va  2s . c  o  m
        try {
            return b.utf8ToString() + " " + b;
        } catch (Throwable t) {
            // If BytesRef isn't actually UTF8, or it's eg a
            // prefix of UTF8 that ends mid-unicode-char, we
            // fallback to hex:
            return b.toString();
        }
    }
}

From source file:com.rocana.lucene.codec.v1.RocanaIntersectTermsEnum.java

License:Apache License

@SuppressWarnings("unused")
static String brToString(BytesRef b) {
    try {/*from w w w.ja va 2 s  .  c om*/
        return b.utf8ToString() + " " + b;
    } catch (Throwable t) {
        // If BytesRef isn't actually UTF8, or it's eg a
        // prefix of UTF8 that ends mid-unicode-char, we
        // fallback to hex:
        return b.toString();
    }
}

From source file:com.stratio.cassandra.lucene.service.TokenMapperGenericTest.java

License:Apache License

@Test
public void testValue() {
    DecoratedKey key = decoratedKey("key");
    Token token = key.getToken();//from ww  w  . j  a v a 2s .  c  o m
    BytesRef value = mapper.bytesRef(token);
    assertNotNull("Value is wrong", value);
    assertEquals("Value is wrong", "[3c 6e b 8a 9c 15 22 4a 82 28 b9 a9 8c a1 53 1d]", value.toString());
}

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   w ww.  j a v a2  s. c o 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.AnalysisRequestHandlerBase.java

License:Apache License

/**
 * Converts the list of Tokens to a list of NamedLists representing the tokens.
 *
 * @param tokenList  Tokens to convert/* w  w  w  .  j av a 2 s  . c o  m*/
 * @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<NamedList>();
    final FieldType fieldType = context.getFieldType();
    final AttributeSource[] tokens = tokenList.toArray(new AttributeSource[tokenList.size()]);

    // sort the tokens by absoulte 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<Object>();
        final TermToBytesRefAttribute termAtt = token.getAttribute(TermToBytesRefAttribute.class);
        BytesRef rawBytes = termAtt.getBytesRef();
        termAtt.fillBytesRef();
        final String text = fieldType.indexedToReadable(rawBytes, new CharsRef(rawBytes.length)).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;
}

From source file:org.apache.solr.search.QueryParsing.java

License:Apache License

/**
 * @see #toString(Query,IndexSchema)// w w  w  .ja  v  a 2s  .  c o  m
 */
public static void toString(Query query, IndexSchema schema, Appendable out, int flags) throws IOException {
    boolean writeBoost = true;

    if (query instanceof TermQuery) {
        TermQuery q = (TermQuery) query;
        Term t = q.getTerm();
        FieldType ft = writeFieldName(t.field(), schema, out, flags);
        writeFieldVal(t.bytes(), ft, out, flags);
    } else if (query instanceof TermRangeQuery) {
        TermRangeQuery q = (TermRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesLower() ? '[' : '{');
        BytesRef lt = q.getLowerTerm();
        BytesRef ut = q.getUpperTerm();
        if (lt == null) {
            out.append('*');
        } else {
            writeFieldVal(lt, ft, out, flags);
        }

        out.append(" TO ");

        if (ut == null) {
            out.append('*');
        } else {
            writeFieldVal(ut, ft, out, flags);
        }

        out.append(q.includesUpper() ? ']' : '}');
    } else if (query instanceof NumericRangeQuery) {
        NumericRangeQuery q = (NumericRangeQuery) query;
        String fname = q.getField();
        FieldType ft = writeFieldName(fname, schema, out, flags);
        out.append(q.includesMin() ? '[' : '{');
        Number lt = q.getMin();
        Number ut = q.getMax();
        if (lt == null) {
            out.append('*');
        } else {
            out.append(lt.toString());
        }

        out.append(" TO ");

        if (ut == null) {
            out.append('*');
        } else {
            out.append(ut.toString());
        }

        out.append(q.includesMax() ? ']' : '}');
    } else if (query instanceof BooleanQuery) {
        BooleanQuery q = (BooleanQuery) query;
        boolean needParens = false;

        if (q.getBoost() != 1.0 || q.getMinimumNumberShouldMatch() != 0 || q.isCoordDisabled()) {
            needParens = true;
        }
        if (needParens) {
            out.append('(');
        }
        boolean first = true;
        for (BooleanClause c : q.clauses()) {
            if (!first) {
                out.append(' ');
            } else {
                first = false;
            }

            if (c.isProhibited()) {
                out.append('-');
            } else if (c.isRequired()) {
                out.append('+');
            }
            Query subQuery = c.getQuery();
            boolean wrapQuery = false;

            // TODO: may need to put parens around other types
            // of queries too, depending on future syntax.
            if (subQuery instanceof BooleanQuery) {
                wrapQuery = true;
            }

            if (wrapQuery) {
                out.append('(');
            }

            toString(subQuery, schema, out, flags);

            if (wrapQuery) {
                out.append(')');
            }
        }

        if (needParens) {
            out.append(')');
        }
        if (q.getMinimumNumberShouldMatch() > 0) {
            out.append('~');
            out.append(Integer.toString(q.getMinimumNumberShouldMatch()));
        }
        if (q.isCoordDisabled()) {
            out.append("/no_coord");
        }

    } else if (query instanceof PrefixQuery) {
        PrefixQuery q = (PrefixQuery) query;
        Term prefix = q.getPrefix();
        FieldType ft = writeFieldName(prefix.field(), schema, out, flags);
        out.append(prefix.text());
        out.append('*');
    } else if (query instanceof WildcardQuery) {
        out.append(query.toString());
        writeBoost = false;
    } else if (query instanceof FuzzyQuery) {
        out.append(query.toString());
        writeBoost = false;
    } else if (query instanceof ConstantScoreQuery) {
        out.append(query.toString());
        writeBoost = false;
    } else if (query instanceof WrappedQuery) {
        WrappedQuery q = (WrappedQuery) query;
        out.append(q.getOptions());
        toString(q.getWrappedQuery(), schema, out, flags);
        writeBoost = false; // we don't use the boost on wrapped queries
    } else {
        out.append(query.getClass().getSimpleName() + '(' + query.toString() + ')');
        writeBoost = false;
    }

    if (writeBoost && query.getBoost() != 1.0f) {
        out.append("^");
        out.append(Float.toString(query.getBoost()));
    }

}