Search function not returning anything.
Created 1 year ago by gonziis

I have this weird problem after migrating my project to a live server. I have identical code (I think so) on both sides, dumped my DB so it's the same there. But the search page is not working on my live page.

{% set q = request_get("q") %}
{% set query = entries("posts", "posts") %}
{% set query = query.search(q) %}
{% set posts = query.orderBy("publish_at","desc").paginate(0) %}
{{dd(query.paginate())}}

nor posts, nor dump is returning anything. But if I take out the search function and just do a pagination with sorting, the results are there, so the posts are actually there, the search function alone is somehow not returning anything. Do you have any clue? I can't think of any reasons yet of this happening.

gonziis  —  1 year ago

Also tested with manual search parameter just entering search("kp"), also 0 results, but existing results on my local machine.

ryanthompson  —  1 year ago

The search index uses SQLite so it's physically in your application (somewhere in storage directory). To rebuild your search index use php artisan streams:index and all searchable streams will rebuild their index.

mitch  —  1 year ago

Did this work for you? I recently tried adding search capabilities, but when I do php artisan streams:index it gives me the error: Call to a member function searchable() on null. If I dd($models) in SearchableScope->extend(), I see the model in there with searchable: true. But $models->filter->shouldBeSearchable() is null.

ryanthompson  —  1 year ago

Yes I get the following:

configuration.configuration is not searchable.
dashboard.dashboards is not searchable.
dashboard.widgets is not searchable.
files.disks is not searchable.
files.folders is not searchable.
Imported [Anomaly\FilesModule\File\FileModel] models up to ID: 8
All [Anomaly\FilesModule\File\FileModel] records have been imported.
navigation.menus is not searchable.
navigation.links is not searchable.
Imported [Anomaly\PagesModule\Page\PageModel] models up to ID: 33
All [Anomaly\PagesModule\Page\PageModel] records have been imported.
pages.types is not searchable.
posts.categories is not searchable.
Imported [Anomaly\PostsModule\Post\PostModel] models up to ID: 124
All [Anomaly\PostsModule\Post\PostModel] records have been imported.
posts.types is not searchable.
preferences.preferences is not searchable.
redirects.redirects is not searchable.
settings.settings is not searchable.
Imported [Anomaly\UsersModule\User\UserModel] models up to ID: 2
All [Anomaly\UsersModule\User\UserModel] records have been imported.
users.roles is not searchable.
page_link_type.pages is not searchable.
url_link_type.urls is not searchable.
files.images is not searchable.
files.documents is not searchable.
pages.default_pages is not searchable.
posts.default_posts is not searchable.
forms.forms is not searchable.
forms.notifications is not searchable.
grid.content is not searchable.
grid.html is not searchable.
files.publications is not searchable.
forms.request_reserve is not searchable.
forms.volunteer is not searchable.

Which model is failing for you?

What is $models->filter->shouldBeSearchable()?

mitch  —  1 year ago

configuration.configuration is not searchable. dashboard.dashboards is not searchable. dashboard.widgets is not searchable. files.disks is not searchable. files.folders is not searchable. null $models->filter->shouldBeSearchable() returns null as well, that's the piece of code it's using to filter out non-searchable models in Laravel\Scout\SearchableScope.

EDIT: How do I format multiple lines as code on this forum? 😄

ryanthompson  —  1 year ago

@mitch can you try using php artisan scout:import 'YourModel' and see if that works? Is it a streams entry model?

If it's a stream (assuming, being Pyro) you can try php artisan streams:import $namespace $slug as well.

mitch  —  1 year ago

In SearchableScope.php line 36ish: Call to a member function searchable() on null Which is $models->filter->shouldBeSearchable()->searchable() in vendor/laravel/scout/src/searchableScope.php: 36ish.

mitch  —  1 year ago

Also, command streams:import is not defined for me.

ryanthompson  —  1 year ago

@mitch what version are you on? Perhaps you are a bit out of date and that is causing issues?

mitch  —  1 year ago

Ok, so searchable is actually false for my Model, I must have been looking at the wrong module before.. I've added use Searchable, toSearchableArray.

ryanthompson  —  1 year ago

Cool - working then?

mitch  —  1 year ago

Nope, I've had those in my model the whole time. Is there anything else I'm missing from it?

ryanthompson  —  1 year ago

Make sure your model is bound to the generated one is all I can think of. The system might not be getting your extended model.

Like this: https://github.com/anomalylabs/pages-module/blob/2.4/src/PagesModuleServiceProvider.php#L47

mitch  —  1 year ago

Yep, that's in there. So this is happening on the FilesModel as well, which I haven't touched anything on. If I knew of another searchable model I could try it on, I'd bet it'd happen with that one, too.

ryanthompson  —  1 year ago

What's your version?

mitch  —  1 year ago

v3.4

gonziis  —  1 year ago

Everything is working for me after php artisan streams:index, but also additional question - does search function limit the results to 15 or something? Because it doesn't give me pagination even if I paginate. {% set posts = query.search(q).orderBy("publish_at","desc").paginate() %} I get 15 results, but no pagination buttons at the bottom. If I take out the search function, I get the pagination element at the bottom with 60+ pages.

<div class="pagination">
    {{posts.appends({"q":q}).render()|raw}}
</div>

This is the pagination code

ryanthompson  —  1 year ago

@gonziis check this out for pagination links: https://github.com/anomalylabs/posts-module/blob/2.4/resources/views/posts/index.twig#L11

.render() and .links() will both do it (per Laravel's paginator object).

I THINK.. limit changes per page? Maybe it's the param on the .paginate($perPage) I can't quite recall. It's defaulted to your settings value.

gonziis  —  1 year ago

I think this is not the case. If I make .paginate(5), it still shows 5 items, but no pagination, but .paginate() shows 15 items (default), still not pagination elements at the bottom. If I take out the .search(), the pagination elements suddenly work. I think this has something to do with .search() and .paginate() not working together well?

ryanthompson  —  1 year ago

What is the object you get if you dd the result of search AND paginate?

gonziis  —  1 year ago

Ok, the one which only has 1 page with limited items is just Paginator {#16429 ▶}, but without .search() I get LengthAwarePaginator {#14098 ▶}

ryanthompson  —  1 year ago

Dig around in those objects a bit. What's the total? Do you know you have multiple pages of pagination? You should be able to get some answers by looking into the data that's dumped there.

gonziis  —  1 year ago

Yes, I know there should be multiple pages. If there would only be 15 items total, I would get 3 pages with .paginate(5), but I still get a single page with just 5 items.

{% set posts = query.search(q).orderBy("publish_at","desc").paginate() %}
{{dd(posts)}}

Result:

Paginator {#16429 ▼
  #hasMore: false
  #items: PostCollection {#16584 ▼
        #items: array:15 [▶]
  }
  #perPage: 15
  #currentPage: 1
  #path: "/"
  #query: []
  #fragment: null
  #pageName: "page"
}

{% set posts = query.search(q).orderBy("publish_at","desc").paginate(5) %}
{{dd(posts)}}

Result:

Paginator {#14894 ▼
  #hasMore: false
  #items: PostCollection {#14949 ▼
        #items: array:5 [▶]
  }
  #perPage: 5
  #currentPage: 1
  #path: "/"
  #query: []
  #fragment: null
  #pageName: "page"
}

{% set posts = query.orderBy("publish_at","desc").paginate() %}
{{dd(posts)}}

Gives me this though:

LengthAwarePaginator {#14098 ▼
  #total: 690
  #lastPage: 46
  #items: PostCollection {#14074 ▼
    #items: array:15 [▶]
  }
  #perPage: 15
  #currentPage: 1
  #path: "http://sub.domain.lv/search"
  #query: []
  #fragment: null
  #pageName: "page"
}