use of org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageBuilder in project kapua by eclipse.
the class EsMessageDAO method query.
//
// public BoolQueryBuilder getQueryByAccountAndDate(String account, boolean isAnyAccount, long start, long end)
// {
//
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
//
// return boolQuery;
// }
//
// public BoolQueryBuilder getQueryByAssetAndDate(String asset, boolean isAnyAsset, long start, long end)
// {
//
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//
// // Asset clauses
// QueryBuilder assetQuery = null;
// if (!isAnyAsset) {
// assetQuery = QueryBuilders.termQuery(EsSchema.MESSAGE_AS_NAME, asset);
// boolQuery.must(assetQuery);
// }
//
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
//
// return boolQuery;
// }
//
// public BoolQueryBuilder getQueryByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic)
// {
//
// // Asset clauses
// QueryBuilder assetQuery = null;
// if (!isAnyAsset) {
// assetQuery = QueryBuilders.termQuery(EsSchema.MESSAGE_AS_NAME, asset);
// }
//
// // Topic clauses
// QueryBuilder topicQuery = null;
// if (isAnySubtopic) {
// topicQuery = QueryBuilders.prefixQuery(EsSchema.MESSAGE_SEM_TOPIC, semTopic);
// }
// else {
// topicQuery = QueryBuilders.termQuery(EsSchema.MESSAGE_SEM_TOPIC, semTopic);
// }
//
// // Composite clause
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// if (assetQuery != null)
// boolQuery.must(assetQuery);
// boolQuery.must(topicQuery);
// //
// return boolQuery;
// }
//
// public BoolQueryBuilder getQueryByTopicAndDate(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
//
// BoolQueryBuilder boolQuery = this.getQueryByTopic(asset, isAnyAsset, semTopic, isAnySubtopic);
//
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
//
// return boolQuery;
// }
//
// public BoolQueryBuilder getQueryByMetricValueAndDate(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// String fullMetricName,
// long start,
// long end,
// Object min,
// Object max)
// {
//
// BoolQueryBuilder boolQuery = this.getQueryByTopic(asset, isAnyAsset, semTopic, isAnySubtopic);
//
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.MESSAGE_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
//
// // Metric value clause
// QueryBuilder valueQuery = QueryBuilders.rangeQuery(fullMetricName).gte(min).lte(max);
// boolQuery.must(valueQuery);
// //
//
// return boolQuery;
// }
//
// public void deleteByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// BoolQueryBuilder boolQuery = this.getQueryByTopicAndDate(asset, isAnyAsset, semTopic, isAnySubtopic, start, end);
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
//
// public void deleteByAccount(long start,
// long end)
// {
// // Query clauses
// boolean isAnyAccount = true;
// BoolQueryBuilder boolQuery = this.getQueryByAccountAndDate(null, isAnyAccount, start, end);
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
public MessageListResult query(MessageQuery query) throws Exception {
MessageQueryImpl localQuery = new MessageQueryImpl();
localQuery.copy(query);
localQuery.setLimit(query.getLimit() + 1);
MessageQueryConverter converter = new MessageQueryConverter();
SearchRequestBuilder builder = converter.toSearchRequestBuilder(esTypeDAO.getIndexName(), esTypeDAO.getTypeName(), query);
SearchResponse response = builder.get(TimeValue.timeValueMillis(EsUtils.getQueryTimeout()));
SearchHits searchHits = response.getHits();
if (searchHits == null || searchHits.getTotalHits() == 0)
return new MessageListResultImpl();
int i = 0;
int searchHitsSize = searchHits.getHits().length;
List<Message> messages = new ArrayList<Message>();
MessageBuilder msgBuilder = new MessageBuilder();
for (SearchHit searchHit : searchHits.getHits()) {
if (i < query.getLimit()) {
Message message = msgBuilder.build(searchHit, query.getFetchStyle()).getMessage();
messages.add(message);
}
i++;
}
// TODO check equivalence with CX with Pierantonio
// TODO what is this nextKey
Object nextKey = null;
if (searchHitsSize > query.getLimit()) {
Message lastMessage = msgBuilder.build(searchHits.getHits()[searchHitsSize - 1], query.getFetchStyle()).getMessage();
nextKey = lastMessage.getTimestamp().getTime();
}
// TODO verifiy total count
long totalCount = 0;
if (query.isAskTotalCount()) {
// totalCount = MessageDAO.findByTopicCount(keyspace, topic, start, end);
totalCount = searchHits.getTotalHits();
}
if (totalCount > Integer.MAX_VALUE)
throw new Exception("Total hits exceeds integer max value");
MessageListResultImpl result = new MessageListResultImpl(nextKey, (int) totalCount);
result.addAll(messages);
return result;
}
Aggregations