[英]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. 我正在使用mongodb构建一个基本的搜索引擎,我已经验证了mongo shell中的基本查询工作。 I am not quite understanding how this can be translated into PHP though.
我不太明白如何将其转换为PHP。
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). 基本的mongo查询(请注意我不是每次都试图翻译这个确切的查询,我需要它在$ ands和$ 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: 我在PHP中生成的代码是这样的:
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. 这是PHP代码返回的查询,您可以看到这些和条件已放入数组中。 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.
我没有看到任何存储它们的方法而不将它们推送到数组,但是看起来mongodb的$或者不喜欢接受数组,我只是不确定如何重新使用搜索算法来解决这个问题。
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 ) )
)
)
)
To explain my comment further I will tell you about the $and operator: http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24and 为了进一步解释我的评论,我将告诉你关于$和运算符: 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
您还可以在Regex中执行$和查询,这里有一些关于正则表达式语法的信息: http : //www.regular-expressions.info/refadv.html
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: 一些更高效的替代方案是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.