2

我的一个项目使用 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;
} 
4

1 回答 1

0

When you say when i use "sort by score" in search function it's not working I assume you are telling that the results are not sorted by score.

This is because your main query is *:* and you are adding your search terms via a filter query, which won't influence the score. See https://wiki.apache.org/solr/CommonQueryParameters#fq where it says

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.

So if you make the search term filter query as your main query, then you should see results sorted by score.

Update: Instead of

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

use

$solrQuery = new SolrQuery();

and instead of

$solrQuery->addFilterQuery($filterQuery);

use

$solrQuery->setQuery($filterQuery);

于 2013-02-20T15:53:01.927 回答