2

I have this piece of code that I need to convert into spring-data-elastic-search.

 {
  "query": {
    "match_all": {}
  },
  "size": 10,
  "from": 0,
  "sort": [
    {
      "program.title.descriptions.value.keyword": {
        "order": "asc",
        "nested": {
          "path": "program.title.descriptions",
          "filter": {
            "match": {
              "program.title.descriptions.length": "LONG"
            }
          }
        }
      }
    }
  ]
}

I have tried up to this,

SortBuilder sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath))

but I don't know how to add the "filter" part into the SortBuilder. If I try the below,

QueryBuilder matchFirst = QueryBuilders.boolQuery().filter(QueryBuilders.matchQuery("program.title.descriptions.length", "LONG"));
sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath)).setNestedFilter(matchFirst);

it would say

java.lang.IllegalArgumentException: Setting both nested_path/nested_filter and nested not allowed
    at org.elasticsearch.search.sort.FieldSortBuilder.setNestedFilter(FieldSortBuilder.java:213)

Any ideas will be much appreciated.

1 Answer 1

2

You need to set the filter inside the nested query. You can try

SortBuilder sb = SortBuilders.fieldSort(sortBy).order(sortOrder).setNestedSort(new NestedSortBuilder(nestedPath).setFilter(matchFirst));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.