-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add Query by Example feature #2418 #2422
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
sothawo
merged 2 commits into
spring-projects:main
from
ezequielantunez:#2418-add-query-by-example-feature
Jan 11, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
...framework/data/elasticsearch/repository/support/querybyexample/ExampleCriteriaMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Copyright 2023 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.elasticsearch.repository.support.querybyexample; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.springframework.dao.InvalidDataAccessApiUsageException; | ||
import org.springframework.data.domain.Example; | ||
import org.springframework.data.domain.ExampleMatcher; | ||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentEntity; | ||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty; | ||
import org.springframework.data.elasticsearch.core.query.Criteria; | ||
import org.springframework.data.mapping.PersistentPropertyAccessor; | ||
import org.springframework.data.mapping.context.MappingContext; | ||
import org.springframework.data.support.ExampleMatcherAccessor; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* Maps a {@link Example} to a {@link org.springframework.data.elasticsearch.core.query.Criteria} | ||
* | ||
* @author Ezequiel Antúnez Camacho | ||
* @since 5.1 | ||
*/ | ||
class ExampleCriteriaMapper { | ||
|
||
private final MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext; | ||
|
||
/** | ||
* Builds a {@link ExampleCriteriaMapper} | ||
* | ||
* @param mappingContext mappingContext to use | ||
*/ | ||
ExampleCriteriaMapper( | ||
MappingContext<? extends ElasticsearchPersistentEntity<?>, ElasticsearchPersistentProperty> mappingContext) { | ||
this.mappingContext = mappingContext; | ||
} | ||
|
||
<S> Criteria criteria(Example<S> example) { | ||
return buildCriteria(example); | ||
} | ||
|
||
private <S> Criteria buildCriteria(Example<S> example) { | ||
final ExampleMatcherAccessor matcherAccessor = new ExampleMatcherAccessor(example.getMatcher()); | ||
|
||
return applyPropertySpecs(new Criteria(), "", example.getProbe(), | ||
mappingContext.getRequiredPersistentEntity(example.getProbeType()), matcherAccessor, | ||
example.getMatcher().getMatchMode()); | ||
} | ||
|
||
private Criteria applyPropertySpecs(Criteria criteria, String path, @Nullable Object probe, | ||
ElasticsearchPersistentEntity<?> persistentEntity, ExampleMatcherAccessor exampleSpecAccessor, | ||
ExampleMatcher.MatchMode matchMode) { | ||
|
||
if (probe == null) { | ||
return criteria; | ||
} | ||
|
||
PersistentPropertyAccessor<?> propertyAccessor = persistentEntity.getPropertyAccessor(probe); | ||
|
||
for (ElasticsearchPersistentProperty property : persistentEntity) { | ||
final String propertyName = property.getName(); | ||
String propertyPath = StringUtils.hasText(path) ? (path + "." + propertyName) : propertyName; | ||
if (exampleSpecAccessor.isIgnoredPath(propertyPath) || property.isCollectionLike() | ||
|| property.isVersionProperty()) { | ||
continue; | ||
} | ||
|
||
Object propertyValue = propertyAccessor.getProperty(property); | ||
if (property.isMap() && propertyValue != null) { | ||
for (Map.Entry<String, Object> entry : ((Map<String, Object>) propertyValue).entrySet()) { | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
criteria = applyPropertySpec(propertyPath + "." + key, value, exampleSpecAccessor, property, matchMode, | ||
criteria); | ||
} | ||
continue; | ||
} | ||
|
||
criteria = applyPropertySpec(propertyPath, propertyValue, exampleSpecAccessor, property, matchMode, criteria); | ||
} | ||
return criteria; | ||
} | ||
|
||
private Criteria applyPropertySpec(String path, Object propertyValue, ExampleMatcherAccessor exampleSpecAccessor, | ||
ElasticsearchPersistentProperty property, ExampleMatcher.MatchMode matchMode, Criteria criteria) { | ||
|
||
if (exampleSpecAccessor.isIgnoreCaseForPath(path)) { | ||
throw new InvalidDataAccessApiUsageException( | ||
"Current implementation of Query-by-Example supports only case-sensitive matching."); | ||
} | ||
|
||
final Object transformedValue = exampleSpecAccessor.getValueTransformerForPath(path) | ||
.apply(Optional.ofNullable(propertyValue)).orElse(null); | ||
|
||
if (transformedValue == null) { | ||
criteria = tryToAppendMustNotSentence(criteria, path, exampleSpecAccessor); | ||
} else { | ||
if (property.isEntity()) { | ||
return applyPropertySpecs(criteria, path, transformedValue, | ||
mappingContext.getRequiredPersistentEntity(property), exampleSpecAccessor, matchMode); | ||
} else { | ||
return applyStringMatcher(applyMatchMode(criteria, path, matchMode), transformedValue, | ||
exampleSpecAccessor.getStringMatcherForPath(path)); | ||
} | ||
} | ||
return criteria; | ||
} | ||
|
||
private Criteria tryToAppendMustNotSentence(Criteria criteria, String path, | ||
ExampleMatcherAccessor exampleSpecAccessor) { | ||
if (ExampleMatcher.NullHandler.INCLUDE.equals(exampleSpecAccessor.getNullHandler()) | ||
|| exampleSpecAccessor.hasPropertySpecifier(path)) { | ||
return criteria.and(path).not().exists(); | ||
} | ||
return criteria; | ||
} | ||
|
||
private Criteria applyMatchMode(Criteria criteria, String path, ExampleMatcher.MatchMode matchMode) { | ||
if (matchMode == ExampleMatcher.MatchMode.ALL) { | ||
return criteria.and(path); | ||
} else { | ||
return criteria.or(path); | ||
} | ||
} | ||
|
||
private Criteria applyStringMatcher(Criteria criteria, Object value, ExampleMatcher.StringMatcher stringMatcher) { | ||
return switch (stringMatcher) { | ||
case DEFAULT -> criteria.is(value); | ||
case EXACT -> criteria.matchesAll(value); | ||
case STARTING -> criteria.startsWith(validateString(value)); | ||
case ENDING -> criteria.endsWith(validateString(value)); | ||
case CONTAINING -> criteria.contains(validateString(value)); | ||
case REGEX -> criteria.regexp(validateString(value)); | ||
}; | ||
ezequielantunez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
private String validateString(Object value) { | ||
if (value instanceof String) { | ||
return value.toString(); | ||
} | ||
throw new IllegalArgumentException("This operation requires a String but got " + value.getClass()); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.