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.operation.language.JavascriptUserDefinedFunctionTest.java

@Test
public void testEvaluateBytesRefInObjectIsConvertedToString() throws Exception {
    registerUserDefinedFunction("f", DataTypes.OBJECT, ImmutableList.of(DataTypes.OBJECT),
            "function f(o) { return {'key1' : o['inner']['key1'][0], 'key2': o['inner']['key2']}; }");
    // the map will be modified
    Map<String, Object> inner = new HashMap<>();
    inner.put("key1", new Object[] { new BytesRef("bar") });
    inner.put("key2", new BytesRef("foo"));
    assertEvaluate("f(obj)", ImmutableMap.of("key1", "bar", "key2", "foo"),
            Literal.of(ImmutableMap.of("inner", inner)));
}

From source file:io.crate.operation.language.JavascriptUserDefinedFunctionTest.java

@Test
public void testEvaluateBytesRefInArrayIsConvertedToString() throws Exception {
    registerUserDefinedFunction("f", DataTypes.STRING,
            ImmutableList.of(new ArrayType(new ArrayType(DataTypes.STRING))),
            "function f(arr) { return arr[0][0]; }");
    assertEvaluate("f(array_string_array)", "foo",
            Literal.of(new Object[][] { new Object[] { new BytesRef("foo") } },
                    new ArrayType(new ArrayType(DataTypes.STRING))));
}

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

License:Apache License

private static Symbol normalizeSymbol(String pattern, String... expressions) {
    Literal patternLiteral = Literal.newLiteral(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }/*from  w ww .  j a v  a 2 s.  co  m*/
    Literal valuesLiteral = Literal.newLiteral(new ArrayType(DataTypes.STRING), value);
    AnyLikeOperator impl = (AnyLikeOperator) new AnyLikeOperator.AnyLikeResolver()
            .getForTypes(Arrays.asList(patternLiteral.valueType(), valuesLiteral.valueType()));

    Function function = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    return impl.normalizeSymbol(function);
}

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

License:Apache License

private Boolean anyLike(String pattern, String... expressions) {
    Literal patternLiteral = Literal.newLiteral(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }/*from   ww w. j  a v a 2 s .  c om*/
    Literal valuesLiteral = Literal.newLiteral(new ArrayType(DataTypes.STRING), value);
    AnyLikeOperator impl = (AnyLikeOperator) new AnyLikeOperator.AnyLikeResolver()
            .getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));

    return impl.evaluate(patternLiteral, valuesLiteral);
}

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

License:Apache License

@Test
public void testNegateLike() throws Exception {
    Literal patternLiteral = Literal.newLiteral("A");
    Literal valuesLiteral = Literal.newLiteral(new ArrayType(DataTypes.STRING),
            new Object[] { new BytesRef("A"), new BytesRef("B") });
    FunctionImplementation<Function> impl = new AnyLikeOperator.AnyLikeResolver()
            .getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));
    Function anyLikeFunction = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    Input<Boolean> normalized = (Input<Boolean>) impl.normalizeSymbol(anyLikeFunction);
    assertThat(normalized.value(), is(true));
    assertThat(new NotPredicate().evaluate(normalized), is(false));
}

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

License:Apache License

private static Symbol normalizeSymbol(String pattern, String... expressions) {
    Literal patternLiteral = Literal.newLiteral(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }//from w  ww  .j a  va 2 s  .c  om
    Literal valuesLiteral = Literal.newLiteral(new ArrayType(DataTypes.STRING), value);
    AnyNotLikeOperator impl = (AnyNotLikeOperator) new AnyNotLikeOperator.AnyNotLikeResolver()
            .getForTypes(Arrays.asList(patternLiteral.valueType(), valuesLiteral.valueType()));

    Function function = new Function(impl.info(), Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    return impl.normalizeSymbol(function);
}

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

License:Apache License

private Boolean anyNotLike(String pattern, String... expressions) {
    Literal patternLiteral = Literal.newLiteral(pattern);
    Object[] value = new Object[expressions.length];
    for (int i = 0; i < expressions.length; i++) {
        value[i] = expressions[i] == null ? null : new BytesRef(expressions[i]);
    }/*from w  ww. j  a  v  a  2 s  . co m*/
    Literal valuesLiteral = Literal.newLiteral(new ArrayType(DataTypes.STRING), value);
    AnyNotLikeOperator impl = (AnyNotLikeOperator) new AnyNotLikeOperator.AnyNotLikeResolver()
            .getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));

    return impl.evaluate(patternLiteral, valuesLiteral);
}

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

License:Apache License

@Test
public void testNegateNotLike() throws Exception {
    Literal patternLiteral = Literal.newLiteral("A");
    Literal valuesLiteral = Literal.newLiteral(new ArrayType(DataTypes.STRING),
            new Object[] { new BytesRef("A"), new BytesRef("B") });
    FunctionImplementation<Function> impl = new AnyNotLikeOperator.AnyNotLikeResolver()
            .getForTypes(Arrays.asList(DataTypes.STRING, valuesLiteral.valueType()));
    Function anyNotLikeFunction = new Function(impl.info(),
            Arrays.<Symbol>asList(patternLiteral, valuesLiteral));
    Input<Boolean> normalized = (Input<Boolean>) impl.normalizeSymbol(anyNotLikeFunction);
    assertThat(normalized.value(), is(true));
    assertThat(new NotPredicate().evaluate(normalized), is(false));
}

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

License:Apache License

@Test
public void testNormalizeSymbolSetLiteralStringIncluded() {
    Literal inValue = Literal.newLiteral("charlie");
    Literal inListValues = Literal.newLiteral(STRING_SET_TYPE, Sets.newHashSet(new BytesRef("alpha"),
            new BytesRef("bravo"), new BytesRef("charlie"), new BytesRef("delta")));

    List<Symbol> arguments = new ArrayList<>();
    arguments.add(inValue);//from  w ww.  j av  a2s  .  c o m
    arguments.add(inListValues);

    InOperator op = new InOperator(Operator.generateInfo(InOperator.NAME, DataTypes.INTEGER));
    Function function = new Function(op.info(), arguments);
    Symbol result = op.normalizeSymbol(function);

    assertThat(result, isLiteral(true));
}

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

License:Apache License

@Test
public void testNormalizeSymbolSetLiteralStringNotIncluded() {
    Literal inValue = Literal.newLiteral("not included");
    Literal inListValues = Literal.newLiteral(STRING_SET_TYPE, Sets.newHashSet(new BytesRef("alpha"),
            new BytesRef("bravo"), new BytesRef("charlie"), new BytesRef("delta")));

    List<Symbol> arguments = new ArrayList<>();
    arguments.add(inValue);/*from w  ww .  j  a v a  2 s  .  c  o  m*/
    arguments.add(inListValues);

    InOperator op = new InOperator(Operator.generateInfo(InOperator.NAME, DataTypes.INTEGER));
    Function function = new Function(op.info(), arguments);
    Symbol result = op.normalizeSymbol(function);

    assertThat(result, isLiteral(false));
}