我是Solr的新手,我对实现一个特殊的方面很感兴趣.
样本文件:
{ hostname: google.com, time_spent: 100 }
{ hostname: facebook.com, time_spent: 10 }
{ hostname: google.com, time_spent: 30 }
{ hostname: reddit.com, time_spent: 20 }
...
Run Code Online (Sandbox Code Playgroud)
我想返回一个具有以下结构的方面:
{ google.com: 130, reddit.com: 20, facebook.com: 10 }
Run Code Online (Sandbox Code Playgroud)
尽管solr返回值比这更冗长,但重要的一点是facet的"计数"是文档的time_spent值的总和,而不是与facet匹配的文档的实际计数.
我可以使用一个支点:
q:*:*
&facet=true
&facet.pivot=hostname,time_spent
Run Code Online (Sandbox Code Playgroud)
但是,这将返回每个唯一主机名的所有唯一时间值的计数.我可以手动在我的应用程序中总结这一点,但这似乎很浪费.
我可以使用统计模块:
q:*:*
&stats=true
&stats.field=time_spent
&stats.facet=hostname
Run Code Online (Sandbox Code Playgroud)
但是,这有两个问题.首先,返回的结果包含所有主机名.这真的有问题,因为我的数据集有超过1米的主机名.此外,返回的结果未排序 - 我需要按照花费的总时间减少的顺序呈现主机名.
对此我的帮助将非常感谢!
谢谢!
使用Solr> = 5.1,这是可能的:
分面排序
字段或术语构面的默认排序是按桶数递减.我们可以选择按每个存储桶中出现的任何构面函数对升序或降序进行排序.例如,如果我们想要按平均价格找到顶部桶,那么我们会在上一个facet请求中添加sort:"x desc":
Run Code Online (Sandbox Code Playgroud)$ curl http://localhost:8983/solr/query -d 'q=*:*& json.facet={ categories:{ type : terms, field : cat, sort : "x desc", // can also use sort:{x:desc} facet:{ x : "avg(price)", y : "sum(price)" } } } '
见Yonik的博客:http://yonik.com/solr-facet-functions/
对于您的用例,这将是:
json.facet={
hostname_time:{
type: terms,
field: hostname,
sort: "time_total desc",
facet:{
time_total: "sum(time_spent)",
}
}
}
Run Code Online (Sandbox Code Playgroud)
调用sum()嵌套方面仅在6.3.0中为我们工作.
小智 0
我相信您正在寻找的是聚合组件,但请注意 solr 是全文搜索引擎而不是数据库。
所以,你的问题的答案是,遵循想法#1。否则,您应该使用配备此类聚合组件的 Elastics Search 或 MongoDB 甚至 Redis。