I discovered today that searching a custom field for a number will return results containing either the positive or negative versions of the number, even if I do an exact search. For example: fieldName::"-2" will give me entries where that field contains either "2" or "-2". Same result if I search for fieldName::"2". The field in question is a "number" field.

Is there any workaround for this?


The reason it's happening:

When Craft's SearchService indexes the keywords for a field, they go though a normalization process that specifically removes punctuation and diacritics before being saved to the craft_searchindex table in the database.

In the case of numeric values, that's going to include things like decimals, grouping separators and negation symbols.

A workaround if you're doing this on a front-end search form, is to query the field's value directly, bypassing the SearchService. So...

{% set results = craft.entries.fieldName('-2').find() %}

Which is going to try and do a direct match against that field's actual value in the craft_content table.

There are a few feature requests you might want to add your voice to that would make your situation better as well:



  • Gotcha. Unfortunately I need this in the control panel entries list. I'll add a comment to that FR. Nov 14 '17 at 16:38

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.