phpsqlcakephpcakephp-2.0cakephp-search-plugin

CakePHP Search plugin, issue with orCondition


I had a problem with orCondition method. I'd like to create a simple search engine using this plugin in that way that I just want to find a phrase (same or similar) if it's in database in title and description, and I really don't know what am I doing wrong. Only effect I achived is I can find a title when I type it in form exactly in the same order as originally in database. For example if the post is titled "One two three" and I type "two" it works, but if I type "three two" there's no result. Here's some code.

Model:

public $actsAs = array('Search.Searchable');

public $filterArgs = array(
'title' => array('type' => 'query', 'method' => 'orConditions'),
'status' => array('type' => 'value'),
'category' => array('type' => 'value'),
'country' => array('type'=>'value'),
'continent' => array('type'=>'value'),
'cycle' => array('type'=>'value'),);

...
        public function orConditions($data = array()) {
        $title = $data['title'];
        $cond = array(
            'OR' => array(
                $this->alias . '.title LIKE' => '%' . $title . '%',
                $this->alias . '.description LIKE' => '%' . $title . '%',
            ));
        return $cond;
        }

View:

echo $this->Form->create('Ad', array('url' => array_merge(array('action' => 'index'), $this->params['pass'])));

Solution

  • I think you understand a few things wrong. The search plugin is not thought to be a search engine in any kind. I think the readme.md states as well that the plugin just implements the PRG pattern. It basically turns your POST into GET and the GET params into CakePHP find conditions. The find conditions are the "search" part of it.

    What you do is not really a "search engine", a search engine is a little more "intelligent" and complex than what you try to do. What you do is a simple SQL query with a few logical operators. What you try to build is a search index.

    The issue you have is a problem of the way you build the query, not the search plugin or CakePHP. If your title is "One two three" and your search expression is %two% it will work because it would match the title no matter where the word two is. But if you do a query using %three two% it won't match anything because it is looking for exactly that string.

    if you want to make that happen you have to split the two words up into a query like WHERE TITLE LIKE = %two% OR %three%. Or if you want to match titles that must contain both %two% AND %three%.

    I recommend you to read about Mysql full text search or how to search data using SQL in general. For building a search index there are better databases like Elastic Search available. Elastic Search is made for building search indexes. The way you try to search things will become for sure inefficient at some point, depending on what "simple" or "small" means for you.