我有类似于以下代码的内容:
[...]
while($result=mysql_fetch_assoc($query)){
//Build a doc
$doc = new Zend_Search_Lucene_Document();
[...]
$doc->addField(Zend_Search_Lucene_Field::text('category', sanitize($result["category"])));
$all_values=explode(',',$result["value"]);
$i=0;
foreach ($all_values as $value) {
$doc->addField(Zend_Search_Lucene_Field::text('value_'.($i++), urlencode($value)));
}
[...]
//Add to doc
$index->addDocument($doc);
}
[...]
现在我想只显示搜索后分数较高的字段value_...
。
我怎么能这样做?
答案 0 :(得分:0)
在考虑了一下之后,我已经解决了我的问题。
Zend Lucene按分数命令结果,因此,我需要做的是为$value
循环的每个foreach
创建一个doc,然后为我当前结果的ID创建一个索引(称为res_id
)在foreach内部。这样,我可以在搜索后以编程方式仅显示同一res_id
的一个结果。
所以我的代码变成了:
[...]
while($result=mysql_fetch_assoc($query)){
$all_values=explode(',',$result["value"]);
//Create a Doc foreach value of $all_values and gave them the same category of the result of the query
foreach ($all_values as $value) {
//Build a doc
$doc = new Zend_Search_Lucene_Document();
[...]
//Added res_id just to help grouping the results
$doc->addField(Zend_Search_Lucene_Field::unIndexed('res_id', sanitize($result["id"])));
$doc->addField(Zend_Search_Lucene_Field::text('category', sanitize($result["category"])));
//Renamed the field to value
$doc->addField(Zend_Search_Lucene_Field::text('value', urlencode($value)));
[...]
//Add to doc
$index->addDocument($doc);
}
}
[...]
现在,当我进行搜索时,我只显示第一次出现的res_id
(得分更高的那个)。
以下是我在Controller上的代码,该代码向视图发送包含项目和总项目的数组:
[...]
public function indexAction()
{
$this->view->query=urldecode($this->_getParam("query"));
if ($this->view->query){
//open the index
$index = new Zend_Search_Lucene('path/to/lucene/index');
$hits = $index->find($this->view->query);
$executed_ids=array();
$total=0;
$this->view->items=array();
foreach ($hits as $hit) {
//Pass item to view only if it is not listed yet on $executed_ids
if (!$executed_ids[$hit->res_id]){
$this->view->items[]=$hit;
$executed_ids[$hit->res_id]=true;
$total++;
}
}
$this->view->total_hits=$total;
}
}
[...]