Php 如何在Zend Lucene上获得场地得分
我有类似于以下代码的代码:Php 如何在Zend Lucene上获得场地得分,php,zend-framework,search,lucene,zend-search-lucene,Php,Zend Framework,Search,Lucene,Zend Search Lucene,我有类似于以下代码的代码: [...] 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_value
[...]
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.
我该怎么做呢?在仔细考虑之后,我找到了解决问题的办法 Zend Lucene根据分数对结果进行排序,因此,我需要做的是为
foreach
循环的每个$value
创建一个文档,然后为我当前结果的ID(称为resu ID
)创建一个索引,该ID位于foreach内部。这样,我可以通过编程在搜索后只显示相同res\u 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\u id
(得分更高的一个)
以下是我控制器上的代码,该代码向视图发送一个包含找到的项目和总项目的数组:
[...]
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;
}
}
[...]