Skip to content

Allow index names per query in bulk requests. #2079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ private List<MultiGetRequest.Item> getMultiRequestItems(Query searchQuery, Class
// region indexing
public IndexRequest indexRequest(IndexQuery query, IndexCoordinates index) {

String indexName = index.getIndexName();
String indexName = query.getIndexName() != null ? query.getIndexName() : index.getIndexName();
IndexRequest indexRequest;

Object queryObject = query.getObject();
Expand Down Expand Up @@ -1027,7 +1027,7 @@ private QueryRescorerBuilder getQueryRescorerBuilder(RescorerQuery rescorerQuery
// region update
public UpdateRequest updateRequest(UpdateQuery query, IndexCoordinates index) {

String indexName = index.getIndexName();
String indexName = query.getIndexName() != null ? query.getIndexName() : index.getIndexName();
UpdateRequest updateRequest = new UpdateRequest(indexName, query.getId());

if (query.getScript() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public class IndexQuery {
@Nullable private Long primaryTerm;
@Nullable private String routing;
@Nullable private OpType opType;
@Nullable private String indexName;

public IndexQuery() {}

public IndexQuery(@Nullable String id, @Nullable Object object, @Nullable Long version, @Nullable String source,
@Nullable String parentId, @Nullable Long seqNo, @Nullable Long primaryTerm, @Nullable String routing,
@Nullable OpType opType) {
@Nullable OpType opType, @Nullable String indexName) {
this.id = id;
this.object = object;
this.version = version;
Expand All @@ -53,6 +54,7 @@ public IndexQuery(@Nullable String id, @Nullable Object object, @Nullable Long v
this.primaryTerm = primaryTerm;
this.routing = routing;
this.opType = opType;
this.indexName = indexName;
}

@Nullable
Expand Down Expand Up @@ -152,6 +154,14 @@ public void setOpType(OpType opType) {
this.opType = opType;
}

/**
* @since 4.4
*/
@Nullable
public String getIndexName() {
return indexName;
}

/**
* OpType for the index operation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class IndexQueryBuilder {
@Nullable private String routing;
@Nullable private IndexQuery.OpType opType;
@Nullable private RefreshPolicy refreshPolicy;
@Nullable private String indexName;

public IndexQueryBuilder() {}

Expand Down Expand Up @@ -89,6 +90,14 @@ public IndexQueryBuilder withOpType(IndexQuery.OpType opType) {
}

public IndexQuery build() {
return new IndexQuery(id, object, version, source, parentId, seqNo, primaryTerm, routing, opType);
return new IndexQuery(id, object, version, source, parentId, seqNo, primaryTerm, routing, opType, indexName);
}

/**
* @since 4.4
*/
public IndexQueryBuilder withIndex(@Nullable String indexName) {
this.indexName = indexName;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class UpdateQuery {
@Nullable private final Integer slices;
@Nullable private final ScriptType scriptType;
@Nullable private final String scriptName;
@Nullable private final String indexName;

public static Builder builder(String id) {
return new Builder(id);
Expand All @@ -81,7 +82,7 @@ private UpdateQuery(String id, @Nullable String script, @Nullable Map<String, Ob
@Nullable Boolean abortOnVersionConflict, @Nullable Integer batchSize, @Nullable Integer maxDocs,
@Nullable Integer maxRetries, @Nullable String pipeline, @Nullable Float requestsPerSecond,
@Nullable Boolean shouldStoreResult, @Nullable Integer slices, @Nullable ScriptType scriptType,
@Nullable String scriptName) {
@Nullable String scriptName, @Nullable String indexName) {

this.id = id;
this.script = script;
Expand Down Expand Up @@ -112,6 +113,7 @@ private UpdateQuery(String id, @Nullable String script, @Nullable Map<String, Ob
this.slices = slices;
this.scriptType = scriptType;
this.scriptName = scriptName;
this.indexName = indexName;
}

public String getId() {
Expand Down Expand Up @@ -258,6 +260,14 @@ public String getScriptName() {
return scriptName;
}

/**
* @since 4.4
*/
@Nullable
public String getIndexName() {
return indexName;
}

public static final class Builder {
private String id;
@Nullable private String script = null;
Expand Down Expand Up @@ -288,6 +298,7 @@ public static final class Builder {
@Nullable private Integer slices;
@Nullable private ScriptType scriptType;
@Nullable private String scriptName;
@Nullable private String indexName;

private Builder(String id) {
this.id = id;
Expand Down Expand Up @@ -441,7 +452,12 @@ public UpdateQuery build() {
return new UpdateQuery(id, script, params, document, upsert, lang, routing, scriptedUpsert, docAsUpsert,
fetchSource, fetchSourceIncludes, fetchSourceExcludes, ifSeqNo, ifPrimaryTerm, refreshPolicy, retryOnConflict,
timeout, waitForActiveShards, query, abortOnVersionConflict, batchSize, maxDocs, maxRetries, pipeline,
requestsPerSecond, shouldStoreResult, slices, scriptType, scriptName);
requestsPerSecond, shouldStoreResult, slices, scriptType, scriptName, indexName);
}

public Builder withIndex(@Nullable String indexName) {
this.indexName = indexName;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.indices.PutIndexTemplateRequest;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
Expand Down Expand Up @@ -57,12 +58,12 @@
import org.springframework.data.elasticsearch.core.index.AliasActionParameters;
import org.springframework.data.elasticsearch.core.index.AliasActions;
import org.springframework.data.elasticsearch.core.index.PutTemplateRequest;
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
import org.springframework.data.elasticsearch.core.reindex.Remote;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.mapping.SimpleElasticsearchMappingContext;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.elasticsearch.core.query.RescorerQuery.ScoreMode;
import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
import org.springframework.data.elasticsearch.core.reindex.Remote;
import org.springframework.lang.Nullable;

/**
Expand Down Expand Up @@ -92,7 +93,8 @@ static void setUpAll() {
requestFactory = new RequestFactory((converter));
}

@Test // FPI-734
@Test
// FPI-734
void shouldBuildSearchWithGeoSortSort() throws JSONException {
CriteriaQuery query = new CriteriaQuery(new Criteria("lastName").is("Smith"));
Sort sort = Sort.by(new GeoDistanceOrder("location", new GeoPoint(49.0, 8.4)));
Expand Down Expand Up @@ -140,7 +142,8 @@ void shouldBuildSearchWithGeoSortSort() throws JSONException {
assertEquals(expected, searchRequest, false);
}

@Test // DATAES-449
@Test
// DATAES-449
void shouldAddRouting() {
String route = "route66";
CriteriaQuery query = new CriteriaQuery(new Criteria("lastName").is("Smith"));
Expand All @@ -152,7 +155,8 @@ void shouldAddRouting() {
assertThat(searchRequest.routing()).isEqualTo(route);
}

@Test // DATAES-765
@Test
// DATAES-765
void shouldAddMaxQueryWindowForUnpagedToRequest() {
Query query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withPageable(Pageable.unpaged()).build();

Expand All @@ -162,7 +166,8 @@ void shouldAddMaxQueryWindowForUnpagedToRequest() {
assertThat(searchRequest.source().size()).isEqualTo(RequestFactory.INDEX_MAX_RESULT_WINDOW);
}

@Test // DATAES-799
@Test
// DATAES-799
void shouldIncludeSeqNoAndPrimaryTermFromIndexQueryToIndexRequest() {
IndexQuery query = new IndexQuery();
query.setObject(new Person());
Expand All @@ -175,7 +180,8 @@ void shouldIncludeSeqNoAndPrimaryTermFromIndexQueryToIndexRequest() {
assertThat(request.ifPrimaryTerm()).isEqualTo(2L);
}

@Test // DATAES-799
@Test
// DATAES-799
void shouldNotRequestSeqNoAndPrimaryTermWhenEntityClassDoesNotContainSeqNoPrimaryTermProperty() {
Query query = new NativeSearchQueryBuilder().build();

Expand All @@ -184,7 +190,8 @@ void shouldNotRequestSeqNoAndPrimaryTermWhenEntityClassDoesNotContainSeqNoPrimar
assertThat(request.source().seqNoAndPrimaryTerm()).isNull();
}

@Test // DATAES-799
@Test
// DATAES-799
void shouldRequestSeqNoAndPrimaryTermWhenEntityClassContainsSeqNoPrimaryTermProperty() {
Query query = new NativeSearchQueryBuilder().build();

Expand All @@ -194,7 +201,8 @@ void shouldRequestSeqNoAndPrimaryTermWhenEntityClassContainsSeqNoPrimaryTermProp
assertThat(request.source().seqNoAndPrimaryTerm()).isTrue();
}

@Test // DATAES-799
@Test
// DATAES-799
void shouldNotRequestSeqNoAndPrimaryTermWhenEntityClassIsNull() {
Query query = new NativeSearchQueryBuilder().build();

Expand All @@ -203,7 +211,8 @@ void shouldNotRequestSeqNoAndPrimaryTermWhenEntityClassIsNull() {
assertThat(request.source().seqNoAndPrimaryTerm()).isNull();
}

@Test // DATAES-864
@Test
// DATAES-864
void shouldBuildIndicesAliasRequest() throws IOException, JSONException {

AliasActions aliasActions = new AliasActions();
Expand Down Expand Up @@ -316,7 +325,8 @@ void shouldBuildIndicesAliasRequest() throws IOException, JSONException {
assertEquals(expected, json, false);
}

@Test // DATAES-612
@Test
// DATAES-612
void shouldCreatePutIndexTemplateRequest() throws JSONException, IOException {

String expected = "{\n" + //
Expand Down Expand Up @@ -430,7 +440,8 @@ private String requestToString(ToXContent request) throws IOException {
return XContentHelper.toXContent(request, XContentType.JSON, true).utf8ToString();
}

@Test // #1686
@Test
// #1686
void shouldBuildSearchWithRescorerQuery() throws JSONException {
CriteriaQuery query = new CriteriaQuery(new Criteria("lastName").is("Smith"));
RescorerQuery rescorerQuery = new RescorerQuery(new NativeSearchQueryBuilder() //
Expand Down Expand Up @@ -546,7 +557,7 @@ void shouldSetRequestCacheFalseOnSearchRequest() {
assertThat(searchRequest.requestCache()).isFalse();
}

@Test
@Test // #2004
@DisplayName("should set stored fields on SearchRequest")
void shouldSetStoredFieldsOnSearchRequest() {

Expand All @@ -560,7 +571,8 @@ void shouldSetStoredFieldsOnSearchRequest() {
.isEqualTo(Arrays.asList("last-name", "current-location"));
}

@Test // #1529
@Test
// #1529
void shouldCreateReindexRequest() throws IOException, JSONException {
final String expected = "{\n" + //
" \"source\":{\n" + //
Expand Down Expand Up @@ -608,6 +620,7 @@ void shouldCreateReindexRequest() throws IOException, JSONException {
}

@Test
// #1529
void shouldAllowSourceQueryForReindexWithoutRemote() throws IOException, JSONException {
final String expected = "{\n" + //
" \"source\":{\n" + //
Expand Down Expand Up @@ -644,6 +657,59 @@ void shouldNotFailOnEmptyWildcardStatesOnToElasticsearchIndicesOptions() {
.isNotNull();
}

@Test // #2043
@DisplayName("should use index name from query if set in bulk index")
void shouldUseIndexNameFromQueryIfSetInBulkIndex() {

String queryIndexName = "query-index-name";
String methodIndexName = "method-index-name";
IndexQuery indexQuery = new IndexQueryBuilder().withIndex(queryIndexName).withId("42").withObject(new Person())
.build();

IndexRequest indexRequest = requestFactory.indexRequest(indexQuery, IndexCoordinates.of(methodIndexName));

assertThat(indexRequest.index()).isEqualTo(queryIndexName);
}

@Test // #2043
@DisplayName("should use index name from method if none is set in query in bulk index")
void shouldUseIndexNameFromMethodIfNoneIsSetInQueryInBulkIndex() {

String methodIndexName = "method-index-name";
IndexQuery indexQuery = new IndexQueryBuilder().withId("42").withObject(new Person()).build();

IndexRequest indexRequest = requestFactory.indexRequest(indexQuery, IndexCoordinates.of(methodIndexName));

assertThat(indexRequest.index()).isEqualTo(methodIndexName);
}

@Test // #2043
@DisplayName("should use index name from query if set in bulk update")
void shouldUseIndexNameFromQueryIfSetInBulkUpdate() {

String queryIndexName = "query-index-name";
String methodIndexName = "method-index-name";
UpdateQuery updateQuery = UpdateQuery.builder("42").withIndex(queryIndexName)
.withDocument(org.springframework.data.elasticsearch.core.document.Document.create()).build();

UpdateRequest updateRequest = requestFactory.updateRequest(updateQuery, IndexCoordinates.of(methodIndexName));

assertThat(updateRequest.index()).isEqualTo(queryIndexName);
}

@Test // #2043
@DisplayName("should use index name from method if none is set in query in bulk update")
void shouldUseIndexNameFromMethodIfNoneIsSetInQueryInBulkUpdate() {

String methodIndexName = "method-index-name";
UpdateQuery updateQuery = UpdateQuery.builder("42")
.withDocument(org.springframework.data.elasticsearch.core.document.Document.create()).build();

UpdateRequest updateRequest = requestFactory.updateRequest(updateQuery, IndexCoordinates.of(methodIndexName));

assertThat(updateRequest.index()).isEqualTo(methodIndexName);
}

// region entities
static class Person {
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CriteriaQueryRestTemplateIntegrationTests extends CriteriaQueryInte
static class Config {
@Bean
IndexNameProvider indexNameProvider() {
return new IndexNameProvider();
return new IndexNameProvider("criteria-query-es7");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@
*/
@ContextConfiguration(classes = { DoubleIDRepositoryRestTemplateIntegrationTests.Config.class })
public class DoubleIDRepositoryRestTemplateIntegrationTests extends DoubleIDRepositoryIntegrationTests {

@Configuration
@Import({ ElasticsearchRestTemplateConfiguration.class })
@EnableElasticsearchRepositories(considerNestedRepositories = true)
static class Config {
@Bean
IndexNameProvider indexNameProvider() {
return new IndexNameProvider();
return new IndexNameProvider("doubleid-reository-es7");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
@ContextConfiguration(classes = { DynamicSettingAndMappingEntityRepositoryRestTemplateIntegrationTests.Config.class })
public class DynamicSettingAndMappingEntityRepositoryRestTemplateIntegrationTests
extends DynamicSettingAndMappingEntityRepositoryIntegrationTests {

@Configuration
@Import({ ElasticsearchRestTemplateConfiguration.class })
@EnableElasticsearchRepositories(considerNestedRepositories = true)
static class Config {
@Bean
IndexNameProvider indexNameProvider() {
return new IndexNameProvider();
return new IndexNameProvider("dynamic-setting-and-mapping-3s7");
}
}

Expand Down