2013-02-20 75 views
2

我的一个项目使用solr1.2,当我在搜索功能中使用“按分数排序”时,它不工作。我不知道为什么?Solr搜索通过solr1.2得分排序

任何人都可以解释这一点。我完全混淆了。

我的控制器在那里我做:

protected function globalSearch($searchTerm, $productFilter = array()) 
{ 
    $solrService = $this->get('syd.solr_service'); 

    $solrQuery = new SolrQuery('*:*'); 
    $solrQuery->addField('id') 
     ->addField('first_product_slug') 
     ->addField('first_product_name') 
     ->addField('name') 
     ->addField('slug') 
     ->addField('thumbnail_path') 
     ->addField('product_slug') 
     ->addField('design_category_id') 
     ->addSortField('score', SolrQuery::ORDER_DESC); 
    $solrQuery->set("group", "true"); 
    $solrQuery->set("group.field", "first_product_id"); 
    $solrQuery->set("group.limit", 4); 

    if($searchTerm){ 
     $filterQueries = array(); 
     $searchTerms = explode(' ',$searchTerm); 
     $searchTerms[] = $searchTerm; 
     $searchTerm = '("' . implode('" OR "', $searchTerms) . '")'; 
     $filterQuery = sprintf(self::SEARCH_STRING, $searchTerm); 
     $solrQuery->addFilterQuery($filterQuery); 
    } 

    if (!empty($productFilter)) 
    { 
     $productFiltersArr = array(); 
     $productFilterQry = ''; 
     foreach ($productFilter as $productFilterValue) 
     { 
      $productFiltersArr[] = 'first_product_slug:' . $productFilterValue; 
     } 
     $productFilterQry = implode(' OR ', $productFiltersArr); 
     $solrQuery->addFilterQuery($productFilterQry); 
    } 

    $solrQuery->setRows(1000); 
    try { 
     $solrObject = $solrService->query(
      'SydPrintBundle:DesignTemplate', 
      $solrQuery, 
      SolrService::WRITER_FORMAT_SOLR_OBJECT 
     ); 
     $templates = $solrObject->offsetGet('grouped')->offsetGet('first_product_id')->offsetGet('groups'); 
    } 
    catch (\Exception $e) { 
     $templates = array(); 
    } 

    if (!$templates) { 

     if (!empty($searchTerm)) { 
      $this->setFlash('catalog-message', 'No results found for your search.'); 
     } 

     return array(); 
    } 
    if (!$searchTerm) { 

     if (!empty($searchTerm)) { 
      $this->setFlash('catalog-message', 'No results found for your search.'); 
     } 

     return array(); 
    } 

    return $templates; 
} 

回答

0

当你说when i use "sort by score" in search function it's not working我以为你告诉了结果不受得分排序。

这是因为您的主要查询是*:*,并且您通过过滤器查询添加搜索词,这不会影响分数。见https://wiki.apache.org/solr/CommonQueryParameters#fq在那里说

This parameter can be used to specify a query that can be used to restrict the super set of documents that can be returned, without influencing score.

所以,如果你做的搜索词过滤查询作为主查询,那么你应该看到得分排序结果。

更新: 而不是

$solrQuery = new SolrQuery('*:*');

使用

$solrQuery = new SolrQuery();

,取而代之的

$solrQuery->addFilterQuery($filterQuery);

使用

$solrQuery->setQuery($filterQuery);

+0

你能解释我如何让搜索词过滤查询作为我的主查询? – Sid 2013-02-21 06:03:16

+0

请参阅我的更新 – arun 2013-02-21 06:12:03

+0

谢谢............... – Sid 2013-02-22 09:00:14