-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLookupResponse.java
More file actions
100 lines (73 loc) · 2.47 KB
/
LookupResponse.java
File metadata and controls
100 lines (73 loc) · 2.47 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package c.a.i.search;
/**
* Created with IntelliJ IDEA.
* User: aminerounak
* Date: 9/24/13
* Time: 11:03 AM
* To change this template use File | Settings | File Templates.
*/
import com.google.api.client.util.Key;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
@JsonIgnoreProperties(ignoreUnknown = true)
public class LookupResponse<T> {
//{"took":2290,"timed_out":false,"_shards":{"total":40,"successful":40,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
private static final List<Hits.Result> EmptyResults = Collections.emptyList();
public LookupResponse() {
}
private final static Logger LOG = LoggerFactory.getLogger(LookupResponse.class);
@Key
private int took;
@Key("timed_out")
private Boolean timedOut;
@Key
private Hits hits;
public int getTook() {
return took;
}
public void setTook(int took) {
this.took = took;
}
public Boolean getTimedOut() {
return timedOut == null ? false : timedOut;
}
public void setTimedOut(Boolean timedOut) {
this.timedOut = timedOut;
}
public Hits getHits() {
return hits;
}
public void setHits(Hits hits) {
this.hits = hits;
}
public List<Map.Entry<String,Map<String, ?>>> getResults(final Integer limit) {
List<Hits.Result> results = getHits() == null || getHits().getResults() == null
? EmptyResults
: getHits().getResults();
List<Map.Entry<String,Map<String, ?>>> sources = new ArrayList<Map.Entry<String,Map<String, ?>>>();
int count = 0;
int stop = (limit == null) ? results.size() : limit;
for (Hits.Result result : results) {
if (++count > stop) {
LOG.info("LIMIT_HIT");
break;
}
if (result != null && result.getSource() != null) {
sources.add(
new AbstractMap.SimpleImmutableEntry<String, Map<String, ?>>(
result.getId(),
result.getSource()
)
);
} else {
LOG.error("UNEXPECTED-NULL-RESULT Id {} has a null result", result.getId());
}
}
return sources;
}
public List<Map.Entry<String,Map<String, ?>>> getResults() {
return getResults(null);
}
}