问题描述
|
我有这个脚本:
$removeList = array(\'+\',\'-\',\'&&\',\'||\',\'!\',\'(\',\')\',\'{\',\'}\',\'[\',\']\',\'^\',\'\"\',\'~\',\'*\',\'?\',\':\',\'\\\\\');
$keyword = str_replace($removeList,\'\',mb_strtolower($_GET[\'query\'],\'UTF-8\'));
$keyword_str = strlen($keyword);
if($keyword_str>=3){
$options = array(\'hostname\' => \'**.*.*.*\',\'login\' => \'\',\'password\' => \'\',\'port\' => 8080);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setTerms(true);
$query->setTermsLimit(10);
$query->setTermsSort(1);
$query->setTermsField(\'keywords\')->setTermsPrefix($keyword);
$updateResponse = $client->query($query);
}
这个脚本工作正常。但是如何使它按类别搜索?
示例:我的变量是$_GET[\'query\'] = \'au\'
,但我只想在cat = 2中进行搜索。现在,它搜索整个数据库。我该怎么做?谢谢。
解决方法
您需要使用TermsComponent吗?如果没有,我的建议是通过使用ѭ2build来构建您的查询,并借助Solr的查询语法在cat字段上设置一个静态过滤器。
例:
<?php
/* ... */
$query = new SolrQuery();
$query->setQuery(\'(cat:2) AND (keywords:\' . $keyword . \')\');
/* ... */
?>
...或使用SolrQuery
构造函数的简写形式:
<?php
/* ... */
$query = new SolrQuery(\'(cat:2) AND (keywords:\' . $keyword . \')\');
/* ... */
?>