private SearchRequestBuilder criteria2builder(ElasticSearchCriteria criteria) { String[] indices = criteria.getIndices(); if (indices == null || indices.length == 0) indices = new String[] {indexManager.getIndexName()}; SearchRequestBuilder srb = client.prepareSearch(indices); srb.setTimeout(new TimeValue(10, TimeUnit.SECONDS)); String[] types = criteria.getTypes(); if (types != null && types.length > 0) srb.setTypes(types); QueryBuilder qb = criteria.getQueryBuilder(); String query = criteria.getQuery(); if (qb == null && StringUtils.isBlank(query)) throw new NullPointerException("queryBuilder is null and queryString is blank"); if (qb == null && StringUtils.isNotBlank(query)) { if (wildcardQueryPattern.matcher(query).matches()) { String[] arr = query.split(":", 2); qb = QueryBuilders.wildcardQuery(arr[0], arr[1]); } else { QueryStringQueryBuilder qsqb = new QueryStringQueryBuilder(query); qsqb.defaultOperator(Operator.AND); qb = qsqb; } } srb.setQuery(qb); Map<String, Boolean> sorts = criteria.getSorts(); for (Map.Entry<String, Boolean> entry : sorts.entrySet()) srb.addSort(entry.getKey(), entry.getValue() ? SortOrder.DESC : SortOrder.ASC); return srb; }
/** Set indices and types to the search request. */ private void setIndicesAndTypes() { request.setIndices(query.getIndexArr()); String[] typeArr = query.getTypeArr(); if (typeArr != null) { request.setTypes(typeArr); } }
public static SearchRequest randomSearch(Client client, String[] indices) { final SearchRequestBuilder builder = new SearchRequestBuilder(client); builder.setIndices(indices); builder.setTypes(INDEX_TYPE); builder.setQuery(randomFrom(TestQueryType.values()).getQuery()); return builder.request(); }
private static SearchResponse searchByAddress( final String aIndex, final Client aClient, final String aType, SearchType aSearchType, QueryBuilder aQuery) { SearchRequestBuilder searchRequest = aClient.prepareSearch(aIndex); if (aType != null) { searchRequest.setTypes(aType); } if (aSearchType != null) { searchRequest.setSearchType(aSearchType); } searchRequest.setQuery(aQuery); return searchRequest.execute().actionGet(); }
/** * Get all active rivers * * @return */ public List<T> get() { if (logger.isDebugEnabled()) logger.debug("get()"); List<T> rivers = new ArrayList<T>(); SearchRequestBuilder srb = new SearchRequestBuilder(client); try { srb.setIndices(SMDSearchProperties.ES_META_INDEX); srb.setTypes(SMDSearchProperties.ES_META_RIVERS); SearchResponse response = srb.execute().actionGet(); if (response.getHits().totalHits() > 0) { for (int i = 0; i < response.getHits().hits().length; i++) { T river = buildInstance(); SearchHit hit = response.getHits().hits()[i]; // We only manage rivers for type getHelper().type() river = getHelper().toRiver(river, hit.sourceAsMap()); if (river.getType().equals(getHelper().type())) { // For each river, we check if the river is started or not river.setStart(riverService.checkState(river)); rivers.add(river); } } } } catch (IndexMissingException e) { // That's a common use case. We started with an empty index } if (logger.isDebugEnabled()) logger.debug("/get()={}", rivers); return rivers; }