_keyword search

4 posts by 3 authors in: Forums > CMS Builder
Last Post: April 20, 2011   (RSS)

Hello,

I'm using the fieldname_keyword function in a search and have only just noticed it doesn't split words...

for example a search for "art and design" returns results which have the whole string "art and design" in.

However a search for "art design" doesn't return "art and design" because it's matching the whole string, i.e. no 'and'.

Is there an easy way to make the _keyword search split multiple words in the search and intelligently return them in results?

Ideally I'd like it to return:

Art and Design
Art
Design

in that order, i.e. 'quality of match'

But that quality of match might be tricky... but at least having it split multiple searched keywords so "art design" returns result titled "art and design"

Any ideas or inspiration?!

Cheers

Re: [rjbathgate] _keyword search

By Jason - April 19, 2011

Hi,

Creating a completely intelligent search like you're suggesting, especially the "quality of match" part can be tricky. This won't completely do it, but it should get your closer.

The idea is to break up each word of your search and search for it separately. In this example, we're assuming the field we're searching on is called title, the section is called news (these can be changed), and we've dropped _keyword from the end of our variable:

$where = "";
$allowSearch = true;

if (@$_REQUEST['title']) {

$separatedWords = array_filter(explode(" ", @$_REQUEST['title']));

if($separatedWords) {

foreach ($separatedWords as $word) {
$where .= "title LIKE '%".mysql_escape($word)."%' OR ";
}

$where = rtrim($where, " OR ");
$allowSearch = false;
}


}

list($newsRecords,$newsMetaData)=getRecords(array(
'tableName' => 'news',
'allowSearch' => $allowSearch,
'where' => $where,

));


So, in this code, if we find "title" in our URL, we break up the search into separated words separated by a space. We then look for each word separately.

The one draw back here is that it doesn't distinguish between words. For example, if you search for "Art and Design", it will return any record that has "art", "and", or "design" in it's title.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [rjbathgate] _keyword search

By Dave - April 20, 2011

Hi Rob,

Also, try *_query which mimics google:
http://www.interactivetools.com/docs/cmsbuilder/viewer_search.html

However, it ANDS words together so it would only return matches with Art AND design.

What you're describing is ORing words together. The problem with that is it can sometimes be unintuitive for the user, as the more keywords they add the more results they get.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com