use of com.yahoo.prelude.query.WeightedSetItem in project vespa by vespa-engine.
the class WeightedSetItemTestCase method testTokenAPI.
@Test
public void testTokenAPI() {
WeightedSetItem ws = new WeightedSetItem("index");
assertEquals(0, ws.getNumTokens());
assertNull(ws.getTokenWeight("bogus"));
// insert tokens
assertEquals(new Integer(1), ws.addToken("foo"));
assertEquals(new Integer(2), ws.addToken("bar", 2));
assertEquals(new Integer(3), ws.addToken("baz", 3));
// check state
assertEquals(3, ws.getNumTokens());
assertEquals(new Integer(1), ws.getTokenWeight("foo"));
assertEquals(new Integer(2), ws.getTokenWeight("bar"));
assertEquals(new Integer(3), ws.getTokenWeight("baz"));
// add duplicate tokens
assertEquals(new Integer(2), ws.addToken("foo", 2));
assertEquals(new Integer(3), ws.addToken("baz", 2));
// check state
assertEquals(3, ws.getNumTokens());
assertEquals(new Integer(2), ws.getTokenWeight("foo"));
assertEquals(new Integer(2), ws.getTokenWeight("bar"));
assertEquals(new Integer(3), ws.getTokenWeight("baz"));
// remove token
assertEquals(new Integer(2), ws.removeToken("bar"));
assertEquals(2, ws.getNumTokens());
assertNull(ws.getTokenWeight("bar"));
// remove non-existing token
assertNull(ws.removeToken("bogus"));
assertEquals(2, ws.getNumTokens());
}
use of com.yahoo.prelude.query.WeightedSetItem in project vespa by vespa-engine.
the class QueryTestCase method testEncodeQueryPacketWithLabelsConnectivityAndSignificance.
@Test
public void testEncodeQueryPacketWithLabelsConnectivityAndSignificance() {
Query query = new Query();
AndItem and = new AndItem();
WeightedSetItem taggable1 = new WeightedSetItem("field1");
taggable1.setLabel("foo");
WeightedSetItem taggable2 = new WeightedSetItem("field2");
taggable1.setLabel("bar");
and.addItem(taggable1);
and.addItem(taggable2);
WordItem word1 = new WordItem("word1", "field3");
word1.setSignificance(0.37);
WordItem word2 = new WordItem("word1", "field3");
word2.setSignificance(0.81);
word2.setConnectivity(word1, 0.15);
and.addItem(word1);
and.addItem(word2);
query.getModel().getQueryTree().setRoot(and);
query.prepare();
QueryPacket packet = QueryPacket.create(query);
byte[] encoded = packetToBytes(packet);
byte[] correctBuffer = new byte[] { 0, 0, 1, 16, 0, 0, 0, -38, 0, 0, 0, 0, 0, 16, 0, 6, 0, 10, ignored, ignored, ignored, ignored, 0, 0, 0x40, 0x03, 7, 'd', 'e', 'f', 'a', 'u', 'l', 't', 0, 0, 0, 1, 0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 5, 0, 0, 0, 18, 'v', 'e', 's', 'p', 'a', 46, 'l', 'a', 'b', 'e', 'l', 46, 'b', 'a', 'r', 46, 'i', 'd', 0, 0, 0, 1, '1', 0, 0, 0, 22, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 'c', 'o', 'n', 'n', 'e', 'x', 'i', 't', 'y', 0, 0, 0, 1, '3', 0, 0, 0, 22, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 'c', 'o', 'n', 'n', 'e', 'x', 'i', 't', 'y', 0, 0, 0, 4, '0', 46, '1', '5', 0, 0, 0, 25, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '3', 46, 's', 'i', 'g', 'n', 'i', 'f', 'i', 'c', 'a', 'n', 'c', 'e', 0, 0, 0, 4, '0', 46, '3', '7', 0, 0, 0, 25, 'v', 'e', 's', 'p', 'a', 46, 't', 'e', 'r', 'm', 46, '4', 46, 's', 'i', 'g', 'n', 'i', 'f', 'i', 'c', 'a', 'n', 'c', 'e', 0, 0, 0, 4, '0', 46, '8', '1', 0, 0, 0, 5, 0, 0, 0, '4', 1, 4, 79, 1, 0, 6, 'f', 'i', 'e', 'l', 'd', '1', 79, 2, 0, 6, 'f', 'i', 'e', 'l', 'd', '2', 68, 3, 6, 'f', 'i', 'e', 'l', 'd', '3', 5, 'w', 'o', 'r', 'd', '1', 68, 4, 6, 'f', 'i', 'e', 'l', 'd', '3', 5, 'w', 'o', 'r', 'd', 49 };
assertEqualArrays(correctBuffer, encoded);
}
use of com.yahoo.prelude.query.WeightedSetItem in project vespa by vespa-engine.
the class WeightedSetItemTestCase method testEncoding.
@Test
public void testEncoding() {
WeightedSetItem item = new WeightedSetItem("index");
// need 2 alternative reference encoding, as the encoding
// order is kept undefined to improve performance.
FakeWSItem ref1 = new FakeWSItem();
FakeWSItem ref2 = new FakeWSItem();
item.addToken("foo", 10);
item.addToken("bar", 20);
ref1.add("foo", 10);
ref1.add("bar", 20);
ref2.add("bar", 20);
ref2.add("foo", 10);
ByteBuffer actual = ByteBuffer.allocate(128);
ByteBuffer expect1 = ByteBuffer.allocate(128);
ByteBuffer expect2 = ByteBuffer.allocate(128);
expect1.put((byte) 15).put((byte) 2);
Item.putString("index", expect1);
new PureWeightedString("foo", 10).encode(expect1);
new PureWeightedString("bar", 20).encode(expect1);
expect2.put((byte) 15).put((byte) 2);
Item.putString("index", expect2);
new PureWeightedString("bar", 20).encode(expect2);
new PureWeightedString("foo", 10).encode(expect2);
assertEquals(3, item.encode(actual));
actual.flip();
expect1.flip();
expect2.flip();
if (actual.equals(expect1)) {
assertFalse(actual.equals(expect2));
} else {
assertTrue(actual.equals(expect2));
}
}
Aggregations