10up / ElasticPress

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

Search for document file type or mime type? #3917

Closed andreaslindahl closed 1 month ago

andreaslindahl commented 2 months ago

Describe your question

I just activated the Documents feature on a website running a local instance of ElasticSearch. I would like the possibility to search for file type, for example "pdf", ".pdf" or similar, but that doesn't seem to work. I have lots of pdf files in the media gallery but searching for "pdf" only results in 1 search hit.

Is it possible to add the file type, URL etc to the index so that visitors can search for all pdfs, for example?

Code of Conduct

felipeelia commented 1 month ago

HI @andreaslindahl,

As WordPress stores some data about the file in a custom meta field, you'll need to tell ElasticPress to index that specific meta field.

You can do that by going to your WP Dashboard > ElasticPress > Search Fields & Weighting, then locate the Media box, open the Metadata collapsible, add _wp_attachment_metadata, make it searchable, and click on Save changes. After that, run a full sync.

This should make files appear as search results in the front end. If you want to search for them in the dashboard as well, you can add this snippet to your codebase:

add_filter(
    'ep_search_fields',
    function( $search_fields ) {
        if ( ! is_array( $search_fields ) ) {
            return $search_fields;
        }
        $search_fields[] = 'meta._wp_attachment_metadata.value';
        return $search_fields;
    }
);