如何一次在多个字段中搜索多个单词?

时间:2013-12-14 10:15:42

标签: php mysql cakephp search-engine cakephp-2.3

我正在编写搜索模块。我不知道我是不是很好,但现在听起来不错,除了一点。

使用以下代码,我只能搜索一个单词。

<?php
class SearchesController extends AppController{

    function index(){

        if($this->request->is('put') || $this->request->is('post') ){
            // Save the send data
            $search = $this->request->data['Search']['key']; 

            //$nbWords = str_word_count($search);
            /*
            THIS DOES NOT WORKS
            if($nbWords > 1){
                $searchTerms = explode(' ', $search);

                $searchTermBits = array();
                foreach ($searchTerms as $term) {
                    $term = trim($term);
                    if (!empty($term)) {
                        $searchTermBits[] = "Page.name LIKE '%$term%' AND Page.content LIKE '%$term%'";
                    }
                }



            }else{
                $term = $search;
                $searchTermBits[] = "Page.name LIKE '%$term%' AND Page.content LIKE '%$term%'";

            }
            */
            // SEARCH IN Pages TABLE
            $this->loadModel('Page');
            // SAVE THE RESULT IN
            $d['pages'] = $this->Page->find('all', array(
                'conditions' => array(
                    'online'=>'1',
                    'type'=>'page',
                    'created <= NOW()',
                    'id > 1',
                    'OR' => array(
                        //implode(' AND ', $searchTermBits)
                        'Page.name LIKE'=>'%'.$search.'%',
                        'Page.content LIKE'=>'%'.$search.'%'
                        )
                    ),
                'fields' => array('name','content','created','id','type')
            ));
            //SEARCH IN Posts TABLE
            $this->loadModel('Post');
            $d['posts'] = $this->Post->find('all', array(
                'conditions' => array(
                    'online'=>'1',
                    'type'=>'post',
                    'created <= NOW()',
                    'OR' => array(
                        'Post.name LIKE'=>'%'.$search.'%',
                        'Post.content LIKE'=>'%'.$search.'%'
                        )
                    ),
                'fields'=> array('name','content','created','id','type','slug')
            ));
            // SEND THE VALUES TO THE VIEW
            $this->set($d);

        }else{
            echo 'e';
        }
    }

}

问题,我希望能够用不同的词搜索,用于“红色汽车”,“房子monrain”,“web html5开发”

使用我的代码,如果我输入一个单词,它会返回一个结果,但如果我将第二个单词添加到第一个单词(带空格),则表示没有结果。

我该如何更改

'OR' => array(
        'Post.name LIKE'=>'%'.$search.'%',
        'Post.content LIKE'=>'%'.$search.'%'
)

能够在“名称”和“内容”中搜索几个单词吗?

Thak求助 干杯

4 个答案:

答案 0 :(得分:0)

你可以试试这个:

'OR' => array(
       'Post.name' IN ('name1', 'name2'),
       'Post.content' IN ('content1', 'content2')
)

显然你可以把你的值放在数组中:

$name_arr = array();
$name_arr[] = 'name1';
$name_arr[] = 'name2';

之后:

'OR' => array(
           'Post.name' IN ($name_arr),
           'Post.content' IN ('content1', 'content2')
    )

答案 1 :(得分:0)

更改$search变量

 $search = $this->request->data['Search']['key']; 
 // if keywords "red car" then it will pass to your query %red%car%;
 $search = str_replace(" ","%",$search);

现在它将搜索包含两个单词的记录。

答案 2 :(得分:0)

如果您想搜索以空格分隔的多个单词,则可以执行此操作。

$searches = explode(" ", $search);
foreach ($searches as $search) {
   //put your search condition in here
}

答案 3 :(得分:0)

您可以查看fulltext搜索。如果正确设置索引,则可以运行以下查询:

SELECT id,MATCH(title,body)AGAINST('Tutorial')FROM articles;