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

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

Introduction

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

Prototype

public String utf8ToString() 

Source Link

Document

Interprets stored bytes as UTF8 bytes, returning the resulting string

Usage

From source file:io.crate.expression.scalar.TimestampFormatter.java

License:Apache License

public static BytesRef format(BytesRef formatString, DateTime timestamp) {
    StringBuilder buffer = new StringBuilder(formatString.length);
    String format = formatString.utf8ToString();
    boolean percentEscape = false;
    int length = format.length();
    for (int i = 0; i < length; i++) {
        char current = format.charAt(i);

        if (current == '%') {
            if (!percentEscape) {
                percentEscape = true;//www .  j  a v  a 2s .co  m
            } else {
                buffer.append('%');
                percentEscape = false;
            }
        } else {
            if (percentEscape) {
                FormatTimestampPartFunction partFormatter = PART_FORMATTERS.get(current);
                if (partFormatter == null) {
                    buffer.append(current);
                } else {
                    buffer.append(partFormatter.format(timestamp));
                }
            } else {
                buffer.append(current);
            }
            percentEscape = false;
        }
    }
    return new BytesRef(buffer.toString());
}

From source file:io.crate.expression.scalar.TimeZoneParser.java

License:Apache License

public static DateTimeZone parseTimeZone(BytesRef timezone) throws IllegalArgumentException {
    if (timezone == null) {
        throw new IllegalArgumentException("invalid time zone value NULL");
    }/*from   w w  w .  j a  va 2s .c o m*/
    if (timezone.equals(DEFAULT_TZ_BYTES_REF)) {
        return DEFAULT_TZ;
    }

    DateTimeZone tz = TIME_ZONE_MAP.get(timezone);
    if (tz == null) {
        try {
            String text = timezone.utf8ToString();
            int index = text.indexOf(':');
            if (index != -1) {
                int beginIndex = text.charAt(0) == '+' ? 1 : 0;
                // format like -02:30
                tz = DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)),
                        Integer.parseInt(text.substring(index + 1)));
            } else {
                // id, listed here: http://joda-time.sourceforge.net/timezones.html
                // or here: http://www.joda.org/joda-time/timezones.html
                tz = DateTimeZone.forID(text);
            }
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException(
                    String.format(Locale.ENGLISH, "invalid time zone value '%s'", timezone.utf8ToString()));
        }
        TIME_ZONE_MAP.putIfAbsent(timezone, tz);
    }
    return tz;
}

From source file:io.crate.lucene.match.MatchQueries.java

License:Apache License

public static Query singleMatch(QueryShardContext queryShardContext, String fieldName, BytesRef queryString,
        @Nullable BytesRef matchType, @Nullable Map<String, Object> options) throws IOException {
    MultiMatchQueryBuilder.Type type = getType(matchType);
    ParsedOptions parsedOptions = OptionParser.parse(type, options);

    MatchQuery.Type matchQueryType = type.matchQueryType();
    MatchQuery matchQuery = new MatchQuery(queryShardContext);

    matchQuery.setAnalyzer(parsedOptions.analyzer());
    matchQuery.setCommonTermsCutoff(parsedOptions.commonTermsCutoff());
    matchQuery.setFuzziness(parsedOptions.fuzziness());
    matchQuery.setFuzzyPrefixLength(parsedOptions.prefixLength());
    matchQuery.setFuzzyRewriteMethod(parsedOptions.rewriteMethod());
    matchQuery.setMaxExpansions(parsedOptions.maxExpansions());
    matchQuery.setPhraseSlop(parsedOptions.phraseSlop());
    matchQuery.setTranspositions(parsedOptions.transpositions());
    matchQuery.setZeroTermsQuery(parsedOptions.zeroTermsQuery());

    return matchQuery.parse(matchQueryType, fieldName, queryString.utf8ToString());
}

From source file:io.crate.operation.operator.any.AbstractAnyLikeOperator.java

License:Apache License

@Override
protected Boolean doEvaluate(Object left, Iterable<?> rightIterable) {
    BytesRef rightBytesRef = (BytesRef) left;
    String pattern = rightBytesRef.utf8ToString();

    boolean hasNull = false;
    for (Object elem : rightIterable) {
        if (elem == null) {
            hasNull = true;/* w  w w .  java2s.  c  o  m*/
            continue;
        }
        assert (elem instanceof BytesRef || elem instanceof String);

        String elemValue;
        if (elem instanceof BytesRef) {
            elemValue = ((BytesRef) elem).utf8ToString();
        } else {
            elemValue = (String) elem;
        }
        if (matches(elemValue, pattern)) {
            return true;
        }
    }
    return hasNull ? null : false;
}

From source file:io.crate.operation.operator.LikeOperator.java

License:Apache License

@Override
public Boolean evaluate(Input<BytesRef>... args) {
    assert (args != null);
    assert (args.length == 2);

    BytesRef expression = args[0].value();
    BytesRef pattern = args[1].value();//from  w ww.j  a  v a 2s .  c om
    if (expression == null || pattern == null) {
        return null;
    }

    return matches(expression.utf8ToString(), pattern.utf8ToString());
}

From source file:io.crate.operation.operator.RegexpMatchOperator.java

License:Apache License

@Override
public Boolean evaluate(Input<BytesRef>... args) {
    assert args.length == 2 : "invalid number of arguments";
    BytesRef source = args[0].value();
    if (source == null) {
        return null;
    }/*from www .  jav  a 2  s  . c  om*/
    BytesRef pattern = args[1].value();
    if (pattern == null) {
        return null;
    }
    if (isPcrePattern(pattern)) {
        return source.utf8ToString().matches(pattern.utf8ToString());
    } else {
        RegExp regexp = new RegExp(pattern.utf8ToString());
        ByteRunAutomaton regexpRunAutomaton = new ByteRunAutomaton(regexp.toAutomaton());
        return regexpRunAutomaton.run(source.bytes, source.offset, source.length);
    }
}

From source file:io.crate.operation.projectors.DMLProjector.java

License:Apache License

@Override
public BatchIterator apply(BatchIterator batchIterator) {
    Supplier<ShardRequest.Item> updateItemSupplier = () -> {
        BytesRef id = (BytesRef) collectIdExpression.value();
        return itemFactory.apply(id.utf8ToString());
    };// w  w  w  .j a va 2s  . c o m
    return IndexWriterCountBatchIterator.newShardInstance(batchIterator, shardId,
            Collections.singletonList(collectIdExpression), bulkShardProcessor, updateItemSupplier);
}

From source file:io.crate.operation.scalar.ClassnamerFunctionTest.java

License:Apache License

@Test
public void testEvaluate() throws Exception {
    registerPlugin(Settings.EMPTY);//from w w  w  .  j  av a2s . c  o m

    BytesRef value = classnamerFunction.evaluate(new Input[0]);
    validateClassnamer(value.utf8ToString());
}

From source file:io.crate.operation.scalar.ClassnamerFunctionTest.java

License:Apache License

@Test
public void testNormalizeWithCustomSetting() throws Exception {
    // lets change the setting to force normalization, function will be executed only once
    // per SQL statement (same value for all rows)
    Settings.Builder builder = Settings.builder().put(ExamplePlugin.EXECUTE_PER_ROW_SETTING, false);
    registerPlugin(builder.build());/*from ww  w . j  ava  2s. c  o m*/

    Function function = new Function(classnamerFunction.info(), Collections.<Symbol>emptyList());

    Symbol symbol = classnamerFunction.normalizeSymbol(function, transactionContext);
    assertThat(symbol, instanceOf(Literal.class));

    Literal literal = (Literal) symbol;
    assertThat(literal.valueType(), instanceOf(StringType.class));
    BytesRef value = (BytesRef) literal.value();

    validateClassnamer(value.utf8ToString());
}

From source file:io.crate.operation.scalar.ConcatFunctionTest.java

License:Apache License

private void assertEval(String expected, String arg1, String arg2) {
    List<DataType> argumentTypes = Arrays.<DataType>asList(DataTypes.STRING, DataTypes.STRING);
    Scalar scalar = ((Scalar) functions.get(new FunctionIdent(ConcatFunction.NAME, argumentTypes)));

    Input[] inputs = new Input[2];
    inputs[0] = Literal.newLiteral(arg1);
    inputs[1] = Literal.newLiteral(arg2);
    @SuppressWarnings("unchecked")

    BytesRef evaluate = (BytesRef) scalar.evaluate(inputs);
    assertThat(evaluate.utf8ToString(), is(expected));
}