Zend_Search_Luncene处理查询

时间:2009-08-19 17:11:29

标签: zend-framework search lucene zend-search-lucene

我试图将Searchmachine实施到我的网站中。 Iam使用Zend_Search_Lucene。

索引创建如下:

public function  create($config, $create = true)
{
    $this->_config = $config;

    // create a new index
    if ($create) {
        Zend_Search_Lucene_Analysis_Analyzer::setDefault(
            new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive()
        );

        $this->_index = Zend_Search_Lucene::create(APPLICATION_PATH . $this->_config->index->path);
    } else {
        $this->_index = Zend_Search_Lucene::open(APPLICATION_PATH . $this->_config->index->path);
    }
}

{

public function addToIndex($data)   
   $i = 0;

    foreach ($data as $val) {
        $scriptObj = new Sl_Model_Script();
        $scriptObj->title = $val['title'];
        $scriptObj->description = $val['description'];
        $scriptObj->link = $val['link'];
        $scriptObj->tutorials = $val['tutorials'];
        $scriptObj->screenshot = $val['screenshot'];
        $scriptObj->download = $val['download'];
        $scriptObj->tags = $val['tags'];
        $scriptObj->version = $val['version'];
        $this->_dao->add($scriptObj);
        $i++;
    }

    return $i;
}


 /**
     * Add to Index
     *
     * @param Sl_Interface_Model $scriptObj
     */
    public function add(Sl_Interface_Model $scriptObj)
    {

        // UTF-8 for INDEX

        $doc = new Zend_Search_Lucene_Document();
        $doc->addField(Zend_Search_Lucene_Field::text('title', $scriptObj->title, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::text('tags', $scriptObj->tags, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::text('version', $scriptObj->version, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::text('download', $scriptObj->download, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::text('link', $scriptObj->link));
        $doc->addField(Zend_Search_Lucene_Field::text('description', $scriptObj->description, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::text('tutorials', $scriptObj->tutorials, 'utf-8'));
        $doc->addField(Zend_Search_Lucene_Field::text('screenshot', $scriptObj->screenshot));
        $this->_index->addDocument($doc);

    }

但是当我尝试用:

查询索引时

$ index-> find('Wordpress 2.8.1'。'*');

我收到以下错误:

“模式开头需要非通配符。”

如何查询像我这样的字符串?对“wordpress”的查询就像例外情况一样。

1 个答案:

答案 0 :(得分:1)

Lucene无法处理领先的通配符,只能处理尾随通配符。也就是说,它不支持像'告诉我每个名字以'att'结尾的人的查询,这类似

first_name:* att

它仅支持尾随通配符。告诉我名字结束的所有人都以'ma'开头

first_name:ma *

请参阅此Lucene FAQ条目:

http://wiki.apache.org/lucene-java/LuceneFAQ#head-4d62118417eaef0dcb87f4370583f809848ea695

Lucene 2.1有一个解决方法,但开发人员说它可能“昂贵”。