简体   繁体   中英

Solr Search sort by score in solr1.2

One of my project use solr1.2 and when i use "sort by score" in search function it's not working.I don't know why?

Can any one explain this.I am totally confuse.

my controller where i do :

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;
} 

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.

Instead of 代替

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

use

$solrQuery = new SolrQuery();

and instead of

$solrQuery->addFilterQuery($filterQuery);

use

$solrQuery->setQuery($filterQuery);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM