JvmSd121
JvmSd121

Reputation: 381

search_after with spring-data-elasticsearch

Has anyone successfully used search_after with spring-data-elasticsearch?

I've added _uid to my sort which is correctly getting put in my Pageable by the @Controller. I turned on the slow query log and validated if I manually run the query the @Repository generates, I get in the response:

    "sort": [
      1522270372773,
      "log#AWJuYn7SAKReCIGzMYda"
    ]

I added into my pojo (the class returned by the @Repository):

 @JsonProperty("sort")
 String[] sort;

Sort is always coming back as null. Tried a couple different things and cannot figure out how to get sort to be set. Any ideas?

Upvotes: 2

Views: 2145

Answers (1)

JvmSd121
JvmSd121

Reputation: 381

I've solved the problem but discovered some disappointing details.

The culprit is here: https://github.com/spring-projects/spring-data-elasticsearch/blob/3.0.x/src/main/java/org/springframework/data/elasticsearch/core/DefaultResultMapper.java

In the mapResults method, the code populates my POJO from SearchHit.sourceAsString(). sourceAsString provides a subset of the original JSON which does not include the sort array, i.e.

   "sort": [
      1522270147602,
      "log#AWJuXxJ_AKReCIGzMYdV"
    ]

The access modifiers (privates with no getters and finals) did not provide for an elegant enhancement. I ended up copying DefaultResultMapper and implementing a method similar to setPersistentEntityId which sets sortValues into my pojo. The impl for this method is as follows:

private <T> void setSearchSortValues(T result, Object[] sortValues, Class<T> clazz) {

    if(SortAware.class.isAssignableFrom(clazz)) {
        ((SortAware) result).setSortValues(sortValues);
    }

}

My POJO implements the SortAware interface which I defined as follows:

public interface SortAware {

    public Object[] getSortValues();

    public void setSortValues(Object[] sortValues);

}

Upvotes: 1

Related Questions