Skip to content
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

Allow IndexCoordinates in repositories #2505

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ls-urs-keller
Copy link
Contributor

@ls-urs-keller ls-urs-keller commented Mar 27, 2023

It is common to have multiple indexes with the same document structure. E.g. rolling indices over time and having a wildcard alias. Sometimes one wants to select different IndexCoordinates than specified in @Document(indexName = "...", ...). Also you might want to use the same Repository to do cross cluster searches and for this need to specify the IndexCoordinates.

It would therefore be helpful to support IndexCoordinates as an argument to repository methods the same way ScrollPosition, Sort and Pageable are handled.

This PR is an attempt to introduce this change. I keep it as draft to get some comments I can add tests to it if we can agree on the change being valid and something the spring-data-elasticsearch sees as useful.

  • You have read the Spring Data contribution guidelines.
  • There is a ticket in the bug tracker for the project in our [issue tracker](https://github.
    com/spring-projects/spring-data-elasticsearch/issues)
    . Add the issue number to the Closes #issue-number line below
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

Closes #2506

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 27, 2023
Copy link
Collaborator

@sothawo sothawo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, I left some comments, but only minor things.

Needs integration tests, but as you wrote, still a draft.

@@ -32,8 +30,6 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;

import java.util.Collections;

/**
* AbstractElasticsearchRepositoryQuery
*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add yourself to the authors in all the files you change


Object result = null;

if (isDeleteQuery()) {
index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need this line, index is already set above

*/
Object[] getValues();

IndexCoordinates getIndexCoordinatesOrDefaults(@NonNull IndexCoordinates defaults);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add javadoc to the new method

class ElasticsearchParameter extends Parameter {
static class ElasticsearchParameter extends Parameter {

private final boolean indexCoordinatesParameter;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private final boolean indexCoordinatesParameter;
private final boolean isIndexCoordinatesParameter;

Comment on lines +49 to +69
int index = 0;
List<Integer> foundIndices = new ArrayList<>();
for (ElasticsearchParameter parameter : this) {
if (parameter.isIndexCoordinatesParameter()) {
foundIndices.add(index);
}
index++;
}
if (foundIndices.size() > 1) {
throw new IllegalArgumentException(this + " can only contain at most one IndexCoordinates parameter.");
}
return foundIndices.isEmpty() ? -1 : foundIndices.get(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternate way without a list

		int indexCoordinatesIndex = -1;
		int index = 0;
		for (ElasticsearchParameter parameter : this) {
			if (parameter.isIndexCoordinatesParameter()) {
				if(indexCoordinatesIndex != -1) {
					throw new IllegalArgumentException(this + " can only contain at most one IndexCoordinates parameter.");
				} else {
					indexCoordinatesIndex = index;
				}
			}
			index++;
		}
		return indexCoordinatesIndex;

@Override
public Object[] getValues() {
return values.toArray();
}

@Override
public IndexCoordinates getIndexCoordinatesOrDefaults(@NonNull IndexCoordinates defaults) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public IndexCoordinates getIndexCoordinatesOrDefaults(@NonNull IndexCoordinates defaults) {
public IndexCoordinates getIndexCoordinates(@NonNull IndexCoordinates defaults) {

I would not put that in the method name in addition to the parameter name

@Bartolg
Copy link

Bartolg commented May 16, 2023

Hi, any progress with this one? I could help with integration tests if needed :)

@ls-urs-keller
Copy link
Contributor Author

Hi, any progress with this one? I could help with integration tests if needed :)

@Bartolg sitting in my inbox, contribution of tests would be welcome

It is common to have multiple indexes with the same document structure.
E.g. rolling indices over time and having a wildcard alias.
Sometimes one wants to select different IndexCoordinates than specified in
`@Document(indexName = "...", ...)`. Also you might want to use the same
Repository to do cross cluster searches and for this need to specify the IndexCoordinates.

It would therefore be helpful to support IndexCoordinates as an argument to repository methods
the same way ScrollPosition, Sort and Pageable are handled.

This PR is an attempt to introduce this change. I keep it as draft to get some comments
I can add tests to it if we can agree on the change being valid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow IndexCoordinates in repositories
4 participants