Расширенный поиск Symfony и Zend Lucene

Я использую Symfony 1.4.11. Я добавил поиск в свой проект, как в уроке Jobeet. Но мне нужно также сделать расширенный поиск. Например, пользователь может указать «округ» и «категорию». Я создаю форму и читаю этот учебник. . Я также нашел это. Но я не хочу сейчас разделять запрос на несколько частей.

Мои занятия

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';
}

person denys281    schedule 29.04.2011    source источник


Ответы (1)


В своем запросе lucene вы можете указать, в каком поле искать значение, см. соответствующая часть документации.

Это работает так:

title:"The Right Way" AND text:go
person Maerlyn    schedule 29.04.2011
comment
Хм, я хотел бы сделать что-то вроде этого` $queryCountry = 'country'; $queryCategory = 'category_id';$query = Zend_Search_Lucene_Search_QueryParser::parse(category_id:($queryCategory) country:($queryCountry));` У меня есть запрос:query=ads&ads.country=DE&ads_category_id=7Но сейчас нет, как получить, например, DE и 7 и передать его в переменные $queryCountry и $queryCategory - person denys281; 05.05.2011