-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add native support for range field types by using a range object #1863
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
xhaggi:1862-native-range-support
Jul 12, 2021
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
444 changes: 444 additions & 0 deletions
444
src/main/java/org/springframework/data/elasticsearch/core/Range.java
Large diffs are not rendered by default.
Oops, something went wrong.
40 changes: 40 additions & 0 deletions
40
.../springframework/data/elasticsearch/core/convert/AbstractPersistentPropertyConverter.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,40 @@ | ||
/* | ||
* Copyright 2021 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.core.convert; | ||
|
||
import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentPropertyConverter; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* @author Sascha Woo | ||
* @since 4.3 | ||
*/ | ||
public abstract class AbstractPersistentPropertyConverter implements ElasticsearchPersistentPropertyConverter { | ||
|
||
private final PersistentProperty<?> property; | ||
|
||
public AbstractPersistentPropertyConverter(PersistentProperty<?> property) { | ||
|
||
Assert.notNull(property, "property must not be null."); | ||
this.property = property; | ||
} | ||
|
||
protected PersistentProperty<?> getProperty() { | ||
return property; | ||
} | ||
|
||
} |
121 changes: 121 additions & 0 deletions
121
...ngframework/data/elasticsearch/core/convert/AbstractRangePersistentPropertyConverter.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,121 @@ | ||
/* | ||
* Copyright 2021 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.core.convert; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.data.elasticsearch.core.Range; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* @author Sascha Woo | ||
* @since 4.3 | ||
*/ | ||
public abstract class AbstractRangePersistentPropertyConverter<T> extends AbstractPersistentPropertyConverter { | ||
|
||
protected static final String LT_FIELD = "lt"; | ||
protected static final String LTE_FIELD = "lte"; | ||
protected static final String GT_FIELD = "gt"; | ||
protected static final String GTE_FIELD = "gte"; | ||
|
||
public AbstractRangePersistentPropertyConverter(PersistentProperty<?> property) { | ||
super(property); | ||
} | ||
|
||
@Override | ||
public Object read(Object value) { | ||
|
||
Assert.notNull(value, "value must not be null."); | ||
Assert.isInstanceOf(Map.class, value, "value must be instance of Map."); | ||
|
||
try { | ||
Map<String, Object> source = (Map<String, Object>) value; | ||
Range.Bound<T> lowerBound; | ||
Range.Bound<T> upperBound; | ||
|
||
if (source.containsKey(GTE_FIELD)) { | ||
lowerBound = Range.Bound.inclusive(parse((String) source.get(GTE_FIELD))); | ||
} else if (source.containsKey(GT_FIELD)) { | ||
lowerBound = Range.Bound.exclusive(parse((String) source.get(GT_FIELD))); | ||
} else { | ||
lowerBound = Range.Bound.unbounded(); | ||
} | ||
|
||
if (source.containsKey(LTE_FIELD)) { | ||
upperBound = Range.Bound.inclusive(parse((String) source.get(LTE_FIELD))); | ||
} else if (source.containsKey(LT_FIELD)) { | ||
upperBound = Range.Bound.exclusive(parse((String) source.get(LT_FIELD))); | ||
} else { | ||
upperBound = Range.Bound.unbounded(); | ||
} | ||
|
||
return Range.of(lowerBound, upperBound); | ||
|
||
} catch (Exception e) { | ||
throw new ConversionException( | ||
String.format("Unable to convert value '%s' of property '%s'", value, getProperty().getName()), e); | ||
} | ||
} | ||
|
||
@Override | ||
public Object write(Object value) { | ||
|
||
xhaggi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Assert.notNull(value, "value must not be null."); | ||
Assert.isInstanceOf(Range.class, value, "value must be instance of Range."); | ||
|
||
try { | ||
Range<T> range = (Range<T>) value; | ||
Range.Bound<T> lowerBound = range.getLowerBound(); | ||
Range.Bound<T> upperBound = range.getUpperBound(); | ||
Map<String, Object> target = new LinkedHashMap<>(); | ||
|
||
if (lowerBound.isBounded()) { | ||
String lowerBoundValue = format(lowerBound.getValue().get()); | ||
if (lowerBound.isInclusive()) { | ||
target.put(GTE_FIELD, lowerBoundValue); | ||
} else { | ||
target.put(GT_FIELD, lowerBoundValue); | ||
} | ||
} | ||
|
||
if (upperBound.isBounded()) { | ||
String upperBoundValue = format(upperBound.getValue().get()); | ||
if (upperBound.isInclusive()) { | ||
target.put(LTE_FIELD, upperBoundValue); | ||
} else { | ||
target.put(LT_FIELD, upperBoundValue); | ||
} | ||
} | ||
|
||
return target; | ||
|
||
} catch (Exception e) { | ||
throw new ConversionException( | ||
String.format("Unable to convert value '%s' of property '%s'", value, getProperty().getName()), e); | ||
} | ||
} | ||
|
||
protected abstract String format(T value); | ||
|
||
protected Class<?> getGenericType() { | ||
return getProperty().getTypeInformation().getTypeArguments().get(0).getType(); | ||
} | ||
|
||
protected abstract T parse(String value); | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
.../org/springframework/data/elasticsearch/core/convert/DatePersistentPropertyConverter.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,70 @@ | ||
/* | ||
* Copyright 2021 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.core.convert; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
|
||
/** | ||
* @author Sascha Woo | ||
* @since 4.3 | ||
*/ | ||
public class DatePersistentPropertyConverter extends AbstractPersistentPropertyConverter { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DatePersistentPropertyConverter.class); | ||
|
||
private final List<ElasticsearchDateConverter> dateConverters; | ||
|
||
public DatePersistentPropertyConverter(PersistentProperty<?> property, | ||
List<ElasticsearchDateConverter> dateConverters) { | ||
|
||
super(property); | ||
this.dateConverters = dateConverters; | ||
} | ||
|
||
@Override | ||
public Object read(Object value) { | ||
|
||
String s = value.toString(); | ||
|
||
for (ElasticsearchDateConverter dateConverter : dateConverters) { | ||
try { | ||
return dateConverter.parse(s); | ||
} catch (Exception e) { | ||
LOGGER.trace(e.getMessage(), e); | ||
} | ||
} | ||
|
||
throw new ConversionException(String.format("Unable to convert value '%s' to %s for property '%s'", s, | ||
getProperty().getActualType().getTypeName(), getProperty().getName())); | ||
} | ||
|
||
@Override | ||
public Object write(Object value) { | ||
|
||
try { | ||
return dateConverters.get(0).format((Date) value); | ||
} catch (Exception e) { | ||
throw new ConversionException( | ||
String.format("Unable to convert value '%s' of property '%s'", value, getProperty().getName()), e); | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
...springframework/data/elasticsearch/core/convert/DateRangePersistentPropertyConverter.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,62 @@ | ||
/* | ||
* Copyright 2021 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.core.convert; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.mapping.PersistentProperty; | ||
|
||
/** | ||
* @author Sascha Woo | ||
* @since 4.3 | ||
*/ | ||
public class DateRangePersistentPropertyConverter extends AbstractRangePersistentPropertyConverter<Date> { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(DateRangePersistentPropertyConverter.class); | ||
|
||
private final List<ElasticsearchDateConverter> dateConverters; | ||
|
||
public DateRangePersistentPropertyConverter(PersistentProperty<?> property, | ||
List<ElasticsearchDateConverter> dateConverters) { | ||
|
||
super(property); | ||
this.dateConverters = dateConverters; | ||
} | ||
|
||
@Override | ||
protected String format(Date value) { | ||
return dateConverters.get(0).format(value); | ||
} | ||
|
||
@Override | ||
protected Date parse(String value) { | ||
|
||
for (ElasticsearchDateConverter converters : dateConverters) { | ||
try { | ||
return converters.parse(value); | ||
} catch (Exception e) { | ||
LOGGER.trace(e.getMessage(), e); | ||
} | ||
} | ||
|
||
throw new ConversionException(String.format("Unable to convert value '%s' to %s for property '%s'", value, | ||
getGenericType().getTypeName(), getProperty().getName())); | ||
} | ||
|
||
} |
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
53 changes: 53 additions & 0 deletions
53
...ringframework/data/elasticsearch/core/convert/NumberRangePersistentPropertyConverter.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,53 @@ | ||
/* | ||
* Copyright 2021 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.core.convert; | ||
|
||
import org.springframework.data.mapping.PersistentProperty; | ||
|
||
/** | ||
* @author Sascha Woo | ||
* @since 4.3 | ||
*/ | ||
public class NumberRangePersistentPropertyConverter extends AbstractRangePersistentPropertyConverter<Number> { | ||
|
||
public NumberRangePersistentPropertyConverter(PersistentProperty<?> property) { | ||
super(property); | ||
} | ||
|
||
@Override | ||
protected String format(Number number) { | ||
return String.valueOf(number); | ||
} | ||
|
||
@Override | ||
protected Number parse(String value) { | ||
|
||
Class<?> type = getGenericType(); | ||
if (Integer.class.isAssignableFrom(type)) { | ||
return Integer.valueOf(value); | ||
} else if (Float.class.isAssignableFrom(type)) { | ||
return Float.valueOf(value); | ||
} else if (Long.class.isAssignableFrom(type)) { | ||
return Long.valueOf(value); | ||
} else if (Double.class.isAssignableFrom(type)) { | ||
return Double.valueOf(value); | ||
} | ||
|
||
throw new ConversionException(String.format("Unable to convert value '%s' to %s for property '%s'", value, | ||
type.getTypeName(), getProperty().getName())); | ||
} | ||
|
||
} |
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.