Search in sources :

Example 1 with StructuredData

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) StructuredData(com.yahoo.search.result.StructuredData) JSONArray(org.json.JSONArray) JsonNode(com.fasterxml.jackson.databind.JsonNode) JSONString(com.yahoo.prelude.hitfield.JSONString) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor) Result(com.yahoo.search.Result) FastHit(com.yahoo.prelude.fastsearch.FastHit) Hit(com.yahoo.search.result.Hit) JSONObject(org.json.JSONObject) SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 2 with StructuredData

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"));
}
Also used : RawData(com.yahoo.prelude.hitfield.RawData) Tensor(com.yahoo.tensor.Tensor) StructuredData(com.yahoo.search.result.StructuredData) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) XMLString(com.yahoo.prelude.hitfield.XMLString) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 3 with StructuredData

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;
}
Also used : Hit(com.yahoo.search.result.Hit) FastHit(com.yahoo.prelude.fastsearch.FastHit) FastHit(com.yahoo.prelude.fastsearch.FastHit) StructuredData(com.yahoo.search.result.StructuredData) FeatureData(com.yahoo.search.result.FeatureData) JSONString(com.yahoo.prelude.hitfield.JSONString) JSONString(com.yahoo.prelude.hitfield.JSONString) Result(com.yahoo.search.Result)

Aggregations

JSONString (com.yahoo.prelude.hitfield.JSONString)3 StructuredData (com.yahoo.search.result.StructuredData)3 FastHit (com.yahoo.prelude.fastsearch.FastHit)2 Result (com.yahoo.search.Result)2 Hit (com.yahoo.search.result.Hit)2 Test (org.junit.Test)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 SlimeAdapter (com.yahoo.data.access.slime.SlimeAdapter)1 RawData (com.yahoo.prelude.hitfield.RawData)1 XMLString (com.yahoo.prelude.hitfield.XMLString)1 FeatureData (com.yahoo.search.result.FeatureData)1 Cursor (com.yahoo.slime.Cursor)1 Slime (com.yahoo.slime.Slime)1 Tensor (com.yahoo.tensor.Tensor)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1