1

我想要做的是能够将返回的结果过滤为特定的 type_id。

首先,我索引所有我想要搜索的文章。

    SELECT
        article_id, article_name, article_body, type_id
    FROM
        articles
    WHERE
        active = 1;

$this->index = Zend_Search_Lucene::create($this->directory);

    foreach ($result as $key => $value)
    {
        $this->doc = new Zend_Search_Lucene_Document();
        //Indexed
        $this->doc->addField(Zend_Search_Lucene_Field::Text('article_name',$value['article_name']));
        $this->doc->addField(Zend_Search_Lucene_Field::Text('article_body', $value['article_body']));
        //Indexed

        //Unindexd
        $this->doc->addField(Zend_Search_Lucene_Field::UnIndexed('article_id', $value['article_id']));
        $this->doc->addField(Zend_Search_Lucene_Field::UnIndexed('type_id', $value['type_id']));
        //Unindexd

        $this->index->addDocument($this->doc);
    }

    $this->index->commit();
    $this->index->optimize();

现在,当我执行搜索时,如果我想通过 type_id 过滤结果,我将如何使用 Zend 的 ->find() 命令来实现呢?

$this->index = Zend_Search_Lucene::open($this->directory);

//Based on the type_id, I only want the indexed articles that match the type_id to be returned.
$results = $this->index->find('+type_id:2 '.$search_term.'*');

//Cycle through the results.

我希望 zend-search-lucene 仅根据我指定的 type_id 返回结果。

4

1 回答 1

0

您不能搜索未编入索引的术语(例如 type_id)。如果您希望该字段可搜索但不标记,您希望将其添加为关键字:

$this->doc->addField(Zend_Search_Lucene_Field::Keyword('type_id', $value['type_id']));

手册

UnIndexed 字段不可搜索,但会随搜索结果一起返回。数据库时间戳、主键、文件系统路径和其他外部标识符是 UnIndexed 字段的良好候选者。

于 2011-03-30T10:26:51.533 回答