0

我想做的是以下。

$collection = $this->db->products_sale;

        $user_query = preg_replace("/[[:blank:]]+/"," ", $data);
        $arr_query = explode(' ', $user_query);

        if (count($arr_query) > 1) {
        $tmp = array();

        foreach ($arr_query as $q) {
            $tmp[] = new MongoRegex( "/". $q ."/i" );
        }

        $who['keywords'] = array('$in' => $tmp);
        $who['title'] = array('$in' => $tmp);
        $who['description'] = array('$in' => $tmp);

        } else {
            $who['keywords'] = new MongoRegex( "/". $user_query ."/" );
            $who['title'] = new MongoRegex( "/". $user_query ."/" );
            $who['description'] = new MongoRegex( "/". $user_query ."/" );
        }

        print json_encode($who);
        $cursor = $collection->find( $who );

如您所见,我正在做一些搜索,我想做的是以下我需要能够搜索关键字、标题、描述

现在我可以做一些我认为我正在做的搜索,但显然不是。从我得到的信息来看,它似乎将一个长字符串 ARRAY 或 JSON 发送回 mongoDB,这与 MySQL 不同的是,您可以进行 OR/AND 搜索甚至 MATCH 搜索,似乎 MongoDB 并不那么聪明。

这是我发现的作品,但随后它会搜索搜索搜索的确切方式,例如

“windows phone 7”将被搜索而不是关键字。

$cursor = $collection->find( 
        array('$or' => array(
        array("keywords" => new MongoRegex( "/$user_query/i" )),
        array("product" => new MongoRegex( "/$user_query/i" )),
        array("description" => new MongoRegex( "/$user_query/i" )),
        )));
4

1 回答 1

0

对于任何想知道我最终如何让它工作的人。这很简单。我仍在努力,但这是我到目前为止所拥有的。

$collection = $this->db->products_sale;

    $user_query = preg_replace("/[[:blank:]]+/"," ", $data);
    $arr_query = explode(' ', $user_query);

    if (count($arr_query) > 1) {
    $tmp = array();

    foreach ($arr_query as $q) {
        $tmp[] = new MongoRegex( "/". $q ."/i" );
    }

    $who['keywords'] = array('$in' => $tmp);
    $who['product'] = array('$in' => $tmp);

    } else {
        $who['keywords'] = new MongoRegex( "/". $user_query ."/" );
        $who['product'] = new MongoRegex( "/". $user_query ."/" );
    }

    //print json_encode($who);
    $cursor = $collection->find( 
    array('$or' => array($who
    )));


    if ($cursor->count() > 0)
    {
    //print "found you";
    $test = array();
    // iterate through the results
    while( $cursor->hasNext() ) {   
        $test[] = ($cursor->getNext());
    }
    print json_encode($test);
于 2012-07-01T12:26:25.013 回答