-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfc-elasticsearch.java
116 lines (105 loc) · 3.72 KB
/
dfc-elasticsearch.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// use Elastic search query to retrieve list of logEntry (events)
//log?[fromDate={fromDate}][&toDate={toDate}][&event={event}][&pidFilter={pidFilter}][&start={start}][&count={count}]
@Override
public Log getLogs(
Date fromDate,
Date toDate,
EventsEnum event,
String pidFilter,
int startIdx,
int count)
throws NoNodeAvailableException {
boolean datesExist = true;
if (fromDate == null && toDate == null) {
datesExist = false;
}
// restrict search to DataONE exposed data objects
// get this list from the Handle server
// should return something like this:
// "\"/dfc3/home/rods/fabfile.py2\".*|\"/dfc3/home/rods/fabfile.py3\".*"
String uriRegex = null;
try {
uriRegex = getDataOneDataObjectsRegex();
} catch (JargonException e) {
logger.error("error getting DataOne uids and converting to regex");
throw new NoNodeAvailableException(e.getMessage());
}
// get elasticsearch properties
PropertiesLoader loader = new PropertiesLoader();
String elasticsearchDNS =
loader.getProperty("irods.dataone.events.elasticsearch.dns");
int elasticsearchport = Integer.parseInt(loader.getProperty("irods.dataone.events.elasticsearch.port"));
String searchIndex =
loader.getProperty("irods.dataone.events.elasticsearch.searchindex");
String searchType =
loader.getProperty("irods.dataone.events.elasticsearch.searchtype");
String clusterName =
loader.getProperty("irods.dataone.events.elasticsearch.cluster.name");
String rangeField = "created";
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("type", "databook.persistence.rule.rdf.ruleset.Access"));
if (event != null) {
boolQuery.must(QueryBuilders.matchQuery("title",event.getDatabookEvent()));
}
if (uriRegex != null && !uriRegex.isEmpty()) {
boolQuery.must(QueryBuilders.regexpQuery("uri", uriRegex));
}
// Note that date time precision is limited to one millisecond.
RangeFilterBuilder filterBuilder = FilterBuilders.rangeFilter(rangeField);
if (datesExist) {
if (fromDate != null) {
filterBuilder
.from(fromDate.getTime())
.includeLower(true);
}
else {
filterBuilder
.from(0)
.includeLower(true);
}
if (toDate != null) {
filterBuilder
.to(toDate.getTime())
.includeUpper(false);
}
else {
filterBuilder
.to(System.currentTimeMillis())
.includeUpper(false);
}
}
logger.info("creating elastic search transport client: dns={}, port={}", elasticsearchDNS, elasticsearchport);
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", clusterName).build();
Client client = null;
if(elasticsearchDNS != null && elasticsearchDNS.length() > 0) {
client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(elasticsearchDNS, elasticsearchport));
}
else {
client = new TransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
}
SearchRequestBuilder srBuilder = client.prepareSearch(searchIndex)
.setTypes(searchType)
.setQuery(boolQuery)
.setFrom(startIdx).setSize(startIdx + count);
if (datesExist) {
srBuilder.setPostFilter(filterBuilder);
}
logger.info("getLogs: built search request: {}", srBuilder.toString());
SearchResponse response = srBuilder.execute().actionGet();
logger.info("getLogs: got search response: {}", response.toString());
SearchHit[] searchHits = response.getHits().getHits();
Log log = new Log();
if (searchHits.length > 0) {
// now put retrieved data into Log object
try {
log = copyHitsToLog(startIdx, count, response.getHits().getTotalHits(), searchHits);
} catch (JargonException e) {
logger.error("error copying elastic search hits into Log entries");
throw new NoNodeAvailableException(e.getMessage());
}
}
return log;
}