Elasticsearch-js: Sort is broken

Created on 19 Sep 2014  路  9Comments  路  Source: elastic/elasticsearch-js

Running a search in Node with a sort is broken. I can run the exact same query with curl and it returns correctly. It looks like it may have something to do with the way the sort array is getting parsed as you can see below in the trace. I searched all over to see if I was doing something wrong, but maybe someone more skilled than me can take a look.

function runSearch() {
        esC.search({
            "query":{
                "bool":{
                    "must":[{
                            "query_string":{
                                "default_field":"UPDATE.user_id","query":"2f9633f8-3cc5-344b-bf71-fe0f78ae46da"}
                        }]
                }
            },
            "from":0,
            "size":50,
            "sort":[{ "timestamp" : {"order" : "desc", "ignore_unmapped" : true}}]
        }, function (err, res) {
            console.log(res);
        });
    }
Elasticsearch DEBUG: 2014-09-19T03:33:04Z
  starting request { method: 'POST',
    path: '/_search',
    query: 
     { query: { bool: [Object] },
       from: 0,
       size: 50,
       sort: '[object Object]' } }


Elasticsearch TRACE: 2014-09-19T03:33:04Z
  -> POST http://10.0.200.95:9200/_search?query=&from=0&size=50&sort=%5Bobject%20Object%5D

  <- 400
  {
    "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[b-srgZ7-S4WD5CSq5njRpQ][feed][0]: RemoteTransportException[[Curtis][inet[/10.0.200.94:9300]][search/phase/query]]; nested: SearchParseException[[feed][0]: from[0],size[50]: Parse Failure [Failed to parse source [{\"from\":0,\"size\":50,\"sort\":[{\"[object Object]\":{}}]}]]]; nested: SearchParseException[[feed][0]: from[0],size[50]: Parse Failure [No mapping found for [[object Object]] in order to sort on]]; }{[V31kXWvmSlyVGDW543VaJA][feed][1]: SearchParseException[[feed][1]: from[0],size[50]: Parse Failure [Failed to parse source [{\"from\":0,\"size\":50,\"sort\":[{\"[object Object]\":{}}]}]]]; nested: SearchParseException[[feed][1]: from[0],size[50]: Parse Failure [No mapping found for [[object Object]] in order to sort on]]; }{[V31kXWvmSlyVGDW543VaJA][feed][2]: SearchParseException[[feed][2]: from[0],size[50]: Parse Failure [Failed to parse source [{\"from\":0,\"size\":50,\"sort\":[{\"[object Object]\":{}}]}]]]; nested: SearchParseException[[feed][2]: from[0],size[50]: Parse Failure [No mapping found for [[object Object]] in order to sort on]]; }]",
    "status": 400
  }

Elasticsearch DEBUG: 2014-09-19T03:33:04Z
  Request complete

{ error: 'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[b-srgZ7-S4WD5CSq5njRpQ][feed][0]: RemoteTransportException[[Curtis][inet[/10.0.200.94:9300]][search/phase/query]]; nested: SearchParseException[[feed][0]: from[0],size[50]: Parse Failure [Failed to parse source [{"from":0,"size":50,"sort":[{"[object Object]":{}}]}]]]; nested: SearchParseException[[feed][0]: from[0],size[50]: Parse Failure [No mapping found for [[object Object]] in order to sort on]]; }{[V31kXWvmSlyVGDW543VaJA][feed][1]: SearchParseException[[feed][1]: from[0],size[50]: Parse Failure [Failed to parse source [{"from":0,"size":50,"sort":[{"[object Object]":{}}]}]]]; nested: SearchParseException[[feed][1]: from[0],size[50]: Parse Failure [No mapping found for [[object Object]] in order to sort on]]; }{[V31kXWvmSlyVGDW543VaJA][feed][2]: SearchParseException[[feed][2]: from[0],size[50]: Parse Failure [Failed to parse source [{"from":0,"size":50,"sort":[{"[object Object]":{}}]}]]]; nested: SearchParseException[[feed][2]: from[0],size[50]: Parse Failure [No mapping found for [[object Object]] in order to sort on]]; }]',
  status: 400 }

Most helpful comment

I think this is still broken. Passing an object in as the elasticsearch rest api supports it does not get parsed to JSON correctly. Adding it to the "body" of the query and your results are not sorted. the sort field does work with simple strings passed in the array something like:

"sort": [ "date:desc", "title" ]

but not:

"sort": [ {"date": "desc"}, {"title": {"unknown_mapping":"keyword", "order": "desc"}} ]

All 9 comments

You should send those values in the body of the request. In order to do that, they need to be placed under the body: key. See the second example here for an example.

Thank you very much.

I'm still having issues, the code isn't breaking, but the results are not actually being sorted by the timestamp. The query works correctly when curled to the search api. Here is an example query:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "UPDATE.user_id",
            "query": "6387fb07-dcc6-3b0d-bfee-cfffa0deb438,502c4da1-9321-337a-b685-e2e4f9c993a7,f3d94cf3-b879-37fe-808a-3777f3573aa5,2f9633f8-3cc5-344b-bf71-fe0f78ae46da"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  },
  "from": 0,
  "size": 10,
  "sort": [
    {
      "timestamp": {
        "order": "desc",
        "ignore_unmapped": true
      }
    }
  ],
  "facets": {}
}

and in Node

{
            "query": {
                "bool": {
                    "must": [{
                            "query_string": {
                                "default_field": "UPDATE.user_id", "query": "6387fb07-dcc6-3b0d-bfee-cfffa0deb438,502c4da1-9321-337a-b685-e2e4f9c993a7,f3d94cf3-b879-37fe-808a-3777f3573aa5,2f9633f8-3cc5-344b-bf71-fe0f78ae46da"}
                        }]
                },
                "sort": [{"timestamp": {"order": "desc"}}]
            },
            "from": 0,
            "size": 10

        }

@trashguy For some reason, elastic search send the timestamp as '@timestamp' in the response. Use it in your request to sort by timestamp.

Has this issue been resolved? I'm having the same problem in API 5.3.

I sent elasticsearch a query that looks like this:

const options = {
    "ignoreUnavailable": true,
    "allowNoIndices": true,
    "requestCache": true,
    "q": "trump",
    "sort": ["published:desc"],
    "index": "*.gov"
};

const response = client.search(options);

The items don't actually sort, they return back sorted via score rather than when published.

I think this is still broken. Passing an object in as the elasticsearch rest api supports it does not get parsed to JSON correctly. Adding it to the "body" of the query and your results are not sorted. the sort field does work with simple strings passed in the array something like:

"sort": [ "date:desc", "title" ]

but not:

"sort": [ {"date": "desc"}, {"title": {"unknown_mapping":"keyword", "order": "desc"}} ]

I'd like to bump this, too. what @akilism is describing is the bahviour I'm seeing, too.

For those still looking for this, contrary to what @akilism said, putting the sort array of objects in the body worked for me. Tested on 6.3 (npm 15.1.1) using date time strings.

You should send those values in the body of the request. In order to do that, they need to be placed under the body: key. See the second example here for an example.

Can you please copy old example? The link is broken and dont work anymore.

upd.
Here it the schematic code:

client.search({
    index,
    type,
    body:{
        query: {...},
        sort: {...}
    },
    //and here other params like
    _source_includes,
    from,
    size,
    ...
})

It's strange, that the official document contains wrong description https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/6.x/api-reference.html#_search. For me, sort on the top level work only if i pass string or array of strings without direction.

Was this page helpful?
0 / 5 - 0 ratings