2016-04-28 11 views
1

Ich versuche Textsuche Aggregation Pipelines in MongoDB 3.2 Dies ist, wie meine Mongo Abfrage aussieht zu implementieren:Textsuche mit Aggregation Pipeline - MongoDB/PHP

db.revws.aggregate(
    [ 
    { $match: { $text: { $search: "terrible" } } }, 
    { $sort: { score: { $meta: "textScore" } } }, 
    { $project: {score: { $meta: "textScore" },'ProductInfo.ProductID' :1 , 'Reviews.Title': 1, 'Reviews.Content': 1, 'Reviews.Overall': 1,'Reviews.Author': 1, _id: 0 } } 
    ] 
) 

fein Dies läuft auf meiner Sammlung. Die sich ergebende Dokumentstruktur sieht wie folgt aus:

{ 
    "Reviews" : { 
     "Title" : "Terrible terrible terrible", 
     "Author" : "C. Leinart", 
     "Overall" : "1.0", 
     "Content" : "I love cameras - i have multiple cameras for multiple uses and this is the absolute worst ever. The quality of the prints are terrible. The paper turns sort of a bluish color. The battery life is terrible and to top it all off, i lost my charger and there is no such thing as a replacement charger in Polaroid-land. Really...you CANNOT buy a replacement charger. So if you have one of these, hang on to the charger and be prepared to only use for fun party pics - it's novel but not worth the $$." 
    }, 
    "ProductInfo" : { 
     "ProductID" : "B005O08KH6" 
    }, 
    "score" : 2.53571428571429 
} 

Wenn ich versuche, ähnliche Abfrage Textsuche basiert auf Benutzer in PHP Eingabe auszuführen. Ich bekomme einen Fehler "$ Projektspezifikation muss ein Objekt sein".

Hier ist mein PHP-Code:

<html> 
<body> 
<h3>MongoDB Test - Aggregation pipeline</h3> 
<h2>Text search - Amazon reviews</h2> 
<br> 
<form method="post"> 
search: <input type="text" name="term"><br><br> 
<input type="submit" value="Display results"> 
</form> 
</body> 
</html> 

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    $term = $_REQUEST['term']; 
    // connect to mongodb : default is localhost:27017 
    $m = new MongoClient(); 
    echo "Connection to database successfully"."</br>"; 
    $db = $m->selectDB('test'); 
    $collection = new MongoCollection($db, 'revws'); 
/* 
db.revws.aggregate(
    [ 
    { $match: { $text: { $search: "terrible" } } }, 
    { $sort: { score: { $meta: "textScore" } } }, 
    { $project: {score: { $meta: "textScore" },'ProductInfo.ProductID' :1 , 'Reviews.Title': 1, 'Reviews.Content': 1, 'Reviews.Overall': 1,'Reviews.Author': 1, _id: 0 } } 
    ] 
) 
* 
*/ 
$pipeline = array (
    array(
     '$match' => array('$text' => array('$search'=> $term)) 
    ), 
    array('$sort' => array("score" => array('$meta' => "textScore")) 
    ), 
    array('$project' => array(array("score" => array('$meta' => "textScore"), 'ProductInfo.ProductID' => 1, 
     'Reviews.Title' => 1, 'Reviews.Content' => 1 , 'Reviews.Overall' => 1, 'Reviews.Author' => 1, '_id' => 0 
       ) 
    ) 
    ) 
); 
var_dump($pipeline); 
$cursor = $collection->aggregate($pipeline); 
echo "<br><br>"; 
echo '<table border="1"><tr><td>text score</td><td>ProductID</td><td>Review Title</td><td>Content</td><td>Author</td><td>Rating</td></tr>'; 

foreach ($cursor as $doc) { 

    echo '<tr>'; 
     echo 
      '<td>'.$doc['score'].'</td>'. 
      '<td>'.$doc['ProductInfo']['ProductID'].'</td>'. 
      '<td>'.$doc['Reviews']['Title'].'</td>'. 
      '<td>'.$doc['Reviews']['Content'].'</td>'. 
      '<td>'.$doc['Reviews']['Author'].'</td>'. 
      '<td>'.$doc['Reviews']['Overall'].'</td>'; 
    echo '</tr>'; 
} 
echo '</table>'; 
} 
?> 

Der Ausgang dieses PHP ist:

Connection to database successfully array(3) { [0]=> array(1) { ["$match"]=> array(1) { ["$text"]=> array(1) { ["$search"]=> string(8) "terrible" } } } [1]=> array(1) { ["$sort"]=> array(1) { ["score"]=> array(1) { ["$meta"]=> string(9) "textScore" } } } [2]=> array(1) { ["$project"]=> array(1) { [0]=> array(7) { ["score"]=> array(1) { ["$meta"]=> string(9) "textScore" } ["ProductInfo.ProductID"]=> int(1) ["Reviews.Title"]=> int(1) ["Reviews.Content"]=> int(1) ["Reviews.Overall"]=> int(1) ["Reviews.Author"]=> int(1) ["_id"]=> int(0) } } } } Fatal error: Uncaught exception 'MongoResultException' with message 'localhost:27017: $project specification must be an object' in C:\xampp\htdocs\MongoSearch\MongoSearchAggr.php:49 Stack trace: #0 C:\xampp\htdocs\MongoSearch\MongoSearchAggr.php(49): MongoCollection->aggregate(Array) #1 {main} thrown in C:\xampp\htdocs\MongoSearch\MongoSearchAggr.php on line 49

ich absichtlich Var_dump gesetzt haben PHP-basierte Mongo Abfrage angezeigt werden soll. kann jemand hier auf den Fehler hinweisen? dank

Antwort

1

Haben Sie

versucht
$pipeline = array (
    array(
     '$match' => array('$text' => array('$search'=> $term)) 
    ), 
    array('$sort' => array("score" => array('$meta' => "textScore")) 
    ), 
    array('$project' => array("score" => array('$meta' => "textScore"), 'ProductInfo.ProductID' => 1, 
     'Reviews.Title' => 1, 'Reviews.Content' => 1 , 'Reviews.Overall' => 1, 'Reviews.Author' => 1, '_id' => 0 

    ) 
    ) 
); 

Ich habe gerade ein zusätzliches Array von $ Projekt entfernt

+0

, das funktioniert. Vielen Dank. –