Sort not working with scan/scroll
Hi @olivere - Not sure if I am not using it correctly but with sort/scan and either a sorter or a sort field, I am getting empty results but without the sort, it works fine. FYI - the values are all integer 0's for now....
Any thoughts?
Thanks. PS. using this way-
elastic.NewScanService(client).Indices(indices...).Types(iTypes...).Query(query).SortBy(sorters...)
@vishalshah-org Hmm... that seems strange. Using scan and scroll has huge performance benefits; iterating through large resultsets will be much faster if sorting is disabled. The ScanService tries to use this knowledge to enable search_type=scan automatically when there is no sort order specified. The Python client has the same behavior IIRC. Besides that, there shouldn't be any difference.
But of course there could be a bug somewhere. Can you enable tracing to see what's happening on the wire?
BTW: ES 2.1 changes this behavior and deprecates search_type=scan. Are you on 1.x or 2.x?
@vishalshah-org Any updates on this?
@olivere Hi, sorry for the late reply. I am on 1.x. WIll post with tracing enabled. Good to know about the performance benefits. I am trying to get all objects using scan/scroll because I dont know upfront how many exist - to set the right from-size. But there is the option of running a count query and then setting the right from/size. I am dealing with 100's of objects max, so a non scan/scroll query should suffice.
Will update soon. Currently on the road. Thanks.
@vishalshah-org Didn't mean to push you. Take your time. But sometimes people just forget about their issues.
Do NOT use from/size for pagination. Beware that 2.1 introduces a limit on from/size. They simply don't want you to paginate with from/size, and they're right.
Also: In scroll-and-scan you can set a size parameter. However, it specifies how many results to return per shard.
Hi @olivere :I am first used elastic,after i did "go get gopkg.in/olivere/elastic.v3" in cmd line, i did as example show, i meet the follow error。 i confused。
import cycle not allowed package gopkg.in/olivere/elastic.v3 imports gopkg.in/olivere/elastic.v3
Any thoughts?
thandks!
Whoops. Will check as soon as I'm back home...
Am 06.12.2015 um 15:18 schrieb leokongwq [email protected]:
Hi @olivere :I am first used elastic,after i did "go get gopkg.in/olivere/elastic.v3" in cmd line, i did as example show, i meet the follow error。 i confused。
import cycle not allowed package gopkg.in/olivere/elastic.v3 imports gopkg.in/olivere/elastic.v3
Any thoughts?
thandks!
— Reply to this email directly or view it on GitHub.
@leokongwq Fixed it. Sorry.
Hi @olivere, It works, thanks !
@vishalshah-org Any news on this issue?
@olivere sorry for the delayed response. I turned on trace debugging. I don't see anything interesting in the trace logs. Just a bunch of 200 OKs. Oh and I am on elasticsearch 1.x.
Here are couple trace log files. elastic1.log has the sorter enabled and the results not returned. elastic.log has the sorter disabled and all the results are correctly returned. That's the only code change i am making, turning off the sorter.
If I notice anything else, I will update this thread.
thanks! V
PS.
this is how i define the sorter on an int property (I have also tried using sort field as well - same result)
sorter := elastic.NewFieldSort(propName).Desc().Missing(0)
this is the scan code
scanSvc := elastic.NewScanService(client).Indices(indices...).Types(iTypes...).Query(query)
if fields != nil {
scanSvc = scanSvc.Fields(fields...)
}
if fsc != nil {
scanSvc = scanSvc.FetchSourceContext(fsc)
}
if len(sorters) > 0 {
scanSvc = scanSvc.SortBy(sorters...)
}
scanCursor, err := scanSvc.Scroll(keepAlive).Size(sizePerShard).Do()
allDocs := []*elastic.SearchHit{}
for {
result, err := scanCursor.Next()
if err == elastic.EOS {
return allDocs, nil
} else if err != nil {
return nil, err
}
allDocs = append(allDocs, result.Hits.Hits...)
}
@olivere thoughts on this issue? at this point i don't have a way to sort with scan requests... thanks.
@vishalshah-org Yes, keep it open. Sorry, I didn't have the time to look into it in detail.
BTW scanning with sorting is not such a good idea if the number of results is large. It's basically like searching with pagination, which got limits in 2.1.
In 2.x, search_type=scroll is the recommended approach AFAIK. Did you try Scroll, too? Here's the code for ScrollService and some tests. However, sorting is very resource intensitive, so if you can circumvent it, that's very much recommended--see notes here.
Also, I think about implementing an Iterator-style cursor which is safe for data races, as some users experience that ES can provide data faster that Go can deserialize it (see #208).
@vishalshah-org I just added a test for Scan with sort. That works fine. Can you check the difference between your code and the test?
@olivere i have a query associated with the scan and sort. Could that cause the issue? Your test does not seem to mention a query.
Did you figure this one out, e.g. by a test case?
I did not, let me see if I can this week.. thx
Hi @olivere - just read in the docs this -
The scan search type disables sorting in order to allow very efficient scrolling through large result sets. Source: https://www.elastic.co/guide/en/elasticsearch/reference/1.7/search-request-search-type.html
That explains this issue. I did not know this. so sorry! closing 🎉
On second thought, I will let u close it 😄
Okay. I'm closing this as it's hard to tell what the problem was by reading through every comment. Just create a new issue if the problem reoccurs somehow.