I'm new to Craft, coming over from ExpressionEngine to start doing maintenance on an agency-built Craft site, and I have what may well be a very basic question... please be gentle.

We have a page that allows searches on a particular section of entries. There is a form that has a handful of input fields corresponding to ExpressionEngine fields that can be searched. One of the input fields (locationState) always gives zero results, even though the data being searched for is there per admin and database.

When I look at craft.log, I can see that several database queries go into the search. The query that's just pulling primary keys for matching rows works fine. The next query, which wants to pull all fields for output for rows with those primary keys, looks like it has a problem:

SELECT * FROM `craft_searchindex` 
WHERE ((`keywords` LIKE '%title%') OR (`keywords` LIKE '%position%') OR (`keywords` LIKE '%jobtitle%') OR (`keywords` LIKE '%bioquick%') OR (`keywords` LIKE '%biofull%') OR (`keywords` LIKE '%institution%'))
AND `locale` = 'en_us' 
AND `elementId` IN (5355,7874,8608)

Those title, position, jobtitle, bioquick, biofull, and institution values look like they're unparsed variable names to me, like someone writing this code forgot to evaluate them for values before handing them off to the search service.


If I go into the template for this search page do a dump(params), where "params" is the name of a variable being constructed out of user input to the search form input fields, I get this:

array(6) { 
    ["section"]=> string(8) "profiles" 
    ["type"]=> string(11) "localExpert" 
    ["order"]=> string(5) "score" 
    ["limit"]=> int(25) 
    ["locationState"]=> string(2) "DC" 
    ["search"]=> array(3) { 
        ["query"]=> string(73) "title: OR position: OR jobTitle: OR bioQuick: OR bioFull: OR institution:" 
        ["subLeft"]=> bool(true) 
        ["subRight"]=> bool(true) 

Here are title, position, jobtitle, bioquick, biofull, and institution again. The full code that dynamically creates the params variable is too long to paste, but the piece of it that addresses these 6 fields looks like this:

{% set fullquery = 'title:' ~ query ~ ' OR ' ~ 'position:' ~ query ~ ' OR ' ~ 'jobTitle:' ~ query ~ ' OR ' ~ 'bioQuick:' ~ query ~ ' OR ' ~ 'bioFull:' ~ query ~ ' OR ' ~ 'institution:' ~ query %}

where "query" is craft.request.getParam('q') and where "fullquery" eventually gets concatenated into params.

The meat of my question here is this: for my failing search, craft.request.getParam('q') is null. So I'd expect that the value of "fullquery" would be concatenating empty strings, which matches what I see when I do that dump(params), but does NOT match the SQL query I see in craft.log, with those seeming unparsed variables.

So I hope someone can help me out by at least confirming that what gets written to craft.log is the final and literal query, post all evaluation and parsing, right? Knowing that alone would help me know I'm not missing something super fundamental on my way to trying to figure out how my dumps don't match what's in craft.log, which seems pretty key to figuring out why my search is failing.

FWIW, I'm doing my dump(params) immediately before the line of the template that runs the search via {% set entries = craft.entries( params ) %}, so nothing should be changing between the dump and the logging of the db query.

Thank you, and I hope my question is clear.

  • 2
    You should check if your search string is not empty otherwise craft will (of course) search for entries that contain the word 'title' or 'position' and so on. From a developer point of view everything makes sense and is correct (what Craft does) You just have to exclude your search parameter if your search string is empty otherwise you order to search for field names – Robin Schambach Dec 16 '17 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.