gpt4 book ai didi

elasticsearch - 在 Elasticsearch 中加入 reverse_nested 聚合

转载 作者:行者123 更新时间:2023-11-29 02:50:37 25 4
gpt4 key购买 nike

请帮我找到一种机制来聚合以下域或证明它不存在于当前 API 中。

    curl -XDELETE 127.0.0.1:9200/test_index

curl -XPUT 127.0.0.1:9200/test_index -d '{
"mappings": {
"contact": {
"properties": {
"facebook_profile": {
"type": "nested",
"properties": {
"education": {
"type": "string"
},
"year": {
"type": "integer"
}
}
},
"google_profile": {
"type": "nested",
"properties": {
"education": {
"type": "string"
},
"year": {
"type": "integer"
}
}
}
}
}
}
}'

curl -XPUT 127.0.0.1:9200/test_index/contact/contact1 -d '{
"google_profile": {
"education": "stanford", "year": 1990
}
}'

curl -XPUT 127.0.0.1:9200/test_index/contact/contact2 -d '
{
"facebook_profile": {
"education": "stanford", "year": 1990
}
}'

如何查询 ES 以查找有关有多少联系人从特定大学毕业的统计信息?

我找到了一种可能性,但它没有给我想要的结果,因为它无法回答上述关于联系人的问题,而只能回答他们的特定个人资料(嵌套文档):

    curl -XPOST '127.0.0.1:9200/test_index/_search?search_type=count&pretty=true' -d '{
"aggs": {
"facebook_educations": {
"aggs": {
"field": {
"terms": {
"field": "contact.facebook_profile.education"
},
"aggs": {
"reverse": {
"reverse_nested": {
}
}
}
}
},
"nested": {
"path": "contact.facebook_profile"
}
},
"google_educations": {
"aggs": {
"field": {
"terms": {
"field": "contact.google_profile.education"
},
"aggs": {
"reverse": {
"reverse_nested": {
}
}
}
}
},
"nested": {
"path": "contact.google_profile"
}
}
}
}'

给我的是什么:

    "aggregations" : {
"facebook_educations" : {
"doc_count" : 1,
"field" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "stanford",
"doc_count" : 1,
"reverse" : {
"doc_count" : 1
}
} ]
}
},
"google_educations" : {
"doc_count" : 1,
"field" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [ {
"key" : "stanford",
"doc_count" : 1,
"reverse" : {
"doc_count" : 1
}
} ]
}
}
}

但在这里我无法确定找到的联系人是相同的还是不同的文档(父),因此我无法回答我最初的问题。

感谢您的任何建议。

最佳答案

听起来你正在尝试 aggregate on multiple fields . Elasticsearch 不直接支持这一点,但有一些方法可以解决这个问题并获得您正在寻找的结果。

看看 discussion on Github , 以及 documentation .

如果我没理解错的话,无论“stanford”出现在 facebook_profile.education 还是 google_profile.education 中,您都需要 contact在聚合中只计算一次。

您应该能够通过以下两种方式之一执行此操作:

  1. 使用脚本连接存储在字段中的值:

    {
    "aggs": {
    "by_education": {
    "terms": {
    "script": "doc['contact.facebook_profile.education'].values + doc['contact.google_profile.education'].values"
    }
    }
    }
    }
  2. 您可以使用 copy_to 选项在索引时创建一个包含两个字段值的新专用字段。然后在单个字段上聚合。例如,您可以将这两个字段的内容复制到名为 education_combined 的新字段。

    {
    "mappings":{
    "contact":{
    "properties":{
    "facebook_profile":{
    "type":"nested",
    "properties":{
    "education":{
    "type":"string",
    "copy_to":"education_combined"
    },
    "year":{
    "type":"integer"
    }
    }
    },
    "google_profile":{
    "type":"nested",
    "properties":{
    "education":{
    "type":"string",
    "copy_to":"education_combined"
    },
    "year":{
    "type":"integer"
    }
    }
    },
    "education_combined":{
    "type":"string"
    }
    }
    }
    }
    }

    然后,简单地在 education_combined 上聚合:

    {
    "aggs": {
    "by_education": {
    "terms": { "field": "education_combined" }
    }
    }
    }

关于elasticsearch - 在 Elasticsearch 中加入 reverse_nested 聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34856199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com