2

I have implemented the standard search form at: https://docs.craftcms.com/v2/templating/search-form.html

and added fuzzy searching:

{% set entries = craft.entries({
search: {
    query: query,
    subLeft: true,
    subRight: true
}
}) %}

as per:

https://docs.craftcms.com/v2/searching.html

And this works well for finding entries using a single search word, even if it is misspelled.

However I need to be able to find entries that contain more than one search term, eg:

Toronto AND French

And for that I get "No results found" in search results.

Searching for "Toronto French" will result in "Your search for “Toronto French” didn’t return any results."

The page:

https://docs.craftcms.com/v2/searching.html Does describe an OR search:

salty OR dog containing either “salty” or “dog” (or both).

But I do not seem to be able to find a way to AND...

Is there a way to do this?

Much thanks ahead of time......

  • Isn't that enough salty dog -> 'containing both “salty” and “dog”.' you could maybe create your own function to parse an and between your words but it's probably easier to use the default way without the and -> separate your terms with a whitespace – Robin Schambach Jun 9 '18 at 17:31
  • Hi Robin. I have an Entry that has the words "Toronto" and "French" in it. Searching for "Toronto OR French" will find the entry. Searching for "Toronto French" will result in "Your search for “Toronto French” didn’t return any results." So unfortunately separating search terms with a whitespace does not work :( – ironfish Jun 9 '18 at 17:40
  • I am searching a Section that has multiple fields in it. One field contains "Toronto", another field contains "French". Maybe if both words were in a single field then searching for "Toronto French" would work? Is there maybe a way to tell Craft to search all fields? Maybe I have neglected to do that? – ironfish Jun 9 '18 at 17:44
  • Searching using two words and a whitespace between them will work if the words are in the same field. But not if they are in different fields. If using OR then the search will work with words being in different fields. I have rebuilt the search index, done a lot of Googling to try and find a solution, but can't seem to find anything to resolve this. Any help much appreciated. – ironfish Jun 10 '18 at 12:35
  • It can be done with a normal query craft.entries.fieldHandleOne('*term*').fieldHandleTwo('*term*').all() if your fields are known /specified. Otherwise you'll need to prepend the field handle in your search. Or you need to create a custom search – Robin Schambach Jun 10 '18 at 12:39
2

This section has about 30 fields I would like a user to be able to search for any text in any field, so I wont know the field names ahead of time. The user will not know the handles of the fields so that is not an option unfortunately.

I don't think this is going to be possible natively. Maybe if the 30 fields were all relational (entries, users, tags, categories, etc.), you could pull it off with some multi-step relation queries, but it wouldn't be pretty.

Alternatively, you could write a custom plugin that implemented its own custom search logic or there are some existing plugins that integrate with external search providers like Algolia that might do what you're looking for.

  • There seems to be very little documentation on how to do this kind of search in a custom plugin. Respectfully, I do appreciate that I could write a custom plugin, and that is already obvious, but without specifying at all how to write some custom search logic, or pointing to any docs or examples, I am not able to proceed. For this reason I am unvoting the above being an answer. – ironfish Jul 2 '18 at 18:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.