jjcohen
jjcohen

Reputation: 59

Translating mongo query into php mongo

I am building a basic search engine using mongodb, I have verified that the basic query work in the mongo shell. I am not quite understanding how this can be translated into PHP though.

Spaces in the input string signify 'and' operators and | or pipe characters are the 'or' operators. The input query changes , but could be something along these lines (minus the quotes!):

'o g|ra'

That would be equivalent to writing:

(o&&g)||(ra)

Basic mongo query (please note I am not trying to translate this exact query everytime, I need it to be flexible in terms of the number of $ands and $ors). Have tested this and it works fine:

db.scores.find({$or:[{Title:/o/i, Title: /g/i},{Title:/ra/i}])

The code that I have produced in PHP is this:

if(strstr($textInput, '|') != FALSE)
{
    foreach($orArray as $item)
    {
        $itemMod = explode( " " , $item);
        array_push($stringArray, $itemMod);
    }

    $masterAndQueryStack = array();

    foreach ($stringArray as $varg)
    {
            $multiAndQuerySet = array();

            foreach ($varg as $obj)
            {
                $searchText = '/'. $obj .'/i';
                $regexObj = new MongoRegex( $searchText ) ; 
                $singleQuery = array('Title' => $regexObj); 
                array_push($multiAndQuerySet , $singleQuery);
            }
            array_push($masterAndQueryStack , $multiAndQuerySet);

    }

    $orAndQueryStack =  array('$or' => $masterAndQueryStack);
    return $orAndQueryStack ;
}

This is the query that has been returned by the PHP code, as you can see the and terms have been put in an array. I can't see any way of storing these without pushing them to an array, however it seems that mongodb's $or does not like accepting an array, I'm just not sure how to re-work the search algorithm to account for this.

Array 
(
    [$or] => Array
    (
        [0] => Array 
        ( 
            [0] => Array ( [Title] => MongoRegex Object ( [regex] => o [flags] => i ) )
            [1] => Array ( [Title] => MongoRegex Object ( [regex] => g [flags] => i ) ) 
        )
        [1] => Array 
        ( 
            [0] => Array ( [Title] => MongoRegex Object ( [regex] => ra [flags] => i ) ) 
        ) 
    ) 
)

Upvotes: 1

Views: 581

Answers (2)

Stennie
Stennie

Reputation: 65303

Not sure what sort of corpus of data you have to search, but there are some significant limitations with your current approach:

All of the above caveats may be fine if you don't have a large data set to search.

Some more performant alternatives would be:

Upvotes: 1

Sammaye
Sammaye

Reputation: 43884

To explain my comment further I will tell you about the $and operator: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24and

You can nest this within your first $or making:

Array 
(
    [$or] => Array
    (
        [0] => Array
        (
            [$and] => Array 
            ( 
            [0] => Array ( [Title] => MongoRegex Object ( [regex] => o [flags] => i ) )
            [1] => Array ( [Title] => MongoRegex Object ( [regex] => g [flags] => i ) ) 
            )
        )
        [1] => Array 
        ( 
            [Title] => MongoRegex Object ( [regex] => ra [flags] => i ) 
        ) 
    ) 
)

Like that. You can also perform $and queries in Regex, some info here about regex syntax: http://www.regular-expressions.info/refadv.html

Upvotes: 2

Related Questions