Jump to content

FieldTypeFile file names/description in search query


Recommended Posts

Hello. Earlier I was able to implement a simple search on a knowledge sharing project for my company but now I was asked to add some functionality where to any page some files can be attached. It went pretty straight forward to use the FieldTypeFile and show up on the frontend all attached files by names.

After completting the task I thougth that I could expand my search results allowing the query to search for a string within the attached file names and/or description. So on my working query:

$matches = $pages->find("template!=keywords,title|body|keywords~=$q, limit=10");

I tried adding the files_upload field which I created and assigned some file types. The final query got:

    $matches = $pages->find("template!=keywords,title|body|keywords|file_upload~=$q, limit=10");

but I got a long error text:

Error: Exception: SQLSTATE[HY000]: General error: 1191 Can't find FULLTEXT index matching the column list (in C:\OpenServer\domains\nowknow.pw\wire\core\PageFinder.php line 384)

#0 [internal function]: ProcessWire\PageFinder->___find(Object(ProcessWire\Selectors), Array)
#1 C:\OpenServer\domains\nowknow.pw\wire\core\Wire.php(374): call_user_func_array(Array, Array)
#2 C:\OpenServer\domains\nowknow.pw\wire\core\WireHooks.php(549): ProcessWire\Wire->_callMethod('___find', Array)
#3 C:\OpenServer\domains\nowknow.pw\wire\core\Wire.php(399): ProcessWire\WireHooks->runHooks(Object(ProcessWire\PageFinder), 'find', Array)
#4 C:\OpenServer\domains\nowknow.pw\wire\core\PagesLoader.php(216): ProcessWire\Wire->__call('find', Array)
#5 C:\OpenServer\domains\nowknow.pw\wire\core\PagesLoader.php(216): ProcessWire\PageFinder->find(Object(ProcessWire\Selectors), Array)
#6 C:\OpenServer\domains\nowknow.pw\wire\core\Pages.php(225): ProcessWire\PagesLoader->find('template!=keywo...', Array)
#7 [internal function]: ProcessWire\Pages->_

This error message was shown because: site is in debug mode. ($config->debug = true; => /site/config.php). Error has been logged.

Obviously I am not getting the correct way of searching for a file name or file description in the query. Any ideas how to accomplish that?

Link to comment
Share on other sites

You can use the %=, ^= or $= operators to match the basename in a Files or Images field.

$matches = $pages->find("file_upload%=$q");

You can use any of the operators on the description subfield.

$matches = $pages->find("file_upload.description~=$q");


  • Like 1
Link to comment
Share on other sites

@Robin S I knew it would be that easy, but was missing the point. Thank you very much. Here are the changes applied that worked just fine for my needs:

// Search the title, body and sidebar fields for our query text.
// Limit the results to 10 pages. 
// Exclude results that use the 'keywords' template. 
// Search within the following fields: title, body, keywords, file name, file description

$matches = $pages->find("template!=keywords,title|body|keywords|file_upload|file_upload.description%=$q, limit=10");

The elegance of PW is amazing me everyday even after considering the fact that I am having still a lot of PHP to learn to get to more advanced levels.

Thanks again.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...