ElasticSearch NEST - Sort by a list of dates in a specific range

I have some documents in elastic search with the following display (simplified)

{
    "primaryRecord": {
        "orderTrades": {
                "type" : "nested",
                "include_in_parent":true,
                "properties" : {
                    "materialDates": {"type" : "date", "index" : "not_analyzed", "include_in_all" : "false"},
                }
        }
    }
}

      

Now - I'm asking to get the results based on the date range in the field materialDates

(which is actually the list of dates in the index) - everything is fine. I am having problems sorting the results based on the consistent result from the dates of the list, like this:

Range Request: Jul 7, 2014 - Jul 14, 2014

Sample documents

{
    orderTrades: [
        {
            materialDates: [3/13/2013, 7/10/2014, 8/10/2014]
        },
        {
            materialDates: [7/9/2014, 8/15/2014]
        }
    ]
}

      

Both documents match a range filter, but when sorting in ascending order, the second object must come first, but the value is 3/13/2013

considered in the sort and therefore comes first. Is there a way to only sort the value of the matched array? The current NEST I am using:

query.Sort(
    x => x.OnField(j => j.OrderTrades.Select(z => z.MaterialDates))
        .NestedPath(h => h.OrderTrades)
        .NestedFilter(q => q.Range(
           v => v.OnField("orderTrades.materialDates").From(fromRange).To(toRange)
        ))
        .ToggleSort(true)
);

      

Also capped by an elastic 0.9. Thanks for any ideas - let me know if more information is needed.

+3


source to share


2 answers


This is a tricky requirement because the behavior you are asking is ambiguous because you are trying to sort the value of the array. I think you want it to sort the smallest value in the array that is in the query range. However, this assumes an ascending sort; do you want the top-down variety to matter most?

All considered, that's a fairly specific requirement that I don't think there is an easy way, but you could achieve this with a script - based sort . The script will have to replicate the range of requests and replace any values ​​out of range with something really low or really high depending on the sort order. The scripts are pretty powerful as you can use Groovy and the elasticsearch docs are pretty good.

Having said that, a note in the docs of the sorting script is interesting:



Note. It is recommended to use for script based sort only for custom sort instead of using function_score query as score based sort is faster.

You won't say if you are using the result in your query, but if you could not use function_score to implement the sorting requirement with a similar script element.

0


source


In your example, you have one orderTrades file with two of your nested materials. ES will not sort materialDates within a single OrderTrades document. When you sort the results in ES, it sorts the order in which all orderTrades documents are returned.



I think if you were using ES 1.x you could possibly use nested queries to do what you want, but that is not supported in 0.9

0


source







All Articles