Sort Usageedit

Allows to add one or more sort on specific fields. Each sort can be reversed as well. The sort is defined on a per field level, with special field name for _score to sort by score.

Fluent DSL exampleedit

s => s
.Sort(ss => ss
    .Ascending(p => p.StartedOn)
    .Descending(p => p.Name)
    .Descending(SortSpecialField.Score)
    .Ascending(SortSpecialField.DocumentIndexOrder)
    .Field(f => f
        .Field(p => p.LastActivity)
        .Order(SortOrder.Descending)
        .MissingLast()
        .UnmappedType(FieldType.Date)
        .Mode(SortMode.Average)
        .NestedPath(p => p.Tags)
        .NestedFilter(q => q.MatchAll())
    )
    .GeoDistance(g => g
        .Field(p => p.Location)
        .DistanceType(GeoDistanceType.Arc)
        .Order(SortOrder.Ascending)
        .Unit(DistanceUnit.Centimeters)
        .Mode(SortMode.Min)
        .PinTo(new GeoLocation(70, -70), new GeoLocation(-12, 12))
    )
    .Script(sc => sc
        .Type("number")
        .Ascending()
        .Script(script => script
            .Inline("doc['numberOfCommits'].value * factor")
            .Lang("groovy")
            .Params(p => p.Add("factor", 1.1))
        )
    )
)

Object Initializer syntax exampleedit

new SearchRequest<Project>
{
    Sort = new List<ISort>
    {
        new SortField { Field = "startedOn", Order = SortOrder.Ascending },
        new SortField { Field = "name", Order = SortOrder.Descending },
        new SortField { Field = "_score", Order = SortOrder.Descending },
        new SortField { Field = "_doc", Order = SortOrder.Ascending },
        new SortField
        {
            Field = Field<Project>(p=>p.LastActivity),
            Order = SortOrder.Descending,
            Missing = "_last",
            UnmappedType = FieldType.Date,
            Mode = SortMode.Average,
            NestedPath = Field<Project>(p=>p.Tags),
            NestedFilter = new MatchAllQuery(),
        },
        new GeoDistanceSort
        {
            Field = "location",
            Order = SortOrder.Ascending,
            DistanceType = GeoDistanceType.Arc,
            GeoUnit = DistanceUnit.Centimeters,
            Mode = SortMode.Min,
            Points = new [] {new GeoLocation(70, -70), new GeoLocation(-12, 12) }
        },
        new ScriptSort
        {
            Type = "number",
            Order = SortOrder.Ascending,
            Script =  new InlineScript("doc['numberOfCommits'].value * factor")
            {
                Lang = "groovy",
                Params = new Dictionary<string, object>
                {
                    { "factor", 1.1 }
                }
            }
        }
    }
}

Example json output. 

{
  "sort": [
    {
      "startedOn": {
        "order": "asc"
      }
    },
    {
      "name": {
        "order": "desc"
      }
    },
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "_doc": {
        "order": "asc"
      }
    },
    {
      "lastActivity": {
        "missing": "_last",
        "order": "desc",
        "mode": "avg",
        "nested_filter": {
          "match_all": {}
        },
        "nested_path": "tags",
        "unmapped_type": "date"
      }
    },
    {
      "_geo_distance": {
        "location": [
          {
            "lat": 70.0,
            "lon": -70.0
          },
          {
            "lat": -12.0,
            "lon": 12.0
          }
        ],
        "order": "asc",
        "mode": "min",
        "distance_type": "arc",
        "unit": "cm"
      }
    },
    {
      "_script": {
        "order": "asc",
        "type": "number",
        "script": {
          "params": {
            "factor": 1.1
          },
          "inline": "doc['numberOfCommits'].value * factor",
          "lang": "groovy"
        }
      }
    }
  ]
}