Search 按集合字段分组solr搜索结果的解决方法
我是Solr的初学者。可能会有错误。很抱歉 我使用的是7.7.1版。 假设有下一个文档:Search 按集合字段分组solr搜索结果的解决方法,search,solr,lucene,Search,Solr,Lucene,我是Solr的初学者。可能会有错误。很抱歉 我使用的是7.7.1版。 假设有下一个文档: { "documents": [ { "id": 1, "category": [ "a", "b" ], "score":0.10 //lucene score }, { "id": 2, "category": [ "b", "c",
{
"documents": [
{
"id": 1,
"category": [
"a",
"b"
],
"score":0.10 //lucene score
},
{
"id": 2,
"category": [
"b",
"c",
"d",
"e"
],
"score":0.20 //lucene score
},
{
"id": 3,
"category": [
"a",
"e"
],
"score":0.30 //lucene score
},
{
"id": 4,
"category": [
"d",
"e"
],
"score":0.40 //lucene score
},
{
"id": 5,
"category": [
"a",
"c"
],
"score":0.50 //lucene score
}
]
}
主要任务是下一步。我得到3个或更多不同的类别,我只需要一个文档,每个类别的得分最高。换句话说,我需要按类别字段对结果进行分组,每个组都必须按分数desc排序,每个组必须限制为1
例如,对于got a、b、c类别,结果必须包含3个文档
document with id == 5 for a category
document with id == 2 for b category
document with id == 5 for c category
是否可以创建solr查询以通过单个请求获得此类结果
我尝试了下一种方法,但没有起到任何作用:
感谢和问候json。facet在这里会有所帮助。您可以使用下面的查询,它将根据您的需要为您提供响应。此查询将首先为按升序排序的类别字段创建bucket,然后使用通过“qq”参数传递的查询的lucene得分排序的ID嵌套bucket 我使用了一个函数查询,我们使用它来评估给定查询的lucene分数。现在,这是一个简单的查询,但您也可以创建一个复杂的查询并传入qq参数。阅读此处:-获取有关函数查询的更多信息
q=*&qq=category:a&json.facet={
categories:{
type:terms,
field:category,
sort:{index:asc},
facet:{
id:{
type:terms,
field:id,
sort:"query_score desc",
facet:{
query_score:"min(if(exists(query($qq)),query($qq),0))"
}
}
}
}
}
[对上述查询的响应]
{
"responseHeader":{
"status":0,
"QTime":7,
"params":{
"qq":"category:a",
"q":"*",
"json.facet":"{ categories:{ type:terms, field:category, sort:{index:asc}, facet:{ id:{ type:terms, field:id, sort:\"query_score desc\", facet:{ query_score:\"min(if(exists(query($qq)),query($qq),0))\" } } } } }",
"indent":"on",
"fl":"*,query($qq,-1)",
"rows":"0",
"wt":"json"}},
"response":{"numFound":5,"start":0,"docs":[]
},
"facets":{
"count":5,
"categories":{
"buckets":[{
"val":"a",
"count":3,
"id":{
"buckets":[{
"val":"1",
"count":1,
"query_score":0.5389965176582336},
{
"val":"3",
"count":1,
"query_score":0.5389965176582336},
{
"val":"5",
"count":1,
"query_score":0.5389965176582336}]}},
{
"val":"b",
"count":2,
"id":{
"buckets":[{
"val":"1",
"count":1,
"query_score":0.5389965176582336},
{
"val":"2",
"count":1,
"query_score":0.0}]}},
{
"val":"c",
"count":2,
"id":{
"buckets":[{
"val":"5",
"count":1,
"query_score":0.5389965176582336},
{
"val":"2",
"count":1,
"query_score":0.0}]}},
{
"val":"d",
"count":2,
"id":{
"buckets":[{
"val":"2",
"count":1,
"query_score":0.0},
{
"val":"4",
"count":1,
"query_score":0.0}]}},
{
"val":"e",
"count":3,
"id":{
"buckets":[{
"val":"3",
"count":1,
"query_score":0.5389965176582336},
{
"val":"2",
"count":1,
"query_score":0.0},
{
"val":"4",
"count":1,
"query_score":0.0}]}}]}}}
有关json.facet的更多信息:-感谢您的回答。看起来它从结果中提取了所有可能的类别,并为每个类别创建了一个bucket。这与我的意思略有不同,因为需要对类别进行额外检查,但这比提出大量请求要好。再次感谢。您的示例中还有另一个问题。是否可以按文档的“分数”对嵌套面进行排序?我已经阅读了您在邮件中提供的文档。看起来不可能使用文档的“分数”对桶进行排序。不幸的是,这是我需要执行Solr请求而不是数据库请求的主要原因。你能告诉我是否能修好吗?谢谢当你说“分数”时,你是指lucene分数还是为每个文档创建的自定义字段?。另外,在json方面,我已经在使用自定义字段“score”进行排序了。如果混淆了,很抱歉。我的示例中也是指lucene分数。在我的生产solr中没有自定义“分数”。当我把分数插入。。。分数:“最小(分数)…”我对消息“未定义字段:\“分数\”有异常