use of com.yahoo.search.result.StructuredData in project vespa by vespa-engine.
the class JsonRendererTestCase method testJsonObjects.
@Test
public void testJsonObjects() throws InterruptedException, ExecutionException, IOException, JSONException {
String expected = "{\n" + " \"root\": {\n" + " \"children\": [\n" + " {\n" + " \"fields\": {\n" + " \"inspectable\": {\n" + " \"a\": \"b\"\n" + " },\n" + " \"jackson\": {\n" + " \"Nineteen-eighty-four\": 1984\n" + " },\n" + " \"json producer\": {\n" + " \"long in structured\": 7809531904\n" + " },\n" + " \"org.json array\": [\n" + " true,\n" + " true,\n" + " false\n" + " ],\n" + " \"org.json object\": {\n" + " \"forty-two\": 42\n" + " }\n" + " },\n" + " \"id\": \"json objects\",\n" + " \"relevance\": 1.0\n" + " }\n" + " ],\n" + " \"fields\": {\n" + " \"totalCount\": 0\n" + " },\n" + " \"id\": \"toplevel\",\n" + " \"relevance\": 1.0\n" + " }\n" + "}\n";
Result r = newEmptyResult();
Hit h = new Hit("json objects");
JSONObject o = new JSONObject();
JSONArray a = new JSONArray();
ObjectMapper mapper = new ObjectMapper();
JsonNode j = mapper.createObjectNode();
JSONString s = new JSONString("{\"a\": \"b\"}");
Slime slime = new Slime();
Cursor c = slime.setObject();
c.setLong("long in structured", 7809531904L);
SlimeAdapter slimeInit = new SlimeAdapter(slime.get());
StructuredData struct = new StructuredData(slimeInit);
((ObjectNode) j).put("Nineteen-eighty-four", 1984);
o.put("forty-two", 42);
a.put(true);
a.put(true);
a.put(false);
h.setField("inspectable", s);
h.setField("jackson", j);
h.setField("json producer", struct);
h.setField("org.json array", a);
h.setField("org.json object", o);
r.hits().add(h);
String summary = render(r);
assertEqualJson(expected, summary);
}
use of com.yahoo.search.result.StructuredData in project vespa by vespa-engine.
the class SlimeSummaryTestCase method testDecoding.
@Test
public void testDecoding() {
Tensor tensor1 = Tensor.from("tensor(x{},y{}):{{x:foo,y:bar}:0.1}");
Tensor tensor2 = Tensor.from("tensor(x[],y[1]):{{x:0,y:0}:-0.3}");
String summary_cf = "file:src/test/java/com/yahoo/prelude/fastsearch/summary.cfg";
DocsumDefinitionSet set = createDocsumDefinitionSet(summary_cf);
byte[] docsum = makeDocsum(tensor1, tensor2);
FastHit hit = new FastHit();
assertNull(set.lazyDecode("default", docsum, hit));
assertThat(hit.getField("integer_field"), equalTo(4));
assertThat(hit.getField("short_field"), equalTo((short) 2));
assertThat(hit.getField("byte_field"), equalTo((byte) 1));
assertThat(hit.getField("float_field"), equalTo(4.5f));
assertThat(hit.getField("double_field"), equalTo(8.75));
assertThat(hit.getField("int64_field"), equalTo(8L));
assertThat(hit.getField("string_field"), equalTo("string_value"));
assertThat(hit.getField("data_field"), instanceOf(RawData.class));
assertThat(hit.getField("data_field").toString(), equalTo("data_value"));
assertThat(hit.getField("longstring_field"), equalTo((Object) "longstring_value"));
assertThat(hit.getField("longdata_field"), instanceOf(RawData.class));
assertThat(hit.getField("longdata_field").toString(), equalTo("longdata_value"));
assertThat(hit.getField("xmlstring_field"), instanceOf(XMLString.class));
assertThat(hit.getField("xmlstring_field").toString(), equalTo("<tag>xmlstring_value</tag>"));
if (hit.getField("jsonstring_field") instanceof JSONString) {
JSONString jstr = (JSONString) hit.getField("jsonstring_field");
assertThat(jstr.getContent(), equalTo("{\"foo\":1,\"bar\":2}"));
assertThat(jstr.getParsedJSON(), notNullValue());
com.yahoo.data.access.Inspectable obj = jstr;
com.yahoo.data.access.Inspector value = obj.inspect();
assertThat(value.field("foo").asLong(), equalTo(1L));
assertThat(value.field("bar").asLong(), equalTo(2L));
} else {
StructuredData sdata = (StructuredData) hit.getField("jsonstring_field");
assertThat(sdata.toJson(), equalTo("{\"foo\":1,\"bar\":2}"));
com.yahoo.data.access.Inspectable obj = sdata;
com.yahoo.data.access.Inspector value = obj.inspect();
assertThat(value.field("foo").asLong(), equalTo(1L));
assertThat(value.field("bar").asLong(), equalTo(2L));
}
assertEquals(tensor1, hit.getField("tensor_field1"));
assertEquals(tensor2, hit.getField("tensor_field2"));
}
use of com.yahoo.search.result.StructuredData in project vespa by vespa-engine.
the class JSONDebugSearcher method search.
public Result search(com.yahoo.search.Query query, Execution execution) {
Result r = execution.search(query);
String propertyName = query.properties().getString(PROPERTYNAME);
if (propertyName != null) {
execution.fill(r);
for (Iterator<Hit> i = r.hits().deepIterator(); i.hasNext(); ) {
Hit h = i.next();
if (h instanceof FastHit) {
FastHit hit = (FastHit) h;
Object o = hit.getField(propertyName);
if (o instanceof JSONString) {
JSONString j = (JSONString) o;
r.getQuery().trace(JSON_FIELD + j.getContent(), false, 5);
}
if (o instanceof StructuredData) {
StructuredData d = (StructuredData) o;
r.getQuery().trace(STRUCT_FIELD + d.toJson(), false, 5);
}
if (o instanceof FeatureData) {
FeatureData d = (FeatureData) o;
r.getQuery().trace(FEATURE_FIELD + d.toJson(), false, 5);
}
}
}
}
return r;
}
Aggregations