我正在使用SolrClient连接数据库,并遵循https://www.php.net/manual/en/book.solr.php
中的示例我正在尝试生成通过solr仪表板创建的查询
URL /商店/选择?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,最终价格,store_from,卖方,product1,网站,product_id,company_name,category_name,sub_category_name,折扣百分比,list_price,关联公司名称,v_index_flag ,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 + %22&fq = sub_category_name_small:%22air + conditioners%22&json.facet = {类别:{type:terms,field:cat_subcat_type_small,sort:{parent_unique:desc},facet:{parent_unique:%22unique(parent_id)%22}}}&wt = json
我正在做的是将整个URL传递到php页面,然后分解为单个部分并添加创建查询的内容
$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]);
}
}
代码尚未进行优化。一切正常,但BoostQuery(bq)。我没有在SolrQuery类中看到任何要添加此函数的函数,但是在SolrDisMaxQuery https://php.net/manual/en/solrdismaxquery.addboostquery.php
中看到了一个函数但这显然不会将bq添加到当前的SolrQuery对象中。
所以我有两个问题
如果有人找到优化代码或逻辑更改建议的方法,它将真的帮助我。
有没有一种方法可以直接使用PHP执行查询?
答案 0 :(得分:1)
仅创建一个对象并为其设置值-然后对其执行任何其他操作将无济于事。因此,这部分不会影响任何内容:
$dismaxQuery = new SolrDisMaxQuery();
$dismaxQuery->addBoostQuery($cut2[0], $cut2[1]);
相反,将查询创建为SolrDisMaxQuery
而不是SolrQuery
:
$query = new SolrDisMaxQuery();
foreach ($sections as $field) {
...
}
这样,您可以设置特定于DisMax查询解析器的参数。
SolrDisMaxQuery
扩展了SolrQuery
,因此可以与上一堂课一样使用。