Search in sources :

Example 1 with MessageQueryConverter

use of org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter in project kapua by eclipse.

the class EsMessageDAO method count.

public long count(MessageQuery query) throws Exception {
    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)
        return 0;
    return searchHits.getTotalHits();
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) MessageQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with MessageQueryConverter

use of org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter 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;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Message(org.eclipse.kapua.service.datastore.model.Message) SearchHit(org.elasticsearch.search.SearchHit) MessageListResultImpl(org.eclipse.kapua.service.datastore.internal.model.MessageListResultImpl) MessageQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter) ArrayList(java.util.ArrayList) EsDatastoreException(org.eclipse.kapua.service.datastore.internal.elasticsearch.EsDatastoreException) UnknownHostException(java.net.UnknownHostException) KapuaException(org.eclipse.kapua.KapuaException) SearchResponse(org.elasticsearch.action.search.SearchResponse) MessageQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl) MessageBuilder(org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageBuilder) SearchHits(org.elasticsearch.search.SearchHits)

Aggregations

MessageQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageQueryConverter)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 SearchHits (org.elasticsearch.search.SearchHits)2 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 KapuaException (org.eclipse.kapua.KapuaException)1 EsDatastoreException (org.eclipse.kapua.service.datastore.internal.elasticsearch.EsDatastoreException)1 MessageBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.MessageBuilder)1 MessageListResultImpl (org.eclipse.kapua.service.datastore.internal.model.MessageListResultImpl)1 MessageQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.MessageQueryImpl)1 Message (org.eclipse.kapua.service.datastore.model.Message)1 SearchHit (org.elasticsearch.search.SearchHit)1