<p>Searching against lightswitch should work. Make sure <em>Use this field’s values as search keywords</em> is checked in Settings → Fields → <strong>your field</strong> then run <code>./craft resave/entries --update-search-index</code> to (re)index your values.</p>

CC BY-SA 4.0
2020-10-15T08:01:57.293Z
2020-10-15T08:01:57.293Z
Oli
<p>Bit of an unusual one and sure must be missing something but know if we have a defined lightswitch field we want to filter against it can quite easily be done so like this:</p>
<pre><code>{{ craft.products().myField(1) }}
</code></pre>
<p>However, if I have several lightswitch fields and a front-end filtering form I don't especially want to explicitly do this against all of them, eg <code>{{ craft.products().myField(1).anotherField(9) }}</code>. Was wondering then if it is possible to build out some kind of dynamic search or filter but unless doing something crazy looks like the Craft search parameter won't filter against lightswitch fields, eg the following won't work:</p>
<pre><code>{{ craft.products().search('myField:1') }}
</code></pre>
<p>I think I have found a workaround using the <code>where()</code> parameter but feels a bit … inelegant … as essentially specifying my query now against DB values rather than against Craft elements/properties:</p>
<pre><code>{{ craft.products().where('field_myField=1') }}
</code></pre>
<p>Any thoughts on improving? Is there a way to do this in native Craft/twig without having to query against specific database values?</p>
<p>Cheers,</p>
<p>Cole</p>
<p>PS above are examples working with Craft Commerce Products. Not entirely sure if this is an issue limited to products as opposed to - say - entries but as these are both essentially elements had assumed not.</p>

CC BY-SA 4.0
2020-10-13T14:37:07.880Z
Searching against lightswitch should work. Did you tick _Use this field’s values as search keywords_ on your fields?
Oli
CC BY-SA 4.0
2020-10-14T15:22:58.120Z
Bingo - I had updated the fields to be searchable but hadn't refreshed the search index. `php craft resave/products --update-search-index` has resolved.

Thanks!
Cole Henley
CC BY-SA 4.0
2020-10-14T15:33:17.477Z
Excellent! Glad you're sorted :)
Oli
CC BY-SA 4.0
2020-10-15T04:27:30.640Z
Someone want to add an official answer for this? :)
Brad Bell
<craft3><templating><search><filter><lightswitch>
Filtering by lightswitch fields
# Filtering by lightswitch fields

(Asked by: username_0 on 2020-10-13)

Bit of an unusual one and sure must be missing something but know if we have a defined lightswitch field we want to filter against it can quite easily be done so like this:
{{ craft.products().myField(1) }}

However, if I have several lightswitch fields and a front-end filtering form I don't especially want to explicitly do this against all of them, eg {{ craft.products().myField(1).anotherField(9) }}. Was wondering then if it is possible to build out some kind of dynamic search or filter but unless doing something crazy looks like the Craft search parameter won't filter against lightswitch fields, eg the following won't work:
{{ craft.products().search('myField:1') }}

I think I have found a workaround using the where() parameter but feels a bit … inelegant … as essentially specifying my query now against DB values rather than against Craft elements/properties:
{{ craft.products().where('field_myField=1') }}

Any thoughts on improving? Is there a way to do this in native Craft/twig without having to query against specific database values?
Cheers,
Cole
PS above are examples working with Craft Commerce Products. Not entirely sure if this is an issue limited to products as opposed to - say - entries but as these are both essentially elements had assumed not.

**Tags:** craft3, templating, search, filter, lightswitch
> **Comments:**
> **username_1**: Searching against lightswitch should work. Did you tick _Use this field’s values as search keywords_ on your fields?  
> **username_0**: Bingo - I had updated the fields to be searchable but hadn't refreshed the search index. `php craft resave/products --update-search-index` has resolved.  Thanks!  
> **username_1**: Excellent! Glad you're sorted :)  
> **username_2**: Someone want to add an official answer for this? :)  

---

## 1 answers

### Answer by username_1 on 2020-10-15. Score: 2 | Accepted answer

Searching against lightswitch should work. Make sure *Use this field’s values as search keywords* is checked in Settings → Fields → **your field** then run `./craft resave/entries --update-search-index` to (re)index your values.


---

