Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to sort #67

Open
robjbrain opened this issue May 2, 2023 · 4 comments
Open

Unable to sort #67

robjbrain opened this issue May 2, 2023 · 4 comments

Comments

@robjbrain
Copy link

Description

At present it doesn't seem possible to sort, or if it is it's not documented anywhere.

Steps to reproduce

Setup on a model in this example Post and try the following:

Post::search(['q' => $searchTerm, 'sort_by' => 'created_at:asc'])

Expected Behavior

I would expect the results to be ordered by the created_at attribute in ascending order

Actual Behavior

Instead the following error message is show:

Request Malformed: Parameter q is required.

Metadata

Typesense Driver Version: 5.2.4

@karakhanyans
Copy link
Collaborator

Hi @robjbrain,
The search method is not accepting an array, it accepts a search query string.

So you should use this as following: Post::search($searchTerm)->orderBy('created_at', 'asc');

If you have any further questions, please let us know.

@karakhanyans
Copy link
Collaborator

Hi @robjbrain, have you tried the solution that we suggested? Do you have any other problems using the drivers?

@robjbrain
Copy link
Author

robjbrain commented Jun 16, 2023

Hey sorry not replying sooner. I realised later that you're reliant on Scout to only accept a string, but that this is pretty limiting.

The orderBy syntax isn't documented in the readme for this package. A quick test shows it's working.

However at the moment i'm exclusively using the driver using "SearchMulti" to allow more advanced search.

e.g.

            $filter = ['status:=enabled'];
            if ($forumId = $request->input('forum_id')) {
                $filter[] = 'forum_id:='.$forumId;
            }

            $rawResults = Topic::search('')->searchMulti(
                [
                    [
                        'collection' => 'content',
                        'q' => $searchTerm,
                        'sort_by' => '_text_match:desc,created_at:desc',
                        'per_page' => 30,
                        'filter_by' => implode(' && ', $filter)
                    ]
                ]
            ))->raw();

            $ids = Arr::pluck(data_get($rawResults, 'results.0.hits', []), 'document.id');
            
            if ($ids) {
                $results = Topic::query()
                    ->find($ids)
                    ->sortBy(function(ContentItem $item) use ($ids) {
                        return array_search($item->getKey(), $ids);
                    });
            } else {
                $results = collect();
            }

If there isn't a better way of doing the above i'd be all ears. But it's not clear from your docs. It does feel like a very dirty hack though.

@karakhanyans
Copy link
Collaborator

@robjbrain you can rewrite it like this


$results = Topic::search($searchTerm)
            ->where('status', 'enabled')
            ->where('forum_id', $request->input('forum_id'))
            ->orderBy('created_at', 'desc')
            ->paginate(30);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants