This is Spring Boot Data Elasticsearch (Embedded Elasticsearch).
I have a simple query:
{
"query": {
"match_phrase" : {
"text" : "12"
}
}
}
And when I send this query via localhost:9200 there's an answer:
{
"took": 56, "timed_out": false,
"_shards": {"total": 1, "successful": 1, "failed": 0 },
"hits": {
"total": 5,
"max_score": 0.3172162,
"hits": [
{
"_index": "abc", "_type": "abc", "_id": "AWbzvDOghegm62vg48M5",
"_score": 0.3172162,
"_source": {
"entityId": "5840", "text": "abc"
}
},
{
"_score": 0.3172162,
"_source": {"entityId": "5838"}
},
{
"_score": 0.3172162,
"_source": {"entityId": "5834"}
},
{
"_score": 0.26434684,
"_source": {"entityId": "5850"}
},
{
"_score": 0.26434684,
"_source": {"entityId": "5846"}
}
]
}
}
Everything is OK.
In project I have a Data ElasticsearchRepository:
public interface MyESEntityRepository extends ElasticsearchRepository<MyESEntity, String> {
@Query("same ES query")
List<MyESEntity> find(String q);
}
And here my problem: I sand the same query and receive same set but in a different order.
Through 9200 I receive next ordered by _score set with entityId: 5840 5838 5834 5850 5846.
ES Data Repository return: 5850 5846 5840 5838 5834. Thus, in this example, it looks as if it is a desc order by entityId. However, in a real query, something is different. Perhaps this is the order of time modification.
How to get order by _score?
With
Pageable same effect:
Page<MyESEntity> find(String q, Pageable pageable);
and
find("12", new PageRequest(0, 10, new Sort(new Sort.Order(Sort.Direction.DESC,"_score")))).getContent();