-
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
Supply a custom Sort.Order
providing Elasticsearch specific parameters
#1911
Comments
can I see an example of a |
for example Sort sort = Sort.by(new GeoDistanceOrder("property", new GeoPoint(47.5, 6.4)).withIgnoreUnmapped(false)); |
@sothawo I'm trying to sort on a Java Date field (which appears to map to a long type), how would I do it? |
If a Date (please don't use |
@sothawo this is a Spring Data Elasticsearch problem, how do I achieve this with |
@sothawo is there some way to set for a field like: it would be nice for Spring Data ES to translate something down into this ES code: |
@sothawo I've also noticed that you have mentioned many times online to switch from
all of my |
that was exactly the point of this issue. So now it is possible to do import org.springframework.data.elasticsearch.core.query.Order;
Sort sort = Sort.by(new Order(Sort.Direction.ASC, "created").withUnmappedType("long")); and use that sort in a How is this date field mapped in your index? Is it really unmapped? What does /<indexname>/_mappings show for that property? If you have data for that property in the index then it will be surely mapped, so adding an unmapped type to a sort has not effect. What is the problem that you have with the sorting? As for the temporal types: |
@sothawo thanks for the help this is the problem i'm trying to solve:
this error goes away as soon as one document is put into the index |
When you use a Spring Data Elasticsearch repository, then on application startup the repository checks if the index exists, and if not creates it with the mapping. If you do not use repositories you can do something like this: @SpringBootApplication
public class SpringdataElasticTestApplication {
@Autowired
private ElasticsearchOperations operations;
public static void main(String[] args) {
SpringApplication.run(SpringdataElasticTestApplication.class, args);
}
@Autowired
public void initializeIndex() {
var indexOperations = operations.indexOps(Foo.class);
if (!indexOperations.exists()) {
indexOperations.createWithMapping();
}
}
} |
@sothawo I am using a Spring Data Elasticsearch repository, and I believe it is creating the indexes, but without mappings: @Document(indexName = "broadcasters")
public class Broadcaster {
@Id
private String id;
private Instant created;
...
} public interface BroadcasterDao extends ElasticsearchRepository<Broadcaster, String> {
...
} but I do not use any explicit how do I ensure that an empty index is created with mappings? |
A property without a |
@sothawo why Spring Data Elasticsearch cannot have default mappings? there are many cases like this where it is natural for Spring Boot Elasticsearch to make assumptions on what the best mapping should be, and implement that mapping implicitly, until overridden explicitly |
@sothawo I changed all of my
if do you agree? |
is this a deficiency inside Spring Core? do you think it is reasonable to do a feature request for this converter? |
Spring Boot makes assumptions what might be right, Spring Data does not. Why should a long be the default instead of a basic_date_time or date_time? Often the users of Spring Data Elasticsearch have data ingested in the indices from other sources and timestamps often are written in forms like "2022-03-20T18:42:38.016+01:00" The default behaviour of Elasticsearch when a field is not added to the mapping is auto mapping. The same is true for Spring Data Elasticsearch. If you do not specify the type, it's auto-mapped. Btw, date/time/timezone mapping is a pretty difficult and complicated topic. Consider the following values (skipping seconds and msecs): 2022-03-20 18:47 CET - Germany mapped to an Instant they are all the same. If you store these as long, you will always get it back in UTC: 2022-03-20 17:47 UTC You then can display it in the TZ you"re in, but what was the original value sent to Elasticsearch? So how dates are mapped into Elasticsearch is a pretty specific topic depending on the application, using default implicit values is an accident waiting to happen - and one of the reasons why I refrain from using using It might have worked for you with the default mapping (what version of Spring Data Elasticsearch are you using btw?), but since 4.0 we are quite specific about the daste handling in Spring Data Elasticsearch . And - when talking to the maintainers of Spring Data mongo | jpa or whatever module - just metioning dates/timezones will get them running away, crying out loud, despairing. I know, it's an awful topic. |
@sothawo I'm using Spring 5.3 and Spring Data ES 4.3 I, personally, think it is reasonable to make
@Field(type = FieldType.Date, format = DateFormat.epoch_millis)
private Instant created;
|
Spring Data Elasticsearch should supply a
Sort.Order
derived class that can provide custom parameters likeignore_unmapped
(see #1908) for an example where this is neededThe text was updated successfully, but these errors were encountered: