私はこれは私のmongoクエリがどのように見えるかであるMongoDBの3.2 に集約パイプラインを使用してテキスト検索を実装しようとしています:これは私のコレクションに細かい実行されているテキスト検索 - 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 } }
]
)
を。結果の文書構造は次のようになります。
{
"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
}
私はPHPでのユーザの入力に基づいて、類似したテキスト検索クエリを実行しようとします。私は "$プロジェクト仕様はオブジェクトでなければならない"というエラーを受け取りました。
<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>';
}
?>
このPHPの出力は次のとおりです:私は意図的にPHPベースのmongoクエリを表示するのvar_dumpを入れている
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
は、ここで私のPHPコードです。 誰でもここで間違いを指摘できますか? おかげ
から1つの余分のアレイを削除しました。ありがとうございました。 –