-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,9 @@ | |
*/ | ||
package org.springframework.data.elasticsearch.repository.query; | ||
|
||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; | ||
import org.springframework.data.repository.query.ParameterAccessor; | ||
import org.springframework.lang.NonNull; | ||
|
||
/** | ||
* @author Christoph Strobl | ||
|
@@ -29,4 +31,6 @@ public interface ElasticsearchParameterAccessor extends ParameterAccessor { | |
* @return | ||
*/ | ||
Object[] getValues(); | ||
|
||
IndexCoordinates getIndexCoordinatesOrDefaults(@NonNull IndexCoordinates defaults); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add javadoc to the new method |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,10 +29,11 @@ | |
* @since 3.2 | ||
*/ | ||
public class ElasticsearchParameters extends Parameters<ElasticsearchParameters, ElasticsearchParameter> { | ||
|
||
private final List<ElasticsearchParameter> scriptedFields = new ArrayList<>(); | ||
private final List<ElasticsearchParameter> runtimeFields = new ArrayList<>(); | ||
|
||
private final int indexCoordinatesIndex; | ||
|
||
public ElasticsearchParameters(Method method, TypeInformation<?> domainType) { | ||
|
||
super(method, parameter -> new ElasticsearchParameter(parameter, domainType)); | ||
|
@@ -50,17 +51,35 @@ public ElasticsearchParameters(Method method, TypeInformation<?> domainType) { | |
runtimeFields.add(parameter); | ||
} | ||
} | ||
this.indexCoordinatesIndex = initIndexCoordinatesIndex(); | ||
} | ||
|
||
private int initIndexCoordinatesIndex() { | ||
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); | ||
Comment on lines
+58
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. alternate way without a list
|
||
} | ||
|
||
private ElasticsearchParameter parameterFactory(MethodParameter methodParameter, TypeInformation<?> domainType) { | ||
return new ElasticsearchParameter(methodParameter, domainType); | ||
} | ||
|
||
|
||
private ElasticsearchParameters(List<ElasticsearchParameter> parameters) { | ||
super(parameters); | ||
this.indexCoordinatesIndex = initIndexCoordinatesIndex(); | ||
} | ||
|
||
|
||
@Override | ||
protected ElasticsearchParameters createFrom(List<ElasticsearchParameter> parameters) { | ||
return new ElasticsearchParameters(parameters); | ||
|
@@ -73,4 +92,12 @@ List<ElasticsearchParameter> getScriptedFields() { | |
List<ElasticsearchParameter> getRuntimeFields() { | ||
return runtimeFields; | ||
} | ||
|
||
public boolean hasIndexCoordinatesParameter() { | ||
return this.indexCoordinatesIndex != -1; | ||
} | ||
|
||
public int getIndexCoordinatesIndex() { | ||
return indexCoordinatesIndex; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -15,7 +15,13 @@ | |||||
*/ | ||||||
package org.springframework.data.elasticsearch.repository.query; | ||||||
|
||||||
import java.util.Arrays; | ||||||
import java.util.List; | ||||||
|
||||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; | ||||||
import org.springframework.data.repository.query.Parameters; | ||||||
import org.springframework.data.repository.query.ParametersParameterAccessor; | ||||||
import org.springframework.lang.NonNull; | ||||||
|
||||||
/** | ||||||
* @author Christoph Strobl | ||||||
|
@@ -25,6 +31,7 @@ class ElasticsearchParametersParameterAccessor extends ParametersParameterAccess | |||||
implements ElasticsearchParameterAccessor { | ||||||
|
||||||
private final Object[] values; | ||||||
private final ElasticsearchParameters eleasticSearchParameters; | ||||||
|
||||||
/** | ||||||
* Creates a new {@link ElasticsearchParametersParameterAccessor}. | ||||||
|
@@ -36,10 +43,20 @@ class ElasticsearchParametersParameterAccessor extends ParametersParameterAccess | |||||
|
||||||
super(method.getParameters(), values); | ||||||
this.values = values; | ||||||
this.eleasticSearchParameters = method.getParameters(); | ||||||
} | ||||||
|
||||||
@Override | ||||||
public Object[] getValues() { | ||||||
return values; | ||||||
} | ||||||
|
||||||
|
||||||
@Override | ||||||
public IndexCoordinates getIndexCoordinatesOrDefaults(@NonNull IndexCoordinates defaults) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I would not put that in the method name in addition to the parameter name |
||||||
if (!eleasticSearchParameters.hasIndexCoordinatesParameter()) { | ||||||
return defaults; | ||||||
} | ||||||
return (IndexCoordinates) getValues()[eleasticSearchParameters.getIndexCoordinatesIndex()]; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
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