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';
}
I use Symfony 1.4.11. I added search to my project, like in Jobeet tutorial. But I need to also make advanced search. For example, the user can іудусе "county" and "category". I make form and I read this tutorial. I also found this. But I do not now to separate query into several parts.
My class
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();
}
Table class
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';
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 lucene 查询中,您可以指定在哪个字段中查找值,请参阅 文档的相关部分。
它的工作原理如下:
In your lucene query you can tell which field to look in for the value, see the relevant part of the documentation.
It works like: