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

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

Introduction

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

Prototype

public BytesRef(CharSequence text) 

Source Link

Document

Initialize the byte[] from the UTF8 bytes for the provided String.

Usage

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

License:Apache License

@Test
public void testEvaluateWithTimezone() throws Exception {
    assertEvaluate("date_format(time_format, timezone, timestamp)",
            new BytesRef("Sun Jan 1 1st 01 1 000000 04 04 04 00 001 4 4 January 01 AM 04:00:00 AM "
                    + "00 00 04:00:00 01 00 01 52 Sunday 0 1871 1870 1871 71"),
            Literal.of("%a %b %c %D %d %e %f %H %h %I %i %j %k %l %M %m %p %r "
                    + "%S %s %T %U %u %V %v %W %w %X %x %Y %y"),
            Literal.of("EST"),
            Literal.of(DataTypes.TIMESTAMP, DataTypes.TIMESTAMP.value("1871-01-01T09:00:00.000Z")));
}

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

License:Apache License

@SafeVarargs
@Override/*from ww  w .j  a v a 2 s .c  om*/
public final BytesRef evaluate(Input<Object>... args) {
    assert args.length > 1 : "number of args must be > 1";
    Object arg0Value = args[0].value();
    assert arg0Value != null : "1st argument must not be null";

    Object[] values = new Object[args.length - 1];
    for (int i = 0; i < args.length - 1; i++) {
        Object value = args[i + 1].value();
        if (value instanceof BytesRef) {
            values[i] = ((BytesRef) value).utf8ToString();
        } else {
            values[i] = value;
        }
    }

    return new BytesRef(String.format(Locale.ENGLISH, ((BytesRef) arg0Value).utf8ToString(), values));
}

From source file:io.crate.expression.scalar.regex.MatchesFunction.java

License:Apache License

@Override
public BytesRef[] evaluate(Input[] args) {
    assert args.length == 2 || args.length == 3 : "number of args must be 2 or 3";
    Object val = args[0].value();
    final Object patternValue = args[1].value();
    if (val == null || patternValue == null) {
        return null;
    }/*from  w w w  .  j av  a2 s .c  o m*/
    assert patternValue instanceof BytesRef : "patternValue must be BytesRef";
    // value can be a string if e.g. result is retrieved by ESSearchTask
    if (val instanceof String) {
        val = new BytesRef((String) val);
    }

    RegexMatcher matcher;
    if (regexMatcher == null) {
        String pattern = ((BytesRef) patternValue).utf8ToString();
        BytesRef flags = null;
        if (args.length == 3) {
            flags = (BytesRef) args[2].value();
        }
        matcher = new RegexMatcher(pattern, flags);
    } else {
        matcher = regexMatcher;
    }

    if (matcher.match((BytesRef) val)) {
        return matcher.groups();
    }
    return null;
}

From source file:io.crate.expression.scalar.regex.MatchesFunctionTest.java

License:Apache License

@Test
public void testEvaluateWithCompile() throws Exception {
    BytesRef[] expected = new BytesRef[] { new BytesRef("ba") };
    assertEvaluate("regexp_matches(name, '.*(ba).*')", expected, Literal.of("foobarbequebaz bar"));
}

From source file:io.crate.expression.scalar.regex.MatchesFunctionTest.java

License:Apache License

@Test
public void testEvaluate() throws Exception {
    BytesRef[] expected = new BytesRef[] { new BytesRef("ba") };
    assertEvaluate("regexp_matches(name, regex_pattern)", expected, Literal.of("foobarbequebaz bar"),
            Literal.of(".*(ba).*"));
}

From source file:io.crate.expression.scalar.regex.MatchesFunctionTest.java

License:Apache License

@Test
public void testEvaluateWithFlags() throws Exception {
    BytesRef[] expected = new BytesRef[] { new BytesRef("ba") };
    assertEvaluate("regexp_matches(name, regex_pattern, 'us')", expected, Literal.of("foobarbequebaz bar"),
            Literal.of(".*(ba).*"));
}

From source file:io.crate.expression.scalar.regex.MatchesFunctionTest.java

License:Apache License

@Test
public void testNormalizeSymbol() throws Exception {
    assertNormalize("regexp_matches('foobarbequebaz bar', '.*(ba).*')",
            isLiteral(new BytesRef[] { new BytesRef("ba") }, new ArrayType(DataTypes.STRING)));
}

From source file:io.crate.expression.scalar.regex.MatchesFunctionTest.java

License:Apache License

@Test
public void testNormalizeSymbolWithFlags() throws Exception {
    assertNormalize("regexp_matches('foobarbequebaz bar', '.*(ba).*', 'us')",
            isLiteral(new BytesRef[] { new BytesRef("ba") }, new ArrayType(DataTypes.STRING)));
}

From source file:io.crate.expression.scalar.regex.RegexMatcher.java

License:Apache License

@Nullable
public BytesRef[] groups() {
    try {/*  w w w  .j a  v  a 2  s. c  om*/
        if (matcher.groupCount() == 0) {
            return new BytesRef[] { new BytesRef(matcher.group()) };
        }
        BytesRef[] groups = new BytesRef[matcher.groupCount()];
        // skip first group (the original string)
        for (int i = 1; i <= matcher.groupCount(); i++) {
            String group = matcher.group(i);
            if (group != null) {
                groups[i - 1] = new BytesRef(group);
            } else {
                groups[i - 1] = null;
            }
        }
        return groups;
    } catch (IllegalStateException e) {
        // no match -> no groups
    }
    return null;
}

From source file:io.crate.expression.scalar.regex.RegexMatcher.java

License:Apache License

public BytesRef replace(BytesRef term, String replacement) {
    utf8toUtf16(term, utf16);//w w w  .  jav  a  2s. co m
    if (globalFlag) {
        return new BytesRef(matcher.replaceAll(replacement));
    } else {
        return new BytesRef(matcher.replaceFirst(replacement));
    }
}