php mongodb全文搜索和排序 [英] php mongodb full-text search and sort

查看:88
本文介绍了php mongodb全文搜索和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  $ cursor = $ collection-> find(array('$ text'=>(array('$ search'=> $ s))),
array(score=> array('$ meta'=>textScore ))
);

我尝试用以下方式对游标进行排序:

  $ cursor = $ cursor-> sort(array(score=> 1)); 

当我尝试阅读时

 的var_dump($ CURSOR-> GETNEXT()); 

我给了我这个错误
未知的异常'MongoCursorException'带有消息'localhost:27017:无法规范化查询:BadValue不能在$ meta投影上进行非$ meta排序'



有什么想法?

解决方案

你试图在元字段上进行排序,而不是正常的字段名。



第二个参数 $ collection-> find() 决定哪个

这类似于 SELECT * ... / code> vs 在SQL数据库中选择field1,field2 ...

现在,在MongoDB 2.6有一个额外的关键字可以在这里使用, $ meta 。该关键字允许您将字段名注入到返回文档中(否则实际上不会存在)。此注入字段名的值将来自您正在执行的文档或查询的某种元数据。



$ text 查询运算符是一个运算符的例子,它具有关于匹配文档的更多信息。不幸的是,它无法告诉你这些额外的信息,因为这样做会以意想不到的方式操纵你的文档。它确实附加了元数据到文档 - 这取决于您是否需要它。



$ text运算符创建的元数据使用关键字textScore。如果你想包含这些数据,你可以将它分配给你选择的字段名称:

  array( myFieldname => array('$ meta'=>'keyword'))

,在$ text搜索(textScore)的情况下,我们可以通过将该数组作为第二个参数传递给我们的文档来将字段名score注入到 $ collection-> find()

  array(score=> array('$ meta'=>'textScore'))

现在我们已经在我们的返回文档中注入了一个名为score的字段,该文档具有$ text搜索中的textScore值。 仍然只是文档的元数据,如果您想在执行查询之前继续在后续操作中使用此值,则仍需将其作为$ meta数据引用。



这意味着,要在你的领域排序ave在 $ meta projection 上排序

  array('score'=> array('$ meta'=>'textScore'))

然后,您的完整示例变为:

 <?php 
$ mc = new MongoClient();


$ collection = $ mc-> selectCollection(myDatabase,myCollection);

$ string =搜索字符串;
$ cursor = $ collection-> find(
array('$ text'=> array('$ search'=> $ string)),
array('score' =>数组('$ meta'=>'textScore'))
);

$ cursor = $ cursor-> sort(
array('score'=> array('$ meta'=>'textScore'))
);

foreach($ cursor为$ document){
var_dump($ document);
}


i nead to make a search with full text index and this code work:

$cursor=$collection->find(array('$text'=>(array('$search'=>$s))),
                    array("score"=> array('$meta'=>"textScore"))
        );

i try to sort the cursor with:

$cursor =$cursor->sort(array("score"=>1));

when i try to read

var_dump($cursor->getNext());

i gave me this error Uncaught exception 'MongoCursorException' with message 'localhost:27017: Can't canonicalize query: BadValue can't have a non-$meta sort on a $meta projection'

any idea?

解决方案

You are attempting to sort on a meta field, not a normal fieldname.

The second argument to $collection->find() determines which fields of the document you (do/do not) want to be returned by the query.

This is similar to SELECT *... vs SELECT field1, field2 ... in SQL databases.

Now, in MongoDB 2.6 there is an additional keyword you can use here, $meta. This keyword allows you to "inject" fieldnames into the return document (that otherwise would not actually exist). The value of this injected fieldname would come from some sort of "meta data" of the document or query you are executing.

The $text query operator is an example of an operator that has more information available about the matched document.. Unfortunately, it has no way of telling you about this extra information since doing so would manipulate your document in unexpected way. It does however attach a metadata to the document - and it is up to you to decide if you have need for it or not.

The meta data the $text operator creates uses the keyword "textScore". If you want to include that data, you can do so by assigning it to a field name of your choice:

array("myFieldname" => array('$meta' => 'keyword'))

For example, in the case of $text search (textScore) we can inject the fieldname "score" into our document by passing this array as the 2nd argument to $collection->find():

array("score" => array('$meta' => 'textScore'))

Now we have injected a field called "score" into our return document which has the "textScore" value from the $text search.

But since this is still just meta data of the document, if you want to continue to use this value in any subsequent operations before executing the query, you still have to refer to it as $meta data.

This means, to sort on the field you have to sort on the $meta projection

array('score' => array('$meta' => 'textScore'))

Your full example then becomes:

<?php
$mc = new MongoClient();


$collection = $mc->selectCollection("myDatabase", "myCollection");

$string = "search string";
$cursor = $collection->find(
    array('$text' => array('$search' => $string)),
    array('score' => array('$meta' => 'textScore'))
);

$cursor = $cursor->sort(
    array('score' => array('$meta' => 'textScore'))
);

foreach($cursor as $document) {
    var_dump($document);
}

这篇关于php mongodb全文搜索和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆