2014-02-09 76 views
0

我試圖運行這個簡單的例子來試試例子(MongoDB的網站找到)如何在蒙戈2.4沒有收集文本索引的MongoDB

<?php 
    $m = new Mongo(); 
    $d = $m->demo; 
    $c = $d->planets; 

    $c->insert(array("name" => "Mercury", "desc" => "Mercury is the smallest and closest to the Sun")); 
    $c->insert(array("name" => "Venus", "desc" => "Venus is the second planet from the Sun, orbiting it every 224.7 Earth days.")); 
    $c->insert(array("name" => "Earth", "desc" => "Earth is the the densest of the eight planets in the Solar System.")); 
    $c->insert(array("name" => "Mars", "desc" => "Mars is named after the Roman god of war.")); 


    $c->ensureIndex(array('desc' => 'text')); 

    $r = $d->command(array("text" => "planets", 'search' => "sun")); 
    print_r($r); 

    ?> 

使用TEXTSEARCH feauture但我得到這個錯誤:Array ([ok] => 0 [errmsg] => no text index for: demo.planets) 我不明白爲什麼,因爲我已經在這行定義的文本索引:

$c->ensureIndex(array('desc' => 'text')); 

和TEXTSEARCH啓用。

謝謝你的幫助!

回答

1

我找到了解決辦法,顯然我們需要的字段(<>正文指數)指數之前定義:

$c->ensureIndex(array('name'=> 1,'desc' => 1),array('unique'=> true)); 
1

你需要創建文本索引或使用文本命令之前明確地啓用該功能。

您可以啓用在啓動時與textSearchEnabled參數文本搜索功能:

的mongod --setParameter textSearchEnabled =真

+0

作爲MongoDB中的2.6.x的,文本搜索默認情況下啓用。在MongoDB 2.4.x上,你需要啓用它,如上所示 – bjori