Zend_Search_Lucene, return results based on specified type_id

218 views Asked by At

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
    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();

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.

1

There are 1 answers

0
Richard Parnaby-King On

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.