public static void createMapping(Client client, IndexName indexName, TypeName typeName, XContentBuilder mappingsSources) { client.admin() .indices() .preparePutMapping(indexName.getValue()) .setType(typeName.getValue()) .setSource(mappingsSources) .execute() .actionGet(); }
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(); } }
public static XContentBuilder getMappingContent() { try { return jsonBuilder() .startObject() .startObject(QuotaRatioElasticSearchConstants.QUOTA_RATIO_TYPE.getValue()) .startObject(PROPERTIES) .startObject(USER) .field(TYPE, STRING) .field(INDEX, NOT_ANALYZED) .endObject() .startObject(DOMAIN) .field(TYPE, STRING) .field(INDEX, NOT_ANALYZED) .endObject() .startObject(QUOTA_RATIO) .field(TYPE, DOUBLE) .endObject() .endObject() .endObject() .endObject(); } catch (IOException e) { throw new RuntimeException(e); } } }
public static boolean mappingAlreadyExist(Client client, IndexName indexName, TypeName typeName) { return Iterators.toStream(client.admin() .indices() .prepareGetMappings(indexName.getValue()) .execute() .actionGet() .getMappings() .valuesIt()) .anyMatch(mapping -> mapping.keys().contains(typeName.getValue())); }
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 ListenableActionFuture<BulkResponse> deleteRetrievedIds(Client client, SearchResponse searchResponse) { BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); for (SearchHit hit : searchResponse.getHits()) { bulkRequestBuilder.add(client.prepareDelete() .setIndex(aliasName.getValue()) .setType(typeName.getValue()) .setId(hit.getId())); } return bulkRequestBuilder.execute(); }
protected Void doDeleteByQuery(QueryBuilder queryBuilder) { new ScrollIterable(client, client.prepareSearch(aliasName.getValue()) .setTypes(typeName.getValue()) .setScroll(TIMEOUT) .setNoFields() .setQuery(queryBuilder) .setSize(batchSize)) .stream() .map(searchResponse -> deleteRetrievedIds(client, searchResponse)) .forEach(ListenableActionFuture::actionGet); return null; }
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); }
.startObject() .startObject(MailboxElasticSearchConstants.MESSAGE_TYPE.getValue()) .startObject(PROPERTIES)
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; }
@Test public void eventShouldDoNothingWhenNoQuotaEvent() throws Exception { quotaMailboxListener.event(EVENT); embeddedElasticSearch.awaitForElasticSearch(); SearchResponse searchResponse = client.prepareSearch(QuotaRatioElasticSearchConstants.DEFAULT_QUOTA_RATIO_READ_ALIAS.getValue()) .setTypes(QuotaRatioElasticSearchConstants.QUOTA_RATIO_TYPE.getValue()) .setQuery(matchAllQuery()) .execute() .get(); assertThat(searchResponse.getHits().totalHits()).isEqualTo(0); }
@Test public void eventShouldIndexEventWhenQuotaEvent() throws Exception { quotaMailboxListener.event(new QuotaUsageUpdatedEvent(BOB_SESSION, QUOTAROOT, Counts._52_PERCENT, Sizes._55_PERCENT, NOW)); embeddedElasticSearch.awaitForElasticSearch(); SearchResponse searchResponse = client.prepareSearch(QuotaRatioElasticSearchConstants.DEFAULT_QUOTA_RATIO_READ_ALIAS.getValue()) .setTypes(QuotaRatioElasticSearchConstants.QUOTA_RATIO_TYPE.getValue()) .setQuery(matchAllQuery()) .execute() .get(); assertThat(searchResponse.getHits().totalHits()).isEqualTo(1); } }