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);
}
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations