2011-04-29 95 views
1

我使用Symfony 1.4.11。我將搜索添加到我的項目中,如Jobeet tutorial。但我還需要進行高級搜索。例如,用戶可以使用「縣」和「類別」。我製作表格並閱讀this教程。我也發現了this。但我現在不把查詢分成幾個部分。Symfony和Zend高級搜索Lucene

我班

public function updateLuceneIndex() 
{ 
    $index = $this->getTable()->getLuceneIndex(); 

    // remove an existing entry 
    if ($hit = $index->find('pk:'.$this->getAdId())) 
    { 
     $index->delete($hit->Adid); 
    } 

    // don't index expired and non-activated 
    if (!$this->getActive()) 
    { 
     return; 
    } 

    $doc = new Zend_Search_Lucene_Document(); 

    // store primary key URL to identify it in the search results 
    $doc->addField(Zend_Search_Lucene_Field::UnIndexed('pk', $this->getAdId())); 



    $doc->addField(Zend_Search_Lucene_Field::UnStored('address', $this->getAddress(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('company', $this->getCompany(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('country', $this->getCountry(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('contact_person', $this->getContactPerson(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('phone', $this->getPhone(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('email', $this->getEmail(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('title', $this->getTitle(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('content', $this->getContent(), 'utf-8')); 
    $doc->addField(Zend_Search_Lucene_Field::UnStored('category_id', $this->getCategoryId(), 'utf-8')); 


    $index->addDocument($doc); 
    $index->commit(); 

    } 

表類

public function getAdsLuceneQuery($query) 
{ 
ProjectConfiguration::registerZend(); 
$query = Zend_Search_Lucene_Search_QueryParser::parse($query); 

    $hits = self::getLuceneIndex()->find($query); 

    $pks = array(); 
    foreach ($hits as $hit) 
    { 
    $pks[] = $hit->pk; 
    } 

    if (empty($pks)) 
    { 
    return array(); 
    } 

    $q = $this->createQuery('a') 
    ->whereIn('a.AdId', $pks) 
    ->limit(20); 

    $q = $this->createQuery('a') 
      ->andWhere('a.active = ?',1) 
      ->leftJoin('a.Owner o') 
      ->leftJoin('o.Profile p') 
      ->andWhere('p.payed_until > NOW()') 
      ->andWhere('a.expires_at > NOW()') 

    ->addORDERBY ('created_at DESC'); 

    return $q->execute(); 

} 
public function getLuceneIndex() 
    { 
    ProjectConfiguration::registerZend(); 

    if (file_exists($index = $this->getLuceneIndexFile())) 
    { 
     return Zend_Search_Lucene::open($index); 
    } 
    else 
    { 
     return Zend_Search_Lucene::create($index); 
    } 
    } 

static public function getLuceneIndexFile() 
{ 
    return sfConfig::get('sf_data_dir').'/ads.'.sfConfig::get('sf_environment').'.index'; 
} 

回答

2

在你的lucene查詢你可以告訴在尋找其價值領域,看the relevant part of the documentation

它是這樣工作:

title:"The Right Way" AND text:go 
+0

嗯,我想打事端像this' $ queryCountry = '國家'; $ queryCategory ='category_id'; $ query = Zend_Search_Lucene_Search_QueryParser :: parse(「category_id:($ queryCategory)country:($ queryCountry)」);''我有查詢:'query = ads&ads.country = DE&ads_category_id = 7'But I現在不要,如何獲得例如「DE」和「7」,並將其傳遞給變量$ queryCountry和$ queryCategory – denys281 2011-05-05 15:22:58

+0

Ок,所有工作正常,我做到了:) – denys281 2011-05-06 21:12:13