<p>In your last example:</p>
<pre class="lang-php prettyprint-override"><code>$criteria = [
    'section' =&gt; $section_handle, 
    'excerpt' =&gt; &quot;*{$q}*&quot;
];
</code></pre>
<p>You are making a <a href="https://en.wikipedia.org/wiki/Approximate_string_matching" rel="nofollow noreferrer">fuzzy search</a> by surrounding your keyword(s) with <code>*</code> (<a href="https://craftcms.com/docs/3.x/searching.html#supported-syntaxes" rel="nofollow noreferrer">see docs for syntaxes</a>) and I wonder if that's why you are seeing results (I'd need to see the keywords/entry).</p>
<p>However, if that works, then this should work for you:</p>
<pre class="lang-php prettyprint-override"><code>'criteria' =&gt; [
    'section' =&gt; $section_handle,
    'search' =&gt; ($q ? 'title:' . $q . ' OR '.'excerpt:*'. $q . '*' : null),
],
</code></pre>
<p>Alternatively, make everything fuzzy (not <em>as</em> precise):</p>
<pre class="lang-php prettyprint-override"><code>'criteria' =&gt; [
    'section' =&gt; $section_handle,
    'search' =&gt; ($q ? 'title:*' . $q . '* OR '.'excerpt:*'. $q . '*' : null),
],
</code></pre>
<p>One final thought, if your section has three fields, if you only check <em>Use this field’s values as search keywords</em> on your Settings → Fields → excerpt field then you can search on both title + excerpt simply by doing:</p>
<pre class="lang-php prettyprint-override"><code>'criteria' =&gt; [
    'section' =&gt; $section_handle,
    'search' =&gt; ($q ?? null),
],
</code></pre>

CC BY-SA 4.0
2020-12-05T11:58:05.797Z
2020-12-05T11:58:05.797Z
Oli
<p>thank you for your answer. It did help as in I understand it a bit better, and I finally got it to work!</p>
<p>The solution seems to be this:</p>
<p><strong>check the excerpts field 'use this field's values as search keywords'</strong></p>
<p>But then I had to recreate the entries and only then the searching worked!</p>
<p>I am very happy I got it to work now, thanks again Oli!!!</p>

CC BY-SA 4.0
2020-12-07T05:44:00.947Z
Oh! I see :) To re-index your entries, you can run `./craft resave/entries --update-search-index` ([docs](https://craftcms.com/docs/3.x/console-commands.html#resave-entries)) rather than having to recreate them.
Oli
CC BY-SA 4.0
2020-12-06T08:24:25.530Z
2020-12-06T09:05:46.110Z
2020-12-06T09:05:46.110Z
Wouter
<p>I am using the Element API to search for entries that match a search query: either the title <strong>OR</strong> the excerpt should contain the query term. The entry is very simple and has these fields:</p>
<p>-title<br />
-excerpt<br />
-link</p>
<p>when I use this to only search in the title field it works:</p>
<pre><code>$section_handle = ['item'];
$q = Craft::$app-&gt;request-&gt;getParam('query');

$criteria = [
   'section' =&gt; $section_handle, 
   'search' =&gt; 'title:'.$q
];
</code></pre>
<p>So I thought I just add the excerpt field like this:</p>
<pre><code>$criteria = [
   'section' =&gt; $section_handle, 
   'search' =&gt; 'title:'.$q.' OR '.'excerpt:'.$q
];
</code></pre>
<p>but this did NOT work and I found out that it just won't search that excerpt field, this also does not work:</p>
<pre><code>   $criteria = [
   'section' =&gt; $section_handle, 
   'search' =&gt; 'excerpt:'.$q
];
</code></pre>
<p>After some research I found out that when I use this as the 'criteria' it works:</p>
<pre><code>$criteria = [
    'section' =&gt; $section_handle, 
    'excerpt' =&gt; &quot;*{$q}*&quot;
];
</code></pre>
<p>And now I wonder how to proceed. How can I either get the first example to work to search in the excerpt field, or add the OR title clause to my latest example where I am able to search in the excerpt field...</p>
<p>I hope this makes sense and that someone can point me in the right direction!</p>
<p>Thanks very much,</p>
<p>Wouter</p>

<craft3><plugin-elementapi>
How to search on multiple (custom) fields using Element API
# How to search on multiple (custom) fields using Element API

(Asked by: username_0 on 2020-12-04)

I am using the Element API to search for entries that match a search query: either the title OR the excerpt should contain the query term. The entry is very simple and has these fields:
-title
-excerpt
-link
when I use this to only search in the title field it works:
$section_handle = ['item'];
$q = Craft::$app->request->getParam('query');

$criteria = [
   'section' => $section_handle, 
   'search' => 'title:'.$q
];

So I thought I just add the excerpt field like this:
$criteria = [
   'section' => $section_handle, 
   'search' => 'title:'.$q.' OR '.'excerpt:'.$q
];

but this did NOT work and I found out that it just won't search that excerpt field, this also does not work:
   $criteria = [
   'section' => $section_handle, 
   'search' => 'excerpt:'.$q
];

After some research I found out that when I use this as the 'criteria' it works:
$criteria = [
    'section' => $section_handle, 
    'excerpt' => "*{$q}*"
];

And now I wonder how to proceed. How can I either get the first example to work to search in the excerpt field, or add the OR title clause to my latest example where I am able to search in the excerpt field...
I hope this makes sense and that someone can point me in the right direction!
Thanks very much,
username_0

**Tags:** craft3, plugin-elementapi


---

## 2 answers

### Answer by username_1 on 2020-12-05. Score: 3 | Accepted answer

In your last example:
$criteria = [
    'section' => $section_handle, 
    'excerpt' => "*{$q}*"
];

You are making a fuzzy search by surrounding your keyword(s) with * (see docs for syntaxes) and I wonder if that's why you are seeing results (I'd need to see the keywords/entry).
However, if that works, then this should work for you:
'criteria' => [
    'section' => $section_handle,
    'search' => ($q ? 'title:' . $q . ' OR '.'excerpt:*'. $q . '*' : null),
],

Alternatively, make everything fuzzy (not as precise):
'criteria' => [
    'section' => $section_handle,
    'search' => ($q ? 'title:*' . $q . '* OR '.'excerpt:*'. $q . '*' : null),
],

One final thought, if your section has three fields, if you only check Use this field’s values as search keywords on your Settings → Fields → excerpt field then you can search on both title + excerpt simply by doing:
'criteria' => [
    'section' => $section_handle,
    'search' => ($q ?? null),
],


---

### Answer by username_0 on 2020-12-06. Score: 1

thank you for your answer. It did help as in I understand it a bit better, and I finally got it to work!

The solution seems to be this:

**check the excerpts field 'use this field's values as search keywords'**

But then I had to recreate the entries and only then the searching worked!

I am very happy I got it to work now, thanks again username_1!!!

> **Comments:**
> **username_1**: Oh! I see :) To re-index your entries, you can run `./craft resave/entries --update-search-index` ([docs](https://craftcms.com/docs/3.x/console-commands.html#resave-entries)) rather than having to recreate them.  

---

