愚者求师之过,智者从师之长。

请问查询人与人之间合作度,这种聚合查询怎么写呢?

Elasticsearch | 作者 Yu Tao | 发布于2018年05月09日 | 阅读数:6378

请问,我想查询出来,与 张三合作超过5本书的人 ,  
我的数据结构是:
======================
{
"书名":“abc”
“authors”:[
    {"作者":“张三”},
    {"作者":“李四”},
    {"作者":“王五”}
]
}
=====================
{
"书名":“def”
“authors”:[
    {"作者":“张三”},
    {"作者":“李四”}
]
}
 
请问各位同学,这种DSL要怎么写呢?多谢
 
已邀请:

rockybean - Elastic Certified Engineer, ElasticStack Fans,公众号:ElasticTalk

赞同来自: zyb1994111 Yu Tao laoyang360 exceptions cnfang xzy更多 »

这个需求不需要用 nested object,我这里的例子是 6.x 的版本,修改 min_doc_count 来限定合作书本数
 

POST _bulk
{"index":{"_index":"book","_type":"doc","_id":"1"}}
{"book":"book1","author":["tom","lee","lucy"]}
{"index":{"_index":"book","_type":"doc","_id":"2"}}
{"book":"book2","author":["tom","lee"]}
{"index":{"_index":"book","_type":"doc","_id":"3"}}
{"book":"book3","author":["tom","lee","lili"]}
{"index":{"_index":"book","_type":"doc","_id":"4"}}
{"book":"book4","author":["tom","lili"]}
{"index":{"_index":"book","_type":"doc","_id":"5"}}
{"book":"book5","author":["tom","lee"]}


GET book/_search
{
"size":0,
"query":{
"term":{
"author":"tom"
}
},
"aggs":{
"co-authors":{
"terms":{
"field": "author.keyword",
"min_doc_count": 2
}
}
}
}

JackGe

赞同来自: laoyang360 Yu Tao CarrieJin

索引模板信息如下
{
"order": 10,
"template": "fetch_example*",
"settings": {
"index": {
"number_of_shards": "1"
}
},
"mappings": {
"type": {
"_all": {
"enabled": false
},
"properties": {
"书名":{
"type":"string",
"index": "not_analyzed"
},
"作者":{
"type":"string",
"index": "not_analyzed"
},
"authors":{
"type":"nested",
"properties":{
"作者":{ "type":"string",
"index": "not_analyzed"}

}
}
}
}
},
"aliases": {}
}
查询语句为
{
  "size": 0,
  "query": {
    "nested": {
      "path": "authors",
      "query": {
        "term": {
          "authors.作者": {
            "value": "张三"
          }
        }
      }
    }
  },
  "aggs": {
    "1": {
      "nested": {
        "path": "authors"
      },
      "aggs": {
        "2": {
          "terms": {
            "field": "authors.作者",
            "size": 10
          },
          "aggs": {
            "filter": {
              "bucket_selector": {
                "buckets_path": {
                  "the_doc_count": "_count"
                },
                "script": "the_doc_count > 2"
              }
            }
          }
        }
      }
    }
  }
}
查结果如下
{
"took": 849,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 6,
"max_score": 0,
"hits": []
},
"aggregations": {
"1": {
"2": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "张三",
"doc_count": 6
},
{
"key": "李四",
"doc_count": 6
}
]
},
"doc_count": 18
}
}
}

Eviltuzki - 90h后IT

赞同来自:

搜索作者张三,聚合作者,count数量大于5,前提是你的mapping没问题,作者字段必须是不分词的

Jack_Xiong

赞同来自:

您好,请教一下!
我要找大于3个并且小于5个作者的的记录查询语法如何编写?

要回复问题请先登录注册