question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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 }

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
akilismcommented, Aug 21, 2017

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"}} ]
0reactions
iva666kacommented, Aug 2, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sorting by top is still broken and NOT RESOLVED - Reddit
Any update? Sorting by year is still broken for me, sorting by month seems to be fixed tho.
Read more >
Sort broken for date · Issue #1412 · mikefarah/yq - GitHub
I am trying to sort by a dictionary by a value that contains numbers. The sorting does not work, regardless of whether I...
Read more >
Sort not working the way its supposed to. - Microsoft Community
I have a spreadsheet with 8 columns and 1264 rows. I'm trying to do a simple sort on one of the columns. I...
Read more >
Array sort is not working correctly in JavaScript - Stack Overflow
You sorting is failing because your comparison function does not meet the specifications for Array.sort :.
Read more >
Sorting is broken | WordPress.org
When sorting by last name, I'm getting a result that looks like this: Edwards Edwards Farley Ertel Fagin etc. There's inconsistent results when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found