<p>You can now use the <a href="https://craftcms.com/docs/config-settings#defaultSearchTermOptions" rel="noreferrer"><code>attribute</code></a> parameter in your search options, to set the fields the search term should apply to (feature introduced with Craft 2.5).</p>

<p>Unfortunatelly it's currently only possible to set a single attribute, but what you can do is to use the <a href="https://github.com/aelvan/Preparse-Field-Craft" rel="noreferrer">Preparse plugin</a> to add a field to your elements
where you collect / concatenate search terms from all relevant fields, then search in this field only.</p>

<p>The settings for your <code>searchTerms</code> Preparse field:</p>

<pre><code>{{ entry.title }}
{{ entry.body }}
{{ entry.keywords }}
</code></pre>

<p>The search parameter:</p>

<pre><code>{% set searchParam = {
    query: query,
    attribute: 'searchTerms'
    subLeft: true,
    subRight: true,
} %}
</code></pre>

CC BY-SA 3.0
2016-02-10T17:07:34.703Z
2016-02-10T17:07:34.703Z
carlcs
<p>What about including only those fields you want to search:</p>

<pre><code>craft.entries.section('products').search('productName:'~ query ~' OR productDescription:'~ query ~' OR productFoo:'~ query ~' OR productBar:'~ query)
</code></pre>

<p>Yep I know, it's not very nice (but hey, it was you who started listing them). What you could do to make this ugly thing of a search parameter more manageable is to add the conditions programmatically:</p>

<pre><code>{% set searchFields = [
    'productName',
    'productDescription',
    'productFoo',
    'productBar',
] %}

{% set searchParam %}

    {% for searchField in searchFields %}
        {{- not loop.first ? 'OR' -}}
        {{- searchField ~ ':' ~ query -}}
    {% endfor %}

{% endset %}

{% set results = craft.entries.section('products').search(searchParam|trim) %}
</code></pre>

CC BY-SA 3.0
2014-08-28T05:59:51.040Z
Actually, I have tried that too. Specifically: `craft.entries.section('products').search('productName:'~ query ~' OR productDescription:'~ query`and it also returns the product matching on relatedProducts. In fact it seems adding any `OR` in there makes it search every field. In the documentation, the `OR` examples searches for two different words, but always within the same field, like `body:salty OR body:dog` and `body:salty -body:dog`. So maybe this approach is not even supported.
oyvind-redrabbits
CC BY-SA 3.0
2014-08-28T06:02:41.950Z
@oyvind tried it with success?
carlcs
CC BY-SA 3.0
2014-08-28T06:04:22.940Z
Hit enter too early @carlcs - edit above :)
oyvind-redrabbits
CC BY-SA 3.0
2014-08-28T06:06:43.787Z
Hmm.. that's no good. Did you try rebuilding your search indexes with that tool in the CP settings?
carlcs
CC BY-SA 3.0
2014-08-28T06:30:52.333Z
@oyvind I just tested it and can confirm that it's not working. Seems like a bug to me though, that's why I did a [bug report on g+](https://plus.google.com/101325980081569498457/posts/WHjHahiEAsJ).
carlcs
CC BY-SA 3.0
2014-09-03T09:34:34.350Z
@oyvind tested the code with 2.2 and it now it works.
carlcs
CC BY-SA 3.0
2014-09-04T10:46:15.297Z
Great @carlcs - also got an email notification from Brandon at Pixel & Tonic about the fix being included in 2.2 - great and super fast support as always.
oyvind-redrabbits
CC BY-SA 3.0
2015-08-26T16:06:17.280Z
Does anyone know if this is working yet?
Clive Portman
CC BY-SA 3.0
2015-08-26T16:11:12.730Z
@ClivePortman yes, having problems getting it working?
carlcs
CC BY-SA 3.0
2015-08-27T05:58:58.710Z
@carlcs No, just wanted to check whether it was worth trying to use this. Thanks for clarifying.
Clive Portman
CC BY-SA 3.0
2015-08-27T07:31:56.933Z
@ClivePortman ahh I see, it's not marked as accepted... People forget about that, I guess!
carlcs
CC BY-SA 3.0
2014-08-27T13:59:42.973Z
2014-09-03T09:33:22.953Z
2014-09-03T09:33:22.953Z
carlcs
<p>Is it possible to exclude some fields when using search?</p>

<p>Getting the q parameter from the URL and searching the products section including all fields:</p>

<pre><code>{% set query = craft.request.getParam('q') %}
{% set results = craft.entries.section('products').search(query) %}
</code></pre>

<p>This searches all fields, included those for "related articles" and "related products". I need to exclude those from the search.</p>

<p><strong>Some experiments</strong></p>

<p>Works to exclude hits in relatedProducts but limits the fields being searched to the productName field:</p>

<pre><code>craft.entries.section('products').search('productName:'~ query ~' -relatedProducts:'~ query)
</code></pre>

<p>Does not work - also returns the product with a related product matching the query:</p>

<pre><code>craft.entries.section('products').search(query ~' -relatedProducts:'~ query)
</code></pre>

<p>and the same when trying to include several fields before excluding relatedProducts:</p>

<pre><code>craft.entries.section('products').search('productName:'~ query ~' OR productDescription:'~ query ~' -relatedProducts:'~ query)
</code></pre>

<p>Also tried searching just in the relatedProducts, confirming that it returns only the product with a related entry matching the query:</p>

<pre><code>craft.entries.section('products').search('relatedProducts:'~ query)
</code></pre>

<search>
Excluding fields in a search
# Excluding fields in a search

(Asked by: username_0 on 2014-08-27)

Is it possible to exclude some fields when using search?
Getting the q parameter from the URL and searching the products section including all fields:
{% set query = craft.request.getParam('q') %}
{% set results = craft.entries.section('products').search(query) %}

This searches all fields, included those for "related articles" and "related products". I need to exclude those from the search.
Some experiments
Works to exclude hits in relatedProducts but limits the fields being searched to the productName field:
craft.entries.section('products').search('productName:'~ query ~' -relatedProducts:'~ query)

Does not work - also returns the product with a related product matching the query:
craft.entries.section('products').search(query ~' -relatedProducts:'~ query)

and the same when trying to include several fields before excluding relatedProducts:
craft.entries.section('products').search('productName:'~ query ~' OR productDescription:'~ query ~' -relatedProducts:'~ query)

Also tried searching just in the relatedProducts, confirming that it returns only the product with a related entry matching the query:
craft.entries.section('products').search('relatedProducts:'~ query)

**Tags:** search


---

## 2 answers

### Answer by username_1 on 2016-02-10. Score: 5

You can now use the attribute parameter in your search options, to set the fields the search term should apply to (feature introduced with Craft 2.5).
Unfortunatelly it's currently only possible to set a single attribute, but what you can do is to use the Preparse plugin to add a field to your elements
where you collect / concatenate search terms from all relevant fields, then search in this field only.
The settings for your searchTerms Preparse field:
{{ entry.title }}
{{ entry.body }}
{{ entry.keywords }}

The search parameter:
{% set searchParam = {
    query: query,
    attribute: 'searchTerms'
    subLeft: true,
    subRight: true,
} %}


---

### Answer by username_1 on 2014-08-27. Score: 4

What about including only those fields you want to search:
craft.entries.section('products').search('productName:'~ query ~' OR productDescription:'~ query ~' OR productFoo:'~ query ~' OR productBar:'~ query)

Yep I know, it's not very nice (but hey, it was you who started listing them). What you could do to make this ugly thing of a search parameter more manageable is to add the conditions programmatically:
{% set searchFields = [
    'productName',
    'productDescription',
    'productFoo',
    'productBar',
] %}

{% set searchParam %}

    {% for searchField in searchFields %}
        {{- not loop.first ? 'OR' -}}
        {{- searchField ~ ':' ~ query -}}
    {% endfor %}

{% endset %}

{% set results = craft.entries.section('products').search(searchParam|trim) %}

> **Comments:**
> **username_0**: Actually, I have tried that too. Specifically: `craft.entries.section('products').search('productName:'~ query ~' OR productDescription:'~ query`and it also returns the product matching on relatedProducts. In fact it seems adding any `OR` in there makes it search every field. In the documentation, the `OR` examples searches for two different words, but always within the same field, like `body:salty OR body:dog` and `body:salty -body:dog`. So maybe this approach is not even supported.  
> **username_1**: @oyvind tried it with success?  
> **username_0**: Hit enter too early @username_1 - edit above :)  
> **username_1**: Hmm.. that's no good. Did you try rebuilding your search indexes with that tool in the CP settings?  
> **username_1**: @oyvind I just tested it and can confirm that it's not working. Seems like a bug to me though, that's why I did a [bug report on g+](https://plus.google.com/101325980081569498457/posts/WHjHahiEAsJ).  
> **username_1**: @oyvind tested the code with 2.2 and it now it works.  
> **username_0**: Great @username_1 - also got an email notification from Brandon at Pixel & Tonic about the fix being included in 2.2 - great and super fast support as always.  
> **username_2**: Does anyone know if this is working yet?  
> **username_1**: @ClivePortman yes, having problems getting it working?  
> **username_2**: @username_1 No, just wanted to check whether it was worth trying to use this. Thanks for clarifying.  
> **username_1**: @ClivePortman ahh I see, it's not marked as accepted... People forget about that, I guess!  

---

