laravel-scout-tntsearch-driver
laravel-scout-tntsearch-driver copied to clipboard
Incorrect paginate length when no results returned
For whatever reason when the paginate function is called on the search and the result is 0 it will show as 1 under total for the lengthAwarePaginator. But if i call the same paginate on a normal query that returns zero will return what is espected...
I ran a few tests but here is the example that i used (the Employee table was truncated so there are no records in the DB)
$results = Employee::search($request->userSearchTerm)->paginate(15);
dump($results);
$testSearch = Employee::search($request->userSearchTerm)->get();
$allEmployees = Employee::where('deleted_at', null)->paginate(15);
dump($testSearch);
dump($allEmployees);
dd('stop');
That will spit out
LengthAwarePaginator {#894
#total: 1
#lastPage: 1
#items: Collection {#893
#items: []
}
#perPage: 15
#currentPage: 1
#path: "http://ocas.dev/sessionkiosk/search"
#query: array:1 [
"query" => "bob"
]
#fragment: null
#pageName: "page"
}
Collection {#889
#items: []
}
LengthAwarePaginator {#861
#total: 0
#lastPage: 0
#items: Collection {#879
#items: []
}
#perPage: 15
#currentPage: 1
#path: "http://ocas.dev/sessionkiosk/search"
#query: []
#fragment: null
#pageName: "page"
}
"stop"
So im not sure if i'm doing it incorrectly or if it's a bug, any help would be appreciated
The pagination is also thrown off if you use a where clause on the search:
User::search($query)->where('organization_id', $organization->getId())->paginate();
will return the pagination before the where clause is applied, giving you page numbers on empty results.
@BillRiess I'm having a problem that I think is somewhat related to yours. The method getTotalCount
returns the number of results matching your search query in the tntsearch db. When in actual fact we want the total number of items the engine's map
method returns.
I think I have a solution. I'll be forking the repo today in an attempt to resolve this.
Well, issue #128 is closed but the last comment says it needs more work. I have the same problem, that pagination is not correct, because we apply a where clause with the search. Gives me, for example, 22 results instead of 3.
The pagination is not broken, the thing is that TNTSearch is field agnostic so the where
clause doesn't work
Thank you for that important information, but what would you do, to limit the search results to a specific id. For example multiple companies are searched through, but I only want the results for one company.
What you can do is filter through the returned collection and only return the wanted company
But I can't use the paginate function then, can't I?
I get the search results with
$searchResult = Documents::search($request->searchTerm);
Now what I tried was to do
$searchResult = $searchResult->where('company_id', $request->company);
Then the pagination comes
$paginator = $searchResult->paginate(10);
and now the final data to return
$documents = $paginator->getCollection();
After that comes code for returning the data and well, this doesn't work because $paginator has all the items of $searchResult in his belly.
This here works, but throws out the pagination:
$searchResult = Documents::search($request->searchTerm);
$documents = $searchResult->where('company_id', $request->company)->get();
I'm sure I don't get something right in how to use Scout/TNT correctly, but maybe you could give me another hint here, thank you.
I have a feeling that you don't need full text search support at all if you're searching by document id and company id.
There are many solutions depending on your particular use case. Depending on how many companies you have, you could create an index for each one
But what about when you need to search text and still paginate? I still feel this isn't working quite right.
On Thu, Feb 1, 2018, 7:56 AM Nenad Ticaric [email protected] wrote:
I have a feeling that you don't need full text search support at all if you're searching by document id and company id.
There are many solution depending on your particular use case. Depending on how many companies you have, you could create an index for each one
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/teamtnt/laravel-scout-tntsearch-driver/issues/100#issuecomment-362257762, or mute the thread https://github.com/notifications/unsubscribe-auth/AD_R3tWQLDdNy1WYkiFZIhhFJ1-22YEIks5tQbRzgaJpZM4OCm-S .
@BillRiess The pagination works correctly, but the where clause does not, because TNTSearch doesn't distinguish fields
Simply return all the results, filter them by let's say company id, and create a paginator. Performance wise it's the same, because TNTSearch does exactly this when you call the paginate() method
Ok, what helped here, was to do the following.
Instead of this:
$searchResult = Documents::search($request->searchTerm);
I got the recommendation to do this:
$keys = Documents::search($request->searchTerm)->get()->pluck('id');
$searchResult = $searchResult->whereIn('id', $keys);
With this your $searchResult
will be an Illuminate\Database\Eloquent\Builder, instead of Illuminate\Database\Eloquent\Collection.
This makes this work:
$searchResult = $searchResult->where('company_id', $request->company);
and also this:
$paginator = $searchResult->paginate(10);
Why do you even use search if you can do:
return Documents::where("document_id", $request->document_id)->paginate(15);
When I don't use search, I can't profit from the fuzzy search.
Edit: Just got a message because of my error here. I'm not searching for the id, but for a search word in the model. Sorry for the confusion.