简体   繁体   English

php mongodb全文搜索和排序

[英]php mongodb full-text search and sort

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' 我给了我这个错误未捕获的异常'MongoCursorException'消息'localhost:27017:无法规范化查询:BadValue不能对$ meta投影进行非$ meta排序'

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. $collection->find()的第二个参数确定查询返回的文件的哪些字段(是/否)。

This is similar to SELECT *... vs SELECT field1, field2 ... in SQL databases. 这类似于SQL数据库中的SELECT *... vs SELECT field1, field2 ...

Now, in MongoDB 2.6 there is an additional keyword you can use here, $meta . 现在,在MongoDB 2.6中,您可以使用另一个关键字$ 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. 此注入的fieldname的值将来自您正在执行的文档或查询的某种“元数据”。

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. $ text查询运算符是一个运算符的示例,其中包含有关匹配文档的更多可用信息。不幸的是,它无法告诉您有关此额外信息的信息,因为这样做会以意外方式操作您的文档。 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". $ text运算符创建的元数据使用关键字“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() : 例如,在$ text search(textScore)的情况下,我们可以通过将此数组作为第二个参数传递给$collection->find()来将字段名称“score”注入到我们的文档中:

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. 现在我们在返回文档中注入一个名为“score”的字段,该文档具有来自$ text搜索的“textScore”值。

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. 但由于这仍然只是文档的元数据,如果您想在执行查询之前继续在任何后续操作中使用此值,您仍然必须将其称为$ meta数据。

This means, to sort on the field you have to sort on the $meta projection 这意味着,要对字段进行排序,您必须对$ meta投影进行排序

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM