opHasNoName
opHasNoName

Reputation: 20736

Zend_Search_Luncene handle Querys

iam trying to implement an Searchmachine into my site. Iam using Zend_Search_Lucene for this.

The index is created like this :

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

    }

But when i try to query the index with :

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

im getting the following error :

"non-wildcard characters are required at the beginning of pattern."

any ideas how to query for a string like mine ? an query for "wordpress" works like excepted.

Upvotes: 0

Views: 162

Answers (1)

Cody Caughlan
Cody Caughlan

Reputation: 32748

Lucene cannot handle leading wildcards, only trailing ones. That is, it does not support queries like 'tell me everyone whose name ends with 'att'' which would be something like

first_name: *att

It only supports trailing wildcards. Tell me everyone whose names end that start with 'ma'

first_name: ma*

See this Lucene FAQ entry:

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

There IS a workaround for Lucene 2.1 but the developers say it can be "expensive".

Upvotes: 1

Related Questions