开发者

Zend_Search_Lucene, return results based on specified type_id

What I want to do is be able to filter the returned results to a specific type_id.

First I index all the articles I want to be searchable.

    SELECT
        article_id, article_name, article_body, type_id
    FROM
        articles
    WHE开发者_如何转开发RE
        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();

Now when I perform a search, if I want to filter the results by the type_id, how would I do it using the ->find() command of Zend?

$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.

I want zend-search-lucene to only returns results based on the type_id that I specify.


You cannot search for unindexed terms (such as type_id). If you want the field to be searchable, but not tokenised, you want to add it as keyword:

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

From the manual:

UnIndexed fields are not searchable, but they are returned with search hits. Database timestamps, primary keys, file system paths, and other external identifiers are good candidates for UnIndexed fields.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜