Search in sources :

Example 1 with EsIndexProperties

use of io.apiman.common.es.util.builder.index.EsIndexProperties in project apiman by apiman.

the class EsStorage method find.

/**
 * Finds entities using a generic search criteria bean.
 * @param criteria
 * @param type
 * @param unmarshaller
 * @throws StorageException
 */
private <T> SearchResultsBean<T> find(SearchCriteriaBean criteria, String type, IUnmarshaller<T> unmarshaller) throws StorageException {
    try {
        SearchResultsBean<T> rval = new SearchResultsBean<>();
        // Set some default in the case that paging information was not included in the request.
        PagingBean paging = criteria.getPaging();
        if (paging == null) {
            paging = new PagingBean();
            paging.setPage(1);
            paging.setPageSize(20);
        }
        int page = paging.getPage();
        int pageSize = paging.getPageSize();
        int start = (page - 1) * pageSize;
        SearchSourceBuilder builder = new SearchSourceBuilder().size(pageSize).from(start).fetchSource(true);
        // Sort order
        OrderByBean orderBy = criteria.getOrderBy();
        if (orderBy != null) {
            String name = orderBy.getName();
            // Get the index definition so that we can see whether a '.keyword' is available. If so, use it.
            EsIndexProperties esIndex = getEsIndices().get(type);
            if (esIndex.hasProperty(name) && esIndex.getProperty(name).isKeywordMultiField()) {
                name = name + ".keyword";
            }
            if (orderBy.isAscending()) {
                builder.sort(name, SortOrder.ASC);
            } else {
                builder.sort(name, SortOrder.DESC);
            }
        }
        // Now process the filter criteria
        List<SearchCriteriaFilterBean> filters = criteria.getFilters();
        QueryBuilder q = null;
        if (filters != null && !filters.isEmpty()) {
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            List<QueryBuilder> andFilter = boolQuery.filter();
            int filterCount = 0;
            for (SearchCriteriaFilterBean filter : filters) {
                String propertyName = filter.getName();
                if (filter.getOperator() == SearchCriteriaFilterOperator.eq) {
                    andFilter.add(QueryBuilders.termQuery(propertyName, filter.getValue()));
                    filterCount++;
                } else if (filter.getOperator() == SearchCriteriaFilterOperator.like) {
                    q = QueryBuilders.wildcardQuery(propertyName, filter.getValue().toLowerCase().replace('%', '*'));
                } else if (filter.getOperator() == SearchCriteriaFilterOperator.bool_eq) {
                    // $NON-NLS-1$
                    andFilter.add(QueryBuilders.termQuery(propertyName, "true".equals(filter.getValue())));
                    filterCount++;
                }
            // TODO implement the other filter operators here!
            }
            if (filterCount > 0) {
                q = boolQuery;
            }
        }
        builder.query(q);
        String fullIndexName = getFullIndexName(type);
        SearchResponse response = getClient().search(new SearchRequest(fullIndexName).source(builder), RequestOptions.DEFAULT);
        SearchHits thehits = response.getHits();
        rval.setTotalSize((int) thehits.getTotalHits().value);
        for (SearchHit hit : thehits.getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            T bean = unmarshaller.unmarshal(sourceAsMap);
            rval.getBeans().add(bean);
        }
        return rval;
    } catch (Exception e) {
        throw new StorageException(e);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) SearchHit(org.elasticsearch.search.SearchHit) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) INDEX_MANAGER_POSTFIX_CONTRACT(io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CONTRACT) INDEX_MANAGER_POSTFIX_CLIENT(io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CLIENT) PagingBean(io.apiman.manager.api.beans.search.PagingBean) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResultsBean(io.apiman.manager.api.beans.search.SearchResultsBean) SearchHits(org.elasticsearch.search.SearchHits) OrderByBean(io.apiman.manager.api.beans.search.OrderByBean) SearchCriteriaFilterBean(io.apiman.manager.api.beans.search.SearchCriteriaFilterBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 2 with EsIndexProperties

use of io.apiman.common.es.util.builder.index.EsIndexProperties in project apiman by apiman.

the class EsMetrics method getEsIndices.

@Override
public Map<String, EsIndexProperties> getEsIndices() {
    EsIndexProperties propertiesMap = EsIndexProperties.builder().addProperty(EsConstants.ES_FIELD_API_DURATION, LONG_PROP).addProperty(EsConstants.ES_FIELD_API_END, DATE_PROP).addProperty(EsConstants.ES_FIELD_API_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_API_ORG_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_API_START, DATE_PROP).addProperty(EsConstants.ES_FIELD_API_VERSION, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_BYTES_DOWNLOADED, LONG_PROP).addProperty(EsConstants.ES_FIELD_BYTES_UPLOADED, LONG_PROP).addProperty(EsConstants.ES_FIELD_CLIENT_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_CLIENT_ORG_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_CLIENT_VERSION, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_CONTRACT_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_ERROR, BOOL_PROP).addProperty(EsConstants.ES_FIELD_ERROR_MESSAGE, TEXT_AND_KEYWORD_PROP_256).addProperty(EsConstants.ES_FIELD_FAILURE, BOOL_PROP).addProperty(EsConstants.ES_FIELD_FAILURE_CODE, LONG_PROP).addProperty(EsConstants.ES_FIELD_FAILURE_REASON, TEXT_AND_KEYWORD_PROP_256).addProperty(EsConstants.ES_FIELD_METHOD, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_PLAN_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_REMOTE_ADDR, IP_PROP).addProperty(EsConstants.ES_FIELD_REQUEST_DURATION, LONG_PROP).addProperty(EsConstants.ES_FIELD_REQUEST_END, DATE_PROP).addProperty(EsConstants.ES_FIELD_REQUEST_START, DATE_PROP).addProperty(EsConstants.ES_FIELD_RESOURCE, TEXT_AND_KEYWORD_PROP_256).addProperty(EsConstants.ES_FIELD_RESPONSE_CODE, LONG_PROP).addProperty(EsConstants.ES_FIELD_RESPONSE_MESSAGE, TEXT_AND_KEYWORD_PROP_256).addProperty(EsConstants.ES_FIELD_URL, TEXT_AND_KEYWORD_PROP_256).addProperty(EsConstants.ES_FIELD_USER, TEXT_AND_KEYWORD_PROP_256).build();
    Map<String, EsIndexProperties> indexMap = new HashMap<>();
    indexMap.put("", propertiesMap);
    return indexMap;
}
Also used : EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) HashMap(java.util.HashMap)

Example 3 with EsIndexProperties

use of io.apiman.common.es.util.builder.index.EsIndexProperties in project apiman by apiman.

the class EsSharedStateComponent method getEsIndices.

@Override
public Map<String, EsIndexProperties> getEsIndices() {
    EsIndexProperties indexDefinition = EsIndexProperties.builder().addProperty(EsConstants.ES_FIELD_ORGANIZATION_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_TYPE, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_VALUE, TEXT_AND_KEYWORD_PROP_256).addProperty(EsConstants.ES_FIELD_VERSION, KEYWORD_PROP).build();
    Map<String, EsIndexProperties> indexMap = new HashMap<>();
    indexMap.put(EsConstants.INDEX_SHARED_STATE_PROPERTY, indexDefinition);
    return indexMap;
}
Also used : EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) HashMap(java.util.HashMap)

Example 4 with EsIndexProperties

use of io.apiman.common.es.util.builder.index.EsIndexProperties in project apiman by apiman.

the class AbstractClientFactory method initializeIndices.

/**
 * Called to initialize the storage.
 * @param client the es client
 * @param indexDefs the index definitions for the component being initialised (can be empty map)
 * @param indexPrefix the index prefix of the ES index to initialize
 */
protected void initializeIndices(RestHighLevelClient client, Map<String, EsIndexProperties> indexDefs, String indexPrefix) {
    try {
        // Do Health request
        ClusterHealthRequest healthRequest = new ClusterHealthRequest();
        try {
            client.cluster().health(healthRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            logger.error("Health check failed - cannot connect to elasticsearch", e);
        }
        // create the index simultaneously. This caused a non-fatal, but annoying, exception.
        synchronized (AbstractClientFactory.class) {
            for (Entry<String, EsIndexProperties> entry : indexDefs.entrySet()) {
                String indexPostfix = entry.getKey();
                EsIndexProperties index = entry.getValue();
                String fullIndexName = EsIndexMapping.getFullIndexName(indexPrefix, indexPostfix);
                if (!createdIndices.contains(fullIndexName)) {
                    GetIndexRequest indexExistsRequest = new GetIndexRequest(fullIndexName);
                    boolean indexExists = client.indices().exists(indexExistsRequest, RequestOptions.DEFAULT);
                    if (!indexExists) {
                        // $NON-NLS-1$
                        this.createIndex(client, index, indexPrefix, indexPostfix);
                        createdIndices.add(fullIndexName);
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) IOException(java.io.IOException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException)

Example 5 with EsIndexProperties

use of io.apiman.common.es.util.builder.index.EsIndexProperties in project apiman by apiman.

the class EsRateLimiterComponent method getEsIndices.

@Override
public Map<String, EsIndexProperties> getEsIndices() {
    EsIndexProperties indexDef = EsIndexProperties.builder().addProperty(EsConstants.ES_FIELD_COUNT, LONG_PROP).addProperty(EsConstants.ES_FIELD_LAST, LONG_PROP).addProperty(EsConstants.ES_FIELD_ORGANIZATION_ID, KEYWORD_PROP).addProperty(EsConstants.ES_FIELD_VERSION, KEYWORD_PROP).build();
    Map<String, EsIndexProperties> indexMap = new HashMap<>();
    indexMap.put(EsConstants.INDEX_RATE_BUCKET, indexDef);
    return indexMap;
}
Also used : EsIndexProperties(io.apiman.common.es.util.builder.index.EsIndexProperties) HashMap(java.util.HashMap)

Aggregations

EsIndexProperties (io.apiman.common.es.util.builder.index.EsIndexProperties)5 HashMap (java.util.HashMap)3 IOException (java.io.IOException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 INDEX_MANAGER_POSTFIX_CLIENT (io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CLIENT)1 INDEX_MANAGER_POSTFIX_CONTRACT (io.apiman.common.es.util.EsConstants.INDEX_MANAGER_POSTFIX_CONTRACT)1 OrderByBean (io.apiman.manager.api.beans.search.OrderByBean)1 PagingBean (io.apiman.manager.api.beans.search.PagingBean)1 SearchCriteriaFilterBean (io.apiman.manager.api.beans.search.SearchCriteriaFilterBean)1 SearchResultsBean (io.apiman.manager.api.beans.search.SearchResultsBean)1 StorageException (io.apiman.manager.api.core.exceptions.StorageException)1 ElasticsearchStatusException (org.elasticsearch.ElasticsearchStatusException)1 ClusterHealthRequest (org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest)1 SearchRequest (org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 TermsQueryBuilder (org.elasticsearch.index.query.TermsQueryBuilder)1 SearchHit (org.elasticsearch.search.SearchHit)1