use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class DocumentTestCase method getSertestDocument.
private Document getSertestDocument() {
Document doc = new Document(docMan.getDocumentType("sertest"), new DocumentId("doc:sertest:foobar"));
doc.setFieldValue("mailid", "emailfromalicetobob");
// 03/13/06 11:00:00
doc.setFieldValue("date", -2013512400);
doc.setFieldValue("attachmentcount", 2);
byte[] rawBytes = new byte[100];
for (int i = 0; i < rawBytes.length; i++) {
rawBytes[i] = (byte) i;
}
doc.setFieldValue("rawfield", new Raw(ByteBuffer.wrap(rawBytes)));
Document docInDoc = new Document(docMan.getDocumentType("docindoc"), new DocumentId("doc:sertest:inserted"));
docInDoc.setFieldValue("tull", "ball");
doc.setFieldValue("docindoc", docInDoc);
WeightedSet<StringFieldValue> wset = new WeightedSet<>(DataType.getWeightedSet(DataType.STRING));
wset.put(new StringFieldValue("this is a test"), 5);
wset.put(new StringFieldValue("this is another test, blah blah"), 10);
doc.setFieldValue("weightedfield", wset);
MapFieldValue<StringFieldValue, StringFieldValue> map = new MapFieldValue<>(new MapDataType(DataType.STRING, DataType.STRING));
map.put(new StringFieldValue("foo1"), new StringFieldValue("bar1"));
map.put(new StringFieldValue("foo2"), new StringFieldValue("bar2"));
doc.setFieldValue("mapfield", map);
return doc;
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class DocumentTestCase method testModifyDocument.
@Test
public void testModifyDocument() {
Document doc = new Document(testDocType, new DocumentId("doc:ns:testdoc"));
doc.setFieldValue("primitive1", 1);
Struct l1s1 = new Struct(doc.getField("l1s1").getDataType());
l1s1.setFieldValue("primitive1", 2);
Struct l2s1 = new Struct(doc.getField("struct2").getDataType());
l2s1.setFieldValue("primitive1", 3);
l2s1.setFieldValue("primitive2", 4);
Array<IntegerFieldValue> iarr1 = new Array<>(l2s1.getField("iarray").getDataType());
iarr1.add(new IntegerFieldValue(11));
iarr1.add(new IntegerFieldValue(12));
iarr1.add(new IntegerFieldValue(13));
l2s1.setFieldValue("iarray", iarr1);
ArrayDataType dt = (ArrayDataType) l2s1.getField("sarray").getDataType();
Array<Struct> sarr1 = new Array<>(dt);
{
Struct l3s1 = new Struct(dt.getNestedType());
l3s1.setFieldValue("primitive1", 1);
l3s1.setFieldValue("primitive2", 2);
sarr1.add(l3s1);
}
{
Struct l3s1 = new Struct(dt.getNestedType());
l3s1.setFieldValue("primitive1", 1);
l3s1.setFieldValue("primitive2", 2);
sarr1.add(l3s1);
}
l2s1.setFieldValue("sarray", sarr1);
MapFieldValue<StringFieldValue, StringFieldValue> smap1 = new MapFieldValue<>((MapDataType) l2s1.getField("smap").getDataType());
smap1.put(new StringFieldValue("leonardo"), new StringFieldValue("dicaprio"));
smap1.put(new StringFieldValue("ellen"), new StringFieldValue("page"));
smap1.put(new StringFieldValue("joseph"), new StringFieldValue("gordon-levitt"));
l2s1.setFieldValue("smap", smap1);
l1s1.setFieldValue("ss", l2s1.clone());
MapFieldValue<StringFieldValue, Struct> structmap1 = new MapFieldValue<>((MapDataType) l1s1.getField("structmap").getDataType());
structmap1.put(new StringFieldValue("test"), l2s1.clone());
l1s1.setFieldValue("structmap", structmap1);
WeightedSet<StringFieldValue> wset1 = new WeightedSet<>(l1s1.getField("wset").getDataType());
wset1.add(new StringFieldValue("foo"));
wset1.add(new StringFieldValue("bar"));
wset1.add(new StringFieldValue("zoo"));
l1s1.setFieldValue("wset", wset1);
Struct l2s2 = new Struct(doc.getField("struct2").getDataType());
l2s2.setFieldValue("primitive1", 5);
l2s2.setFieldValue("primitive2", 6);
WeightedSet<Struct> wset2 = new WeightedSet<>(l1s1.getField("structwset").getDataType());
wset2.add(l2s1.clone());
wset2.add(l2s2.clone());
l1s1.setFieldValue("structwset", wset2);
doc.setFieldValue("l1s1", l1s1.clone());
{
ModifyIteratorHandler handler = new ModifyIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.structmap.value.smap{leonardo}");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.smap{leonardo}");
assertEquals(new StringFieldValue("newvalue"), fv);
}
{
AddIteratorHandler handler = new AddIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
assertTrue(((Array) fv).contains(new IntegerFieldValue(32)));
assertEquals(4, ((Array) fv).size());
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray[1]");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
assertFalse(((Array) fv).contains(new Integer(12)));
assertEquals(3, ((Array) fv).size());
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.ss.iarray[$x]");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.ss.iarray");
assertEquals(0, ((Array) fv).size());
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.structmap.value.smap{leonardo}");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.structmap.value.smap");
assertFalse(((MapFieldValue) fv).contains(new StringFieldValue("leonardo")));
}
{
RemoveIteratorHandler handler = new RemoveIteratorHandler();
FieldPath path = doc.getDataType().buildFieldPath("l1s1.wset{foo}");
doc.iterateNested(path, 0, handler);
FieldValue fv = doc.getRecursiveValue("l1s1.wset");
assertFalse(((WeightedSet) fv).contains(new StringFieldValue("foo")));
}
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class ToWsetExpression method doExecute.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void doExecute(ExecutionContext ctx) {
FieldValue input = ctx.getValue();
DataType inputType = input.getDataType();
WeightedSetDataType outputType = DataType.getWeightedSet(inputType, createIfNonExistent, removeIfZero);
WeightedSet output = outputType.createFieldValue();
output.add(input);
ctx.setValue(output);
}
Aggregations