Search in sources :

Example 1 with TopicInfoQueryConverter

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

the class EsTopicDAO method count.

public long count(TopicInfoQuery query) throws Exception {
    TopicInfoQueryConverter converter = new TopicInfoQueryConverter();
    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 : TopicInfoQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoQueryConverter) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with TopicInfoQueryConverter

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

the class EsTopicDAO method query.

// 
// public BoolQueryBuilder getQueryBtTopic(String account,
// boolean isAnyAccount,
// String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic)
// {
// 
// QueryBuilder accountQuery = null;
// if (!isAnyAccount)
// accountQuery = QueryBuilders.termQuery(EsSchema.TOPIC_ACCOUNT, account);
// 
// // Asset clauses
// QueryBuilder assetQuery = null;
// if (!isAnyAsset) {
// assetQuery = QueryBuilders.termQuery(EsSchema.TOPIC_ASSET, asset);
// }
// 
// // Topic clauses
// QueryBuilder topicQuery = null;
// if (isAnySubtopic) {
// topicQuery = QueryBuilders.prefixQuery(EsSchema.TOPIC_SEM_NAME, semTopic);
// }
// else {
// topicQuery = QueryBuilders.termQuery(EsSchema.TOPIC_SEM_NAME, semTopic);
// }
// 
// // Composite clause
// BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 
// if (accountQuery != null)
// boolQuery.must(accountQuery);
// 
// if (assetQuery != null)
// boolQuery.must(assetQuery);
// 
// boolQuery.must(topicQuery);
// 
// return boolQuery;
// }
// 
// public BoolQueryBuilder getQueryBtTopicAndDate(String account,
// boolean isAnyAccount,
// String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// 
// BoolQueryBuilder boolQuery = this.getQueryBtTopic(account, isAnyAccount, asset, isAnyAsset, semTopic, isAnySubtopic);
// 
// // Timestamp clauses
// QueryBuilder dateQuery = QueryBuilders.rangeQuery(EsSchema.TOPIC_TIMESTAMP).from(start).to(end);
// boolQuery.must(dateQuery);
// //
// 
// return boolQuery;
// }
// 
// public void deleteByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic)
// {
// 
// boolean isAnyAccount = true;
// BoolQueryBuilder boolQuery = this.getQueryBtTopic(null, isAnyAccount, asset, isAnyAsset, semTopic, isAnySubtopic);
// 
// // delete by query API is deprecated, scroll with bulk delete must be used
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
// 
// public void deleteByTopic(String asset,
// boolean isAnyAsset,
// String semTopic,
// boolean isAnySubtopic,
// long start,
// long end)
// {
// 
// boolean isAnyAccount = true;
// BoolQueryBuilder boolQuery = this.getQueryBtTopicAndDate(null, isAnyAccount, asset, isAnyAsset, semTopic, isAnySubtopic, start, end);
// 
// // delete by query API is deprecated, scroll with bulk delete must be used
// this.esTypeDAO.deleteByQuery(boolQuery);
// }
// 
// public void deleteByAccount(String accountName, long start, long end)
// throws KapuaInvalidTopicException
// {
// 
// KapuaTopic topic = new KapuaTopic(accountName, KapuaTopic.SINGLE_LEVEL_WCARD, KapuaTopic.MULTI_LEVEL_WCARD);
// String asset = topic.getAsset();
// boolean anyAsset = KapuaTopic.SINGLE_LEVEL_WCARD.equals(asset);
// String semTopic = topic.getSemanticTopic();
// boolean topicPrefix = KapuaTopic.MULTI_LEVEL_WCARD.equals(semTopic);
// if (topicPrefix) {
// semTopic = topic.getParentTopic();
// semTopic = semTopic == null ? "" : semTopic;
// }
// 
// this.deleteByTopic(asset, anyAsset, semTopic, topicPrefix, start, end);
// }
// 
// public SearchHits findByAccount(String semTopic,
// boolean isAnySubtopic,
// int offset,
// int size)
// {
// 
// long timeout = EsUtils.getQueryTimeout();
// 
// SearchResponse response = esTypeDAO.getClient().prepareSearch(esTypeDAO.getIndexName())
// .setTypes(esTypeDAO.getTypeName())
// .setFetchSource(false)
// .addFields("sem_topic", "timestamp", "asset", "account")
// .setFrom(offset)
// .setSize(size)
// .get(TimeValue.timeValueMillis(timeout));
// 
// SearchHits searchHits = response.getHits();
// return searchHits;
// }
public TopicInfoListResult query(TopicInfoQuery query) throws UnknownHostException, KapuaException, EsDatastoreException, KapuaInvalidTopicException, ParseException {
    // get one plus (if there is one) to later get the next key value
    TopicInfoQueryImpl localQuery = new TopicInfoQueryImpl();
    localQuery.setLimit(query.getLimit() + 1);
    TopicInfoQueryConverter tic = new TopicInfoQueryConverter();
    SearchRequestBuilder builder = tic.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 TopicInfoListResultImpl();
    int i = 0;
    int searchHitsSize = searchHits.getHits().length;
    List<TopicInfo> topicInfos = new ArrayList<TopicInfo>();
    TopicInfoBuilder topicInfoBuilder = new TopicInfoBuilder();
    for (SearchHit searchHit : searchHits.getHits()) {
        if (i < query.getLimit()) {
            TopicInfo topicInfo = topicInfoBuilder.buildFromTopic(searchHit).getTopicInfo();
            topicInfos.add(topicInfo);
        }
        i++;
    }
    // TODO check equivalence with CX with Pierantonio
    // TODO what is this nextKey
    Object nextKey = null;
    if (searchHits.getTotalHits() > query.getLimit()) {
        nextKey = query.getLimit();
    }
    TopicInfoListResult result = new TopicInfoListResultImpl(nextKey, searchHitsSize);
    result.addAll(topicInfos);
    return result;
}
Also used : TopicInfoQueryConverter(org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoQueryConverter) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) TopicInfoListResult(org.eclipse.kapua.service.datastore.model.TopicInfoListResult) TopicInfo(org.eclipse.kapua.service.datastore.model.TopicInfo) SearchResponse(org.elasticsearch.action.search.SearchResponse) TopicInfoQueryImpl(org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl) SearchHits(org.elasticsearch.search.SearchHits) TopicInfoListResultImpl(org.eclipse.kapua.service.datastore.internal.model.TopicInfoListResultImpl) TopicInfoBuilder(org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoBuilder)

Aggregations

TopicInfoQueryConverter (org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoQueryConverter)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 SearchHits (org.elasticsearch.search.SearchHits)2 ArrayList (java.util.ArrayList)1 TopicInfoBuilder (org.eclipse.kapua.service.datastore.internal.elasticsearch.TopicInfoBuilder)1 TopicInfoListResultImpl (org.eclipse.kapua.service.datastore.internal.model.TopicInfoListResultImpl)1 TopicInfoQueryImpl (org.eclipse.kapua.service.datastore.internal.model.query.TopicInfoQueryImpl)1 TopicInfo (org.eclipse.kapua.service.datastore.model.TopicInfo)1 TopicInfoListResult (org.eclipse.kapua.service.datastore.model.TopicInfoListResult)1 SearchHit (org.elasticsearch.search.SearchHit)1