Do you have some feature in mind that you'd love to see in Couch? Let us know.
17 posts Page 1 of 2
Hi All,

Quick one here, is it possible using <cms:php> to run a MySQL query to perform live search on the front end with:

http://twitter.github.io/typeahead.js/

Also, would it be possible to implement a similar feature in the core of the backend, I have a project that will have many, many cloned pages and searching for them rather than paginating through them all.

:)
As mentioned in viewtopic.php?f=3&t=7258, the next Couch release (1.4.5) will have a search field in the admin to make it easier to find cloned pages. For 1.4.5 it will however not be live or have any type of autocomplete feature.

Nevertheless, on the front end we can accomplish this right now with relative ease. If we take a look at the examples (http://twitter.github.io/typeahead.js/examples/), we can see that the script requires a dataset to search. At minimum this could simply be a list of page titles - equivalent to the first example with countries. The relevant dataset for that is http://twitter.github.io/typeahead.js/data/countries.json. In Couch we could accomplish something similar with a separate template like so:
Code: Select all
<cms:content_type 'application/json'/>

[
    <cms:pages masterpage='blog.php'>
        "<cms:addslashes><cms:show k_page_title/></cms:addslashes>"<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
    </cms:pages>
]
If you need further assistance, let us know and I'm sure we'll get it sorted out. ;)
Cheers Cheesypoof - I'll have a play this evening and if I get it working I'll post for the community.
Hi Cheesypoof,

Just re-visiting this now and while I'll be able to get that working I just clocked that it's a basic implementation that may lead to issues with memory exhaustion (i.e. outputting a single remote list file of more than 300ish entries) on the shared server which is why I was planning to use the remote method an port an implementation I had previously done for an e-commerce site, example code below :


Code: Select all
<? 
$query = get the query passed from the typeahead input;
$searchitems = Db_DbHelper::objectArray('
select * the_table where like :query',array('query'=>'%'.$query.'%'));
         foreach ($searchitems as $results) {
          $results[] = array(
          'name' => $title,
          'link' => $page_url,
          'value' => $title,
          'tokens' => explode( ' ', $product->meta_keywords ),
   );
      
      
}
   echo json_encode( $results );
?>
Hi Patrick,

If the dataset fetched is not very large, using cheeypoof's approach would be the simplest. With caching 'on' in config, 99% of time the visitor will be served a static copy with no database involvement at all.

If the dataset is relatively non-volatile and is not likely to change as often as the other Couch managed contents, we can even deploy our own caching scheme (i.e. saving the output of the cms:pages in a text file. An example could be the twitter addonwe have).

The point of using cms:pages tag is the relative ease of use.
If, however, one is conversant with SQL, there is no reason why we cannot bypass Couch altogether and get the data straight from the database (using <cms:php> is the functionally equivalent of using straight PHP so, to answer your original query, yes, it can be used here).

As for the queries involved, the cardinal fields (page_name, title, id etc.) are stored in a single table so that should be a no-brainer. Those fetching custom fields will involve a little more work as this would take joins into other tables.

Do let us know if you happen to require any help.

Thanks.
Hi there,

this is something I'd really like to use on the website I'm working on. I don't really understand though how to integrate this part:
Code: Select all
<cms:content_type 'application/json'/>

[
    <cms:pages masterpage='blog.php'>
        "<cms:addslashes><cms:show k_page_title/></cms:addslashes>"<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
    </cms:pages>
]

in the javascript.
nevermind I've got it. ^^

(using the following line)
Code: Select all
$.get("database.php");


Edit: nope I was to quick to celebrate, still doesn't work :(
Hi @cheesypoof is it possible to use the inbuilt search <cms:search feature with typeahead on a a template? if possible, could you direct me to a tutorial or sample.

I appreciate your help.
@Saskia, I believe I missed your edit. You merely need to pass the URL (of the Couch template which outputs the dataset) to the typeahead script, which will perform the AJAX request itself - see https://twitter.github.io/typeahead.js/examples/#prefetch.

@jephcol, There is an example at https://twitter.github.io/typeahead.js/examples/#remote which shows something similar. I don't think cms:search (MySQL's Fulltext search) will perform too well in this situation however... Excerpt from the documentation (docs/concepts/using-search.html) concerning search limitations:
It cannot be used to search for words that are less than four characters in length (considers them unimportant).
Also fulltext search does not match partial words.
Additionally, there is an aspect of this which I have been unable to wrap my head around. Let's say that you have a clonable template with countries. You search for "Paris" in the typeahead field, and Couch correctly returns the "France" page because "Paris" was mentioned in a description field. Typeahead.js appears to only be concerned with matching your search term to a single field - usually the title. Paris is not a text match for France and so France is not displayed.

How many pages do you have for this template? Is it just the title field that you wish to search in, or every field of a page?
Thanks @cheesypoof for your reply. I am building a small application that displays locations from locations.php - my clonable template. The number of pages is about 69. At the moment the live search was targeting the page title.
17 posts Page 1 of 2
cron