blob: 979c50f7f8f6775303a3fa97258cd7e378d60b71 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.demo;
import com.yahoo.search.Result;
import com.yahoo.search.rendering.Renderer;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
/**
* Render result sets as plain text. First line is whether an error occurred,
* second rendering initialization time stamp, then each line is the ID of each
* document returned, and the last line is time stamp for when the renderer was
* finished.
*/
public class DemoRenderer extends Renderer {
private String heading;
/**
* No global, shared state to set.
*/
public DemoRenderer() {
}
@Override
protected void render(Writer writer, Result result) throws IOException {
if (result.hits().getErrorHit() == null) {
writer.write("OK\n");
} else {
writer.write("Oops!\n");
}
writer.write(heading);
writer.write("\n");
renderHits(writer, result.hits());
writer.write("Rendering finished work: " + System.currentTimeMillis());
writer.write("\n");
}
private void renderHits(Writer writer, HitGroup hits) throws IOException {
for (Iterator<Hit> i = hits.deepIterator(); i.hasNext();) {
Hit h = i.next();
if (h.types().contains("summary")) {
String id = h.getDisplayId();
if (id != null) {
writer.write(id);
writer.write("\n");
}
}
}
}
@Override
public String getEncoding() {
return "utf-8";
}
@Override
public String getMimeType() {
return "text/plain";
}
/**
* Initialize mutable, per-result set state here.
*/
@Override
public void init() {
long time = System.currentTimeMillis();
heading = "Renderer initialized: " + time;
}
}
|