Search in sources :

Example 1 with WeightedSetDataType

use of com.yahoo.document.WeightedSetDataType in project vespa by vespa-engine.

the class ToWsetTestCase method assertConvert.

private static void assertConvert(boolean createIfNonExistent, boolean removeIfZero) {
    ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
    ctx.setValue(new StringFieldValue("69")).execute(new ToWsetExpression(createIfNonExistent, removeIfZero));
    FieldValue val = ctx.getValue();
    assertEquals(WeightedSet.class, val.getClass());
    WeightedSet wset = (WeightedSet) val;
    WeightedSetDataType type = wset.getDataType();
    assertEquals(DataType.STRING, type.getNestedType());
    assertEquals(createIfNonExistent, type.createIfNonExistent());
    assertEquals(removeIfZero, type.removeIfZero());
    assertEquals(1, wset.size());
    assertEquals(Integer.valueOf(1), wset.get(new StringFieldValue("69")));
}
Also used : SimpleTestAdapter(com.yahoo.vespa.indexinglanguage.SimpleTestAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 2 with WeightedSetDataType

use of com.yahoo.document.WeightedSetDataType in project vespa by vespa-engine.

the class DocumentGenTest method testLocalApp.

@Test
public void testLocalApp() throws MojoFailureException {
    DocumentGenMojo mojo = new DocumentGenMojo();
    mojo.execute(new File("etc/localapp/"), new File("target/generated-test-sources/vespa-documentgen-plugin/"), "com.yahoo.vespa.document");
    Map<String, Search> searches = mojo.getSearches();
    assertEquals(searches.get("video").getDocument("video").getField("weight").getDataType(), DataType.FLOAT);
    assertTrue(searches.get("book").getDocument("book").getField("mystruct").getDataType() instanceof StructDataType);
    assertTrue(searches.get("book").getDocument("book").getField("mywsfloat").getDataType() instanceof WeightedSetDataType);
    assertTrue(((WeightedSetDataType) (searches.get("book").getDocument("book").getField("mywsfloat").getDataType())).getNestedType() == DataType.FLOAT);
}
Also used : StructDataType(com.yahoo.document.StructDataType) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) Search(com.yahoo.searchdefinition.Search) File(java.io.File) Test(org.junit.Test)

Example 3 with WeightedSetDataType

use of com.yahoo.document.WeightedSetDataType in project vespa by vespa-engine.

the class MapValueUpdate method applyTo.

@Override
public FieldValue applyTo(FieldValue fval) {
    if (fval instanceof Array) {
        Array array = (Array) fval;
        FieldValue element = array.getFieldValue(((IntegerFieldValue) value).getInteger());
        element = update.applyTo(element);
        array.set(((IntegerFieldValue) value).getInteger(), element);
    } else if (fval instanceof WeightedSet) {
        WeightedSet wset = (WeightedSet) fval;
        WeightedSetDataType wtype = wset.getDataType();
        Integer weight = wset.get(value);
        if (weight == null) {
            if (wtype.createIfNonExistent() && update instanceof ArithmeticValueUpdate) {
                weight = 0;
            } else {
                return fval;
            }
        }
        weight = (Integer) update.applyTo(new IntegerFieldValue(weight)).getWrappedValue();
        wset.put(value, weight);
        if (wtype.removeIfZero() && update instanceof ArithmeticValueUpdate && weight == 0) {
            wset.remove(value);
        }
    }
    return fval;
}
Also used : Array(com.yahoo.document.datatypes.Array) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 4 with WeightedSetDataType

use of com.yahoo.document.WeightedSetDataType in project vespa by vespa-engine.

the class ArraysWeightedSetsTestCase method testArrayWeightedSetsImporting.

@Test
public void testArrayWeightedSetsImporting() throws java.io.IOException, com.yahoo.searchdefinition.parser.ParseException {
    Search search = SearchBuilder.buildFromFile("src/test/examples/arraysweightedsets.sd");
    SDField tags = (SDField) search.getDocument().getField("tags");
    assertTrue(tags.getDataType() instanceof ArrayDataType);
    assertEquals(DataType.STRING, ((CollectionDataType) tags.getDataType()).getNestedType());
    SDField ratings = (SDField) search.getDocument().getField("ratings");
    assertTrue(ratings.getDataType() instanceof ArrayDataType);
    assertEquals(DataType.INT, ((CollectionDataType) ratings.getDataType()).getNestedType());
    SDField flags = (SDField) search.getDocument().getField("flags");
    assertTrue(flags.getDataType() instanceof WeightedSetDataType);
    assertEquals(DataType.STRING, ((CollectionDataType) flags.getDataType()).getNestedType());
    SDField banners = (SDField) search.getDocument().getField("banners");
    assertTrue(banners.getDataType() instanceof WeightedSetDataType);
    assertEquals(DataType.INT, ((CollectionDataType) banners.getDataType()).getNestedType());
}
Also used : SDField(com.yahoo.searchdefinition.document.SDField) WeightedSetDataType(com.yahoo.document.WeightedSetDataType) ArrayDataType(com.yahoo.document.ArrayDataType) Test(org.junit.Test)

Example 5 with WeightedSetDataType

use of com.yahoo.document.WeightedSetDataType in project vespa by vespa-engine.

the class WeightedSetOperation method apply.

public void apply(SDField field) {
    WeightedSetDataType ctype = (WeightedSetDataType) field.getDataType();
    if (createIfNonExistent != null) {
        field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), createIfNonExistent, ctype.removeIfZero()));
    }
    ctype = (WeightedSetDataType) field.getDataType();
    if (removeIfZero != null) {
        field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), ctype.createIfNonExistent(), removeIfZero));
    }
    ctype = (WeightedSetDataType) field.getDataType();
    for (Object o : field.getAttributes().values()) {
        Attribute attribute = (Attribute) o;
        attribute.setRemoveIfZero(ctype.removeIfZero());
        attribute.setCreateIfNonExistent(ctype.createIfNonExistent());
    }
}
Also used : Attribute(com.yahoo.searchdefinition.document.Attribute) WeightedSetDataType(com.yahoo.document.WeightedSetDataType)

Aggregations

WeightedSetDataType (com.yahoo.document.WeightedSetDataType)12 ArrayDataType (com.yahoo.document.ArrayDataType)5 FieldValue (com.yahoo.document.datatypes.FieldValue)5 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)5 StructDataType (com.yahoo.document.StructDataType)4 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)4 DataType (com.yahoo.document.DataType)3 Field (com.yahoo.document.Field)3 WeightedSet (com.yahoo.document.datatypes.WeightedSet)3 Test (org.junit.Test)3 DocumentType (com.yahoo.document.DocumentType)2 MapDataType (com.yahoo.document.MapDataType)2 PositionDataType (com.yahoo.document.PositionDataType)2 TensorDataType (com.yahoo.document.TensorDataType)2 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)2 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)2 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)2 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)2 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)2 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)2