身安不如心安,屋宽不如心宽 。

父子关系查询如何根据子元素的个数来对父元素进行排序

Elasticsearch | 作者 yixiongmao | 发布于2022年04月08日 | 阅读数:1296

Es版本5.6.3
具体父子结构如下:
机构信息表(父)、机构关键词表(子)、机构专利表(子)
PUT /unit_idx
{
"mappings" : {
"unit_info": {
"dynamic": false,
"properties": {
"unit_id":{
"type":"keyword"
},
"unit_name": {
"type": "text"
}
}
},
"unit_keyword": {
"_parent": {
"type": "unit_info"
},
"dynamic": false,
"properties": {
"unit_keyword_busi_id":{
"type":"keyword"
},
"unit_keyword_text":{
"type":"text",
"fields":{
"cn":{
"type":"text",
"analyzer": "ik_smart"
},
"en":{
"type":"text",
"analyzer":"ik_max_word"
},
"totalword":{
"type":"keyword"
},
"ngram":{
"type": "text",
"analyzer": "ngram_analyzer"
}
}
}
}
},
"unit_output": {
"_parent": {
"type": "unit_info"
},
"dynamic": false,
"properties": {
"u_out_busi_id":{
"type":"keyword"
},
"u_out_fruit_name":{
"type":"text"
},
"u_out_fruit_keyword_arr":{
"type":"keyword"
}
}
}
}
}
需求:
根据关键词查询机构,并且同时得出这些机构发表的包含有该关键词的专利数,并按照专利数进行倒序排序。
也就是查询条件同时匹配父子表,然后根据字表的数量进行排序
目前我只是实现了查询,查询语句和结果如下:
POST /unit_idx/unit_info/_search
{
"query": {
"bool": {
"must": [
{
"has_child": {
"filter": {
"term": {
"unit_keyword_text.totalword": {
"value": "新材料"
}
}
},
"type": "unit_keyword"
}
},
{
"has_child": {
"query": {
"term": {
"u_out_fruit_keyword_arr": "新材料"
}
},
"type": "unit_output",
"inner_hits":{
"size":0
}
}
}
]
}
}
}


QQ截图20220408153441.png

 
 
问题:
根据查询结果,我想把有15个子元素的机构排在有三个子元素的机构上方,DSL语句中排序那里应该怎么写?
 
 
 
 
 
 
 
已邀请:

zcc_vv - 95

赞同来自:

子查询用constant_score 固定分数,score_mode使用sum,那子查询命中多的自然就排在上面了
 

yixiongmao

赞同来自:

问题初步解决。换了一种思路,用子聚合的方式来进行统计排序:
POST /unit_idx/unit_info/_search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"has_child": {
"filter": {
"term": {
"unit_keyword_text.totalword": {
"value": "新材料"
}
}
},
"type": "unit_keyword"
}
},
{
"has_child": {
"filter": {
"term": {
"u_out_fruit_keyword_arr": "新材料"
}
},
"type": "unit_output"
}
}
]
}
},
"_source":[
"unit_name"
],
"aggs": {
"unitName":{
"terms":{
"field":"unit_name.totalword",
"size":5,
"order": { "to-person": "desc" }
},
"aggs": {
"to-person": {
"children": {
"type": "unit_output"
} ,
"aggs": {
"count": {
"value_count": {
"field": "u_out_fruit_guid"
}
}
}
}
}
}
}
}
结果如下:
{
"took": 29,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 34,
"max_score": 0,
"hits":
},
"aggregations": {
"unitName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 29,
"buckets": [
{
"key": "【测试】北京市农林科学院",
"doc_count": 1,
"to-person": {
"doc_count": 15,
"count": {
"value": 15
}
}
},
{
"key": "【测试】东北农业大学",
"doc_count": 1,
"to-person": {
"doc_count": 3,
"count": {
"value": 3
}
}
},
{
"key": "【测试】中国建筑材料科学研究总院",
"doc_count": 1,
"to-person": {
"doc_count": 3,
"count": {
"value": 3
}
}
},
{
"key": "【测试】内蒙古自治区农牧业科学院",
"doc_count": 1,
"to-person": {
"doc_count": 3,
"count": {
"value": 3
}
}
},
{
"key": "【测试】北京京仪世纪电子股份有限公司",
"doc_count": 1,
"to-person": {
"doc_count": 3,
"count": {
"value": 3
}
}
}
]
}
}
}
但是es只支持 children 聚合,parent 聚合不支持。所以该方案个人觉得有其局限性,还是希望有人能提供检索排序方案。
 

要回复问题请先登录注册