public Optional<BulkResponse> delete(List<String> ids) { try { BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); ids.forEach(id -> bulkRequestBuilder.add( client.prepareDelete( aliasName.getValue(), typeName.getValue(), id))); return Optional.of(bulkRequestBuilder.get()); } catch (ValidationException e) { LOGGER.warn("Error while deleting index", e); return Optional.empty(); } }
private boolean aliasExist(Client client, AliasName aliasName) { return client.admin() .indices() .aliasesExist(new GetAliasesRequest() .aliases(aliasName.getValue())) .actionGet() .exists(); }
private void createAliasIfNeeded(Client client, IndexName indexName, AliasName aliasName) { if (!aliasExist(client, aliasName)) { client.admin() .indices() .aliases(new IndicesAliasesRequest() .addAlias(aliasName.getValue(), indexName.getValue())) .actionGet(); } }
public Optional<BulkResponse> update(List<UpdatedRepresentation> updatedDocumentParts) { try { Preconditions.checkNotNull(updatedDocumentParts); BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); updatedDocumentParts.forEach(updatedDocumentPart -> bulkRequestBuilder.add( client.prepareUpdate( aliasName.getValue(), typeName.getValue(), updatedDocumentPart.getId()) .setDoc(updatedDocumentPart.getUpdatedDocumentPart()))); return Optional.of(bulkRequestBuilder.get()); } catch (ValidationException e) { LOGGER.warn("Error while updating index", e); return Optional.empty(); } }
public IndexResponse index(String id, String content) { checkArgument(content); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Indexing {}: {}", id, StringUtils.left(content, DEBUG_MAX_LENGTH_CONTENT)); } return client.prepareIndex(aliasName.getValue(), typeName.getValue(), id) .setSource(content) .get(); }
private SearchRequestBuilder getSearchRequestBuilder(Client client, Collection<MailboxId> users, SearchQuery query, Optional<Long> limit) { return query.getSorts() .stream() .reduce( client.prepareSearch(aliasName.getValue()) .setTypes(typeName.getValue()) .setScroll(TIMEOUT) .addFields(JsonMessageConstants.UID, JsonMessageConstants.MAILBOX_ID, JsonMessageConstants.MESSAGE_ID) .setQuery(queryConverter.from(users, query)) .setSize(computeRequiredSize(limit)), (searchBuilder, sort) -> searchBuilder.addSort(SortConverter.convertSort(sort)), (partialResult1, partialResult2) -> partialResult1); }
public SearchRequestBuilder prepareSearch(QuotaQuery query) { SearchRequestBuilder searchRequestBuilder = client.prepareSearch(readAlias.getValue()) .setTypes(QUOTA_RATIO_TYPE.getValue()) .setScroll(TIMEOUT) .addFields(USER) .setQuery(quotaQueryConverter.from(query)); query.getLimit() .getValue() .ifPresent(searchRequestBuilder::setSize); searchRequestBuilder.addSort( SortBuilders.fieldSort(USER) .order(SortOrder.ASC)); return searchRequestBuilder; }