solr faceting 搜索结果统计 php客户端

关于solr的faceting 有很多中处理方案;

比较常用有

faceting text
faceting date
faceting query


模拟一个简单的需求:

企业索引库中
  • 字段comCate 公司类型
  • 字段comSize 公司规模
  • 字段date    入库时间
  • 字段info    公司基本信息
  • 字段comName 公司名称


业务:输入关键字 (在公司名称字段 + 公司基本信息)中检索、显示出相应的查询结果
并列出  结果中各种类型的公司个数  各种规模的公司个数 
近一年来的入库公司个数 近两年来入库公司的个数 近5年来入库的个数

以上的业务处理完全可以使用faceting来实现;






关于solr搜索结果统计的具体实现【小例子】

【http://localhost:8044/solr/job/select/?q=*%3a*&version=2.2&start=0&rows=10&indent=on&facet=on&facet.field=comCate&f.comCate.facet.missing=on&f.comCate.facet.method=enum】

上面的url为solr的搜索查询命令

相应片段
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">3</int>
−
<lst name="params">
[color=red]<str name="facet">on</str>[/color]
<str name="indent">on</str>
<str name="start">0</str>
<str name="q">*:*</str>
[color=red]<str name="f.comCate.facet.method">enum</str>
<str name="facet.field">comCate</str>
<str name="f.comCate.facet.missing">on</str>[/color]
<str name="rows">10</str>
<str name="version">2.2</str>
</lst>



</lst>



    <str name="facet">on</str>
    打开facet(统计)
    <str name="facet.field">comCate</str>
    统计字段为 comCate
    <str name="f.comCate.facet.missing">on</str>
    貌似不处理空内容
    <str name="f.comCate.facet.method">enum</str>
    统计方法枚举类型统计

相应片段
<lst name="facet_counts">
<lst name="facet_queries"/>
−
<lst name="facet_fields">
−
<lst name="comCate">
<int name="民营公司">1721</int>
<int name="外资(欧美)">367</int>
<int name="外资(非欧美)">333</int>
<int name="合资(非欧美)">169</int>
<int name="合资(欧美)">141</int>
<int name="国企">130</int>
<int name="外企代表处">15</int>
<int>575</int>
</lst>
</lst>
<lst name="facet_dates"/>
</lst>


该应用为检索企业库 统计各个企业类型下有多少企业 类似sql中的 group by
\ count(*)

《solr1.4 enterprise search server》书中 第五章 141页 明细介绍!
---------------------

如果你使用php客户端来处理这个应用的话 在多统计的时候可能会出一些小罗乱:


$solr = new Apache_Solr_service('localhost', '8044', '/solr/ent');
$condition['q.op'] = 'OR';
$condition['sort']='date desc';
$condition['facet'] = 'true';
$condition['facet.mincount'] = '1';
[color=red]$condition['facet.field'] = 'comCate';
$condition['facet.field'] = 'comSize';[/color]
$condition['f.conCate.facet.missing'] = 'true';
$condition['f.conCate.facet.method'] = 'enum';
$condition['f.comSize.facet.missing'] = 'true';
$condition['f.comSize.facet.method'] = 'enum';
$q_str = "*:*";
r = $solr->search($q_str, $offset, 10, $condition);

上面的代码显然是数组的索引重复 、
【解决方案:】

[color=red]$condition['facet.field'] = 'comCate';
$condition['facet.field'] = 'comSize';[/color]
//修改成
[color=red]$condition['facet.field_1'] = 'comCate';
$condition['facet.field_2'] = 'comSize';[/color]
//继续修改 Apache/Solr/service.php文件 960行左右 
// anywhere else the regex isn't expecting it
$queryString = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $queryString);
//在此添加下面代码即可
$queryString = preg_replace('/field_[0-9]{1}/', 'field', $queryString);

猜你喜欢

转载自ynial.iteye.com/blog/598811