elastic-builder icon indicating copy to clipboard operation
elastic-builder copied to clipboard

Support for multi nested paths and filters in sorts

Open mohak-cf opened this issue 5 years ago • 2 comments
trafficstars

Elasticsearch provides a way to have multiple nested paths in a sort field, documentation example - https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html#_nested_sorting_examples

ElasticBuilder only supports adding a path and a filter in the nested query for sort, i.e nested(nested: { path: string, filter: Query }): this; There should also be support for adding another nested type object.

mohak-cf avatar Dec 19 '19 07:12 mohak-cf

There is no hard restriction against setting an object with another nested object. If you are using Typescript, you can add an ignore directive for the same(// @ts-ignore). I will incorporate this suggestion for the next package release.

sudo-suhas avatar Dec 19 '19 09:12 sudo-suhas

Thanks @sudo-suhas -- I didn't know you can pass props to the fns and they won't get dropped!

@mohak-cf & anyone who stumbles on this in the future -- here's a what I ended up going with:

sort("deeply.nested.numericField")
    .mode("min")
    .order("asc")
    .nested({
      path: "deeply",
      filter: termsQuery("deeply.fieldToFilter", ["a", "b", "c"]),
      // @ts-ignore
      nested: {
        path: "deeply.nested",
        filter: termQuery("deeply.nested.anotherFieldToFilter", "xyz")
      }
    });

jzzfs avatar Nov 14 '22 15:11 jzzfs