1
votes

I am using angularjs for frontend and loopback for backend and elastic search for the database.

I have a model with properties as:

 "name": {
  "type": "string",
  "required": true
},
"mobileNumber": {
  "type": "string",
  "required": true
},
"email": {
  "type": "string"
},
"message": {
  "type": "string",
  "required": true
},
"quantity": {
  "type": "number",
  "required": true
},
"price": {
  "type": "number",
  "required": true
},
"status": {
  "type": "string",
  "required": true,
  "default": "open"
}

},

data as:

{
 "_index": "XXXXXX",
 "_type": "XXXXX",
 "_id": "XXXXXXX",
 "_version": 1,
 "_score": 1,
 "_source": {
  "name": "aadil kirana",
  "email": "[email protected]",
  "message": "dfgfb dgfggf",
  "quantity": 3434,
  "price": 5454,
  "status": "open",
  "createdAt": "2017-12-19T14:53:41.727Z",
  "updatedAt": "2017-12-19T14:53:41.727Z"
  }
}

Status could be open, processing, close, reject and failure.

All I want is to get the data in the order where I can see all the open status data ordered by createdAt date, then all the prcoessing status data ordered by createdAt dat and so on....

I tried using loopback filters as:

filter = {
                order: ['status ASC','createdAt DESC'],
            };

but this gives me First all the close status data ordered by date, then all the open status data ordered by date and so on, that status ordered alphabetically.

Please help me to get the desired result.

2
(1) Track down the actual ES query that is built by the loopback-es-connector. It should be possible to spit it out in the logs because the logging framework is debug which allows turning on logging by namespaces. (2) Take that query and try to modify it directly until you get the desired result, which will require gaining a better understanding of ES DSL. (3) Ping the authors of loopback-es-connector with the raw ES query that you tweaked and gives you the right results. Ask them to enhance the connector or tell you how to instruct the connector to end up with a query like that. - pulkitsinghal
use numbers for status - Mehari Mamo
@Mehari do you mean assigning as providing id to status, like open=1, processing=2 and so on ? - Varun Sukheja

2 Answers

0
votes

You can add a new property to your data as statusOrder and define

  • 1 -> open
  • 2 -> close
  • ...

and order by statusOrder instead of status when you are ordering status.

0
votes

All I want is to get the data in the order where I can see all the open status data ordered by createdAt date, then all the prcoessing status data ordered by createdAt dat and so on....

A workaround for this could be to let Elasticsearch do the sort with custom order e.g. in this context the status could be ordered as open followed by processing followed by close followed by reject followed by failure. It can be done with Function Score Query. Some more insights could also be found here

Sample input data for bulk insert:

POST custom/sort/_bulk?pretty
{"index" : {"_index" : "custom"}}
{"status": "open", "createdAt": "2017-12-19T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "open", "createdAt": "2017-12-18T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "processing", "createdAt": "2017-12-19T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "processing", "createdAt": "2017-12-17T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "close", "createdAt": "2017-12-19T14:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "close", "createdAt": "2017-12-19T15:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "failure", "createdAt": "2017-12-19T10:53:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "failure", "createdAt": "2017-12-19T14:59:41.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "reject", "createdAt": "2017-12-19T14:53:40.727Z"}
{"index" : {"_index" : "custom"}}
{"status": "reject", "createdAt": "2017-12-19T14:53:41.727Z"}

Sample response from elastic search (without custom order):

Query:

GET custom/sort/_search?filter_path=took,hits.total,hits.hits._score,hits.hits._source

{
  "took": 0,
  "hits": {
    "total": 10,
    "hits": [
      {
        "_score": 1,
        "_source": {
          "status": "processing",
          "createdAt": "2017-12-19T14:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "close",
          "createdAt": "2017-12-19T14:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "reject",
          "createdAt": "2017-12-19T14:53:40.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "open",
          "createdAt": "2017-12-18T14:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "failure",
          "createdAt": "2017-12-19T10:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "failure",
          "createdAt": "2017-12-19T14:59:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "reject",
          "createdAt": "2017-12-19T14:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "open",
          "createdAt": "2017-12-19T14:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "processing",
          "createdAt": "2017-12-17T14:53:41.727Z"
        }
      },
      {
        "_score": 1,
        "_source": {
          "status": "close",
          "createdAt": "2017-12-19T15:53:41.727Z"
        }
      }
    ]
  }
}

Query to mimic custom ordering :

GET custom/sort/_search?filter_path=took,hits.hits._id,hits.hits._score,hits.hits._source,hits.hits.sort
{
  "query": {
    "function_score": {
      "boost_mode": "replace",
      "query": {
        "constant_score": {
          "filter": {
            "terms": {
              "status.keyword": [
                "open",
                "processing",
                "close",
                "reject",
                "failure"
              ]
            }
          }
        }
      },
      "functions": [
        {
          "filter": {
            "term": {
              "status.keyword": "open"
            }
          },
          "weight": 4
        },
        {
          "filter": {
            "term": {
              "status.keyword": "processing"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "term": {
              "status.keyword": "close"
            }
          },
          "weight": 2
        },
        {
          "filter": {
            "term": {
              "status.keyword": "reject"
            }
          },
          "weight": 1
        },
        {
          "filter": {
            "term": {
              "status.keyword": "failure"
            }
          },
          "weight": 0
        }
      ]
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      },
      "createdAt": {
        "order": "asc"
      }
    }
  ]
}

Output (with custom order):

{
  "took": 4,
  "hits": {
    "hits": [
      {
        "_id": "grOucmABwtSchlgLKlaV",
        "_score": 4,
        "_source": {
          "status": "open",
          "createdAt": "2017-12-18T14:53:41.727Z"
        },
        "sort": [
          4,
          1513608821727
        ]
      },
      {
        "_id": "gbOucmABwtSchlgLKlaV",
        "_score": 4,
        "_source": {
          "status": "open",
          "createdAt": "2017-12-19T14:53:41.727Z"
        },
        "sort": [
          4,
          1513695221727
        ]
      },
      {
        "_id": "hLOucmABwtSchlgLKlaV",
        "_score": 3,
        "_source": {
          "status": "processing",
          "createdAt": "2017-12-17T14:53:41.727Z"
        },
        "sort": [
          3,
          1513522421727
        ]
      },
      {
        "_id": "g7OucmABwtSchlgLKlaV",
        "_score": 3,
        "_source": {
          "status": "processing",
          "createdAt": "2017-12-19T14:53:41.727Z"
        },
        "sort": [
          3,
          1513695221727
        ]
      },
      {
        "_id": "hbOucmABwtSchlgLKlaV",
        "_score": 2,
        "_source": {
          "status": "close",
          "createdAt": "2017-12-19T14:53:41.727Z"
        },
        "sort": [
          2,
          1513695221727
        ]
      },
      {
        "_id": "hrOucmABwtSchlgLKlaV",
        "_score": 2,
        "_source": {
          "status": "close",
          "createdAt": "2017-12-19T15:53:41.727Z"
        },
        "sort": [
          2,
          1513698821727
        ]
      },
      {
        "_id": "ibOucmABwtSchlgLKlaV",
        "_score": 1,
        "_source": {
          "status": "reject",
          "createdAt": "2017-12-19T14:53:40.727Z"
        },
        "sort": [
          1,
          1513695220727
        ]
      },
      {
        "_id": "irOucmABwtSchlgLKlaV",
        "_score": 1,
        "_source": {
          "status": "reject",
          "createdAt": "2017-12-19T14:53:41.727Z"
        },
        "sort": [
          1,
          1513695221727
        ]
      },
      {
        "_id": "h7OucmABwtSchlgLKlaV",
        "_score": 0,
        "_source": {
          "status": "failure",
          "createdAt": "2017-12-19T10:53:41.727Z"
        },
        "sort": [
          0,
          1513680821727
        ]
      },
      {
        "_id": "iLOucmABwtSchlgLKlaV",
        "_score": 0,
        "_source": {
          "status": "failure",
          "createdAt": "2017-12-19T14:59:41.727Z"
        },
        "sort": [
          0,
          1513695581727
        ]
      }
    ]
  }
}