0

Is it possible to configure the mapping of an index, or the discover view of this in index in a way that an array inside the documents is / will be sorted?

Background: I have a es index with documents containing an array: This array is updated from time to time with new entries (objects containing a timestamp), and I would like this arrays to be sorted according to the timestamp inside the objects.

1 Answer 1

1

If your field is define as nested type then you can use inner_hits to sort the array of object. it will return the sorted object array inside inner_hits for each document.

You can define field as nested like below:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "openTimes": {
        "type": "nested",
        "properties": {
          "date": {
            "type": "date"
          },
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

Let consider below is your sample data:

{"index": { } }
{ "name": "second on 6th (3rd on the 5th)", "openTimes": [ { "date": "2018-12-05T12:00:00" ,"name":"abc"}, { "date": "2018-12-06T11:00:00","name":"xyz" }] }
{"index": { } }
{ "name": "third on 6th (1st on the 5th)", "openTimes": [ {"date": "2018-12-05T10:00:00","name":"abc"}, { "date": "2018-12-06T12:00:00","name":"xyz" }] }
{"index": { } }
{ "name": "first on the 6th (2nd on the 5th)", "openTimes": [ {"date": "2018-12-05T11:00:00","name":"abc" }, { "date": "2018-12-06T10:00:00","name":"xyz" }] }

Below is Query:

{
  "query": {
    "nested": {
      "path": "openTimes",
      "query": {
        "match_all": {}
      },
      "inner_hits": {
        "sort": {
           "openTimes.date": "desc"
        }
      }
    }
  }
}

Sample Response:

{
        "_index" : "nested-listings",
        "_type" : "_doc",
        "_id" : "u0fw338BMCbs63yKkqi0",
        "_score" : 1.0,
        "_source" : {
          "name" : "second on 6th (3rd on the 5th)",
          "openTimes" : [
            {
              "date" : "2018-12-05T12:00:00",
              "name" : "abc"
            },
            {
              "date" : "2018-12-06T11:00:00",
              "name" : "xyz"
            }
          ]
        },
        "inner_hits" : {
          "openTimes" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "nested-listings",
                  "_type" : "_doc",
                  "_id" : "u0fw338BMCbs63yKkqi0",
                  "_nested" : {
                    "field" : "openTimes",
                    "offset" : 1
                  },
                  "_score" : null,
                  "_source" : {
                    "date" : "2018-12-06T11:00:00",
                    "name" : "xyz"
                  },
                  "sort" : [
                    1544094000000
                  ]
                },
                {
                  "_index" : "nested-listings",
                  "_type" : "_doc",
                  "_id" : "u0fw338BMCbs63yKkqi0",
                  "_nested" : {
                    "field" : "openTimes",
                    "offset" : 0
                  },
                  "_score" : null,
                  "_source" : {
                    "date" : "2018-12-05T12:00:00",
                    "name" : "abc"
                  },
                  "sort" : [
                    1544011200000
                  ]
                }
              ]
            }
          }
        }
      }
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.