Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Scout orderBy() not supported #86

Closed
denudge opened this issue Oct 28, 2017 · 6 comments
Closed

Scout orderBy() not supported #86

denudge opened this issue Oct 28, 2017 · 6 comments
Labels

Comments

@denudge
Copy link

denudge commented Oct 28, 2017

The function orderBy() on the Scout query builder has no effect here (or is buggy).

@kronthto
Copy link
Contributor

kronthto commented Oct 28, 2017

Can you provide an example of what you tried?

I have an application where I'm using ->orderBy('created_at', 'desc') and it is working just fine. In my case the value of this field is an integer; I think if you want to order by a date or another non-int type you'll have declare what the field contains in the Elastic index by adding a mapping: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html / #47 (comment)

@denudge
Copy link
Author

denudge commented Oct 28, 2017

In my case, I used two simple text fields (name, description). I did not create any special mapping beforehand.
ES has the following:
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}

Then, orderBy('name') didn't work.

@kronthto
Copy link
Contributor

kronthto commented Oct 28, 2017

I just tried adding a sort on a text field (asset_name in my case) and actually got this error from Elastic:
Fielddata is disabled on text fields by default. Set fielddata=true on [asset_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

Following this error message I tried changing it to the following (add .keyword):

->orderBy('asset_name.keyword')

And it worked, I got my result ordered alphabetically by this field.

Edit: Got the idea to add .keyword here: elastic/kibana#6769 (comment)

I don't feel like this is something that should be fixed in this project though, as we can't really know of what type the specified order-column is.

@denudge
Copy link
Author

denudge commented Oct 29, 2017

Thanks for the hint. Unfortunately adding ".keyword" didn't work out for me either. I will try it again later, when I have more time to track the issue down.

@sebastiansulinski
Copy link

I seem to have similar issue with Algolia implementation - here's my controller method:

public function search(Request $request): JsonResponse
{
    $query = Agent::query();

    if (!is_null($keyword = $request->get('keyword'))) {
        $query = Agent::search($keyword);
    }

    if (!is_null($sort = $request->get('sort')) && !is_null($direction = $request->get('direction'))) {
        $query->orderBy($sort, $direction === 'desc' ? 'desc' : 'asc');
    } else {
        $query->orderBy('firstname')->orderBy('lastname');
    }

    $agents = $query->paginate(10);

    return new JsonResponse($agents);
}

When I call with keyword empty - sorting works fine, but when I send the keyword through - orderBy method doesn't have any effect.

I have no default sorting set up on the algolia index for this searchable.

@stale
Copy link

stale bot commented Sep 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Sep 20, 2020
@stale stale bot closed this as completed Sep 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants