<p>If you want to enable fuzzy searching globally (across the entire Craft install - CP, front-end, ElementAPI, etc.), you can use the <a href="https://craftcms.com/docs/config-settings#defaultSearchTermOptions" rel="nofollow">defaultSearchTerms</a> config setting.</p>

<p>You can also just do it programmatically from the ElementAPI plugin with something like:</p>

<p>Suffix wildcard:</p>

<pre><code>'criteria' =&gt; ['title' =&gt; $search ~ '*']
</code></pre>

<p>Prefix wildcard:</p>

<pre><code>'criteria' =&gt; ['title' =&gt; '*' ~ $search]
</code></pre>

<p>Both sides:</p>

<pre><code>'criteria' =&gt; ['title' =&gt; '*' ~ $search ~ '*']
</code></pre>

<p>Or you can use any of the other search syntaxes listed on the <a href="https://craftcms.com/docs/searching#supported-syntaxes" rel="nofollow">search docs</a> page.</p>

CC BY-SA 3.0
2016-10-07T18:12:24.760Z
2016-10-07T18:12:24.760Z
Brad Bell
<p>What is the best way to implement a typeahead search in Craft. I looked at using ElementAPI to do something similar at this example but it doesn't seem to allow me to wildcard search</p>

<p><a href="https://craftcms.com/docs/templating/search-form" rel="nofollow">https://craftcms.com/docs/templating/search-form</a></p>

<p>Here's an example of the endpoints I have configured:</p>

<p>I have one entry with a title of "test" when I call the API doing </p>

<p>When I call <code>/entries/te</code> its returns 0 results. When I call <code>/entries/test</code> it returns 1 result.</p>

<pre><code>&lt;?php

namespace Craft;
return [
    'endpoints' =&gt; [

        'entries/&lt;search:\w+&gt;' =&gt; function($search) {
            return [
                'elementType' =&gt; 'Entry',
                'criteria' =&gt; ['title' =&gt; $search]
            ];
        }

    ]
];
</code></pre>

CC BY-SA 3.0
2016-10-06T18:12:13.820Z
Could you elaborate on what you mean by "wildcard search?" What have you tried, already, within your `criteria` block in the ElementAPI configuration?
August Miller
CC BY-SA 3.0
2016-10-06T18:30:36.633Z
Essentially,  I want to be able to search for entries using any syntaxes mention here https://craftcms.com/docs/searching. When I use the ElementAPI is only returns the results when using the exact word.
jpoiri
CC BY-SA 3.0
2016-10-06T18:37:29.923Z
Can you edit the original question and share the relevant portion of your Element API config where you're trying to perform the search?
Brad Bell
CC BY-SA 3.0
2016-10-06T20:15:07.777Z
Ahh, not so much element-api related, but basically this answer is what you're looking for I'm guessing? http://craftcms.stackexchange.com/a/5477/57
Brad Bell
CC BY-SA 3.0
2016-10-07T13:32:50.293Z
@BradBell this is exactly what I would like to do but throught the ElementAPI so  can return JSON
jpoiri
<search>
Implementing search with Ajax
# Implementing search with Ajax

(Asked by: username_0 on 2016-10-06)

What is the best way to implement a typeahead search in Craft. I looked at using ElementAPI to do something similar at this example but it doesn't seem to allow me to wildcard search
https://craftcms.com/docs/templating/search-form
Here's an example of the endpoints I have configured:
I have one entry with a title of "test" when I call the API doing 
When I call /entries/te its returns 0 results. When I call /entries/test it returns 1 result.
 [

        'entries/' => function($search) {
            return [
                'elementType' => 'Entry',
                'criteria' => ['title' => $search]
            ];
        }

    ]
];

**Tags:** search

> **Comments:**
> **username_1**: Could you elaborate on what you mean by "wildcard search?" What have you tried, already, within your `criteria` block in the ElementAPI configuration?  
> **username_0**: Essentially,  I want to be able to search for entries using any syntaxes mention here https://craftcms.com/docs/searching. When I use the ElementAPI is only returns the results when using the exact word.  
> **username_2**: Can you edit the original question and share the relevant portion of your Element API config where you're trying to perform the search?  
> **username_2**: Ahh, not so much element-api related, but basically this answer is what you're looking for I'm guessing? http://craftcms.stackexchange.com/a/5477/57  
> **username_0**: @BradBell this is exactly what I would like to do but throught the ElementAPI so  can return JSON  

---

## 1 answers

### Answer by username_2 on 2016-10-07. Score: 2

If you want to enable fuzzy searching globally (across the entire Craft install - CP, front-end, ElementAPI, etc.), you can use the defaultSearchTerms config setting.
You can also just do it programmatically from the ElementAPI plugin with something like:
Suffix wildcard:
'criteria' => ['title' => $search ~ '*']

Prefix wildcard:
'criteria' => ['title' => '*' ~ $search]

Both sides:
'criteria' => ['title' => '*' ~ $search ~ '*']

Or you can use any of the other search syntaxes listed on the search docs page.


---

