Elasticsearch sort based on week days


(Vatsal Patel) #1

Hello,
I have store data in below format.I want to filter data based on range and then apply ordering based on days.First item should be the best match with range and days.I also want those items which does not match with days.(But should be in the same range).
I have full Json document. here i have created "serviceTimes" object,inside that i am storing the value for week-days and service time in milliseconds.

"serviceTimes": [
    {
      "from": 39600000,
      "to": 43200000,
      "day": "0",
      "status": "On"
    },
    {
      "from": 0,
      "to": 0,
      "day": "1",
      "status": "Off"
    },
    {
      "from": 40260000,
      "to": 45900000,
      "day": "2",
      "status": "Off"
    },
    {
      "from": 0,
      "to": 0,
      "day": "3",
      "status": "Off"
    },
    {
      "from": 0,
      "to": 0,
      "day": "4",
      "status": "Off"
    },
    {
      "from": 40500000,
      "to": 49500000,
      "day": "5",
      "status": "on"
    },
    {
      "from": 0,
      "to": 0,
      "day": "6",
      "status": "Off"
    }
  ],

Status - on that means service providing on that day.
Day - 0 to 6 are week days from Monday to Sunday.
Below is my sample bool query.

     "query": {
          "bool": {
            "must": [
              {
                "range": {
                  "serviceTimes.to": {
                    "gte": 28800000
                  }
                }
              }
            ],
            "should": [
              {
                "match": {
                  "serviceTimes.day": "1"
                }
              }
            ]
          }
        }

Above query filter the data only which match with serviceTime.day.But, i also want that items which match with serviceTimes.from but does not match with serviceTime.day.
please help me. Thank you in advance.


(system) #2