Search code examples
phpsolrsolr-query-syntax

How to add a boost query using SolrQuery


I am using SolrClient to connect with the database and following the example from https://www.php.net/manual/en/book.solr.php

I am trying to generate the query which I created through the dashboard of solr

URL/shop/select?q=*&sort=brand_logo_value+desc,created+desc&start=0&rows=12&facet=false&indent=true&fq=!final_price:0&fq=!img_flag:0&omitHeader=true&fq=(v_index_flag:1+OR+v_index_flag:4+OR+v_index_flag:99)&fl=id,store_for,store_id,user_id,product_title,final_price,store_from,seller,product1,website,product_id,company_name,category_name,sub_category_name,discount_percentage,list_price,affiliate_name,v_index_flag,parent_id,category_name_small,sub_category_name_small,type_small&group=true&group.field=parent_id&group.sort=final_price+asc&bq=uniq_text:(%22refurbish%22^-1000)&bq=brand_logo_value:(%221%22^5)&fq=category_name_small:%22electronics+and+appliances%22&fq=sub_category_name_small:%22air+conditioners%22&json.facet={categories:{type:terms,field:cat_subcat_type_small,sort:{parent_unique:desc},facet:{parent_unique:%22unique(parent_id)%22}}}&wt=json

What I am doing is passing the entire URL to a php page and exploding into individual part and adding creating the query

$query = new SolrQuery();
        foreach ($sections as $field) {
            $cut = explode("=", $field);
            echo "<br>" . $cut[0] . "===>" . $cut[1];
            switch ($cut[0]) {

            case 'fq':
                $query->setFacet(true);
                $query->addFilterQuery(str_replace("+", " ", $cut[1]));
                break;
            case 'sort':

                $cut2 = explode(",", $cut[1]);
                $SortOrder['asc'] = SolrQuery::ORDER_ASC;
                $SortOrder['desc'] = SolrQuery::ORDER_DESC;
                foreach ($cut2 as $slice) {
                    $bite = explode("+", $slice);
                    $query->addSortField($bite[0], $SortOrder[$bite[1]]);
                }
                break;

            case 'bq':
                $cut2 = explode(":", $cut[1]);
                $dismaxQuery = new SolrDisMaxQuery();
                $dismaxQuery->addBoostQuery($cut2[0], $cut2[1]);

                break;

            case 'group.sort':

                $cut2 = explode(",", $cut[1]);
                $SortOrder['asc'] = SolrQuery::ORDER_ASC;
                $SortOrder['desc'] = SolrQuery::ORDER_DESC;
                foreach ($cut2 as $slice) {
                    $bite = explode("+", $slice);
                    $query->addGroupSortField($bite[0], $SortOrder[$bite[1]]);
                }
                break;

            default:

                $query->setParam($cut[0], $cut[1]);
            }

        }

The code is not yet optimized YET. All are working fine but the BoostQuery(bq). I don't see any function in SolrQuery class to add this but have seen one in SolrDisMaxQuery https://php.net/manual/en/solrdismaxquery.addboostquery.php

But this is not adding the bq to the current SolrQuery object obviously.

So I have two question

  1. Is there a method to add a boostquery with SolrQuery object
  2. Is there a method in which I can directly execute the URL passed and get the result.

EDIT1:

If anyone find ways to optimize the code or recommendation for logic change, It will really hep me.

EDIT2:

Is there a method where I can execute a query directly using PHP?


Solution

  • Just creating an object and setting a value on it - and then doing nothing more with it won't do anything useful. So this part doesn't affect anything:

    $dismaxQuery = new SolrDisMaxQuery();
    $dismaxQuery->addBoostQuery($cut2[0], $cut2[1]);
    

    Instead, create the query as a SolrDisMaxQuery instead of as a SolrQuery:

    $query = new SolrDisMaxQuery();
    
    foreach ($sections as $field) {
       ...
    }
    

    That way you can set the parameters that are specific to the DisMax query parser.

    SolrDisMaxQuery extends SolrQuery, so it can be used in the same way as your previous class.