10up / ElasticPress

A fast and flexible search and query engine for WordPress.
https://elasticpress.io
GNU General Public License v2.0
1.25k stars 311 forks source link

Searching "attachment" post type #3907

Closed jacobmasters closed 5 months ago

jacobmasters commented 5 months ago

Is your enhancement related to a problem? Please describe.

We've got a large image database that's currently searchable using a modified query like:

$args = array('s' => $_GET['search'],'ep_integrate' => true,'posts_per_page' => -1,'post_type' => 'attachment','post_status' => 'any');

which is currently slow, we only need to search the attachment titles/descriptions not the file contents in same way you would all other post types, is there a way to get elastic search to index attachments in this way?

Designs

No response

Describe alternatives you've considered

No response

Code of Conduct

felipeelia commented 5 months ago

You need to use the ep_indexable_post_types and ep_searchable_post_types filters to add attachment to the array of post_types, as we do in the Documents feature.

If you are open to index files' contents, then simply enabling the Documents feature would do it.

jacobmasters commented 5 months ago

So i did this, indexed our 200K attachments but when elasticpress is enabled i get 0 search results, disable it and it works as previously (but slowly), this is on localhost using a trial version on https://www.elasticpress.io/

function mysite_indexable_post_types( $types ) { $types['attachment'] = 'attachment'; return $types; }

function mysite_searchable_post_types( $types ) { $types['attachment'] = 'attachment'; return $types; }

add_filter( 'ep_indexable_post_types', 'mysite_indexable_post_types', 10, 1 ); add_filter( 'ep_searchable_post_types','mysite_searchable_post_types', 10, 1 );

jacobmasters commented 4 months ago

resolved that, needed "protected files" enabled as well