以下是我ES索引中的文档示例:
{
"concepts": [
{
"type": "location",
"entities": [
{ "text": "Raleigh" },
{ "text": "Damascus" },
{ "text": "Brussels" }
]
},
{
"type": "person",
"entities": [
{ "text": "Johnny Cash" },
{ "text": "Barack Obama" },
{ "text": "Vladimir Putin" },
{ "text": "John Hancock" }
]
},
{
"type": "organization",
"entities": [
{ "text": "WTO" },
{ "text": "IMF" },
{ "text": "United States of America" }
]
}
]
}
我正在尝试汇总并计算我的一组文档中每个概念实体的频率,特别是某种特定类型的概念实体。假设我只对聚合“位置”类型的概念实体感兴趣。我的聚合桶将是“concepts.entities.text”,但如果“concepts.type”等于“location”,我仅想聚合它们。以下是我的尝试:
{
"query": {
// Whatever query
},
"aggs": {
"location_concept_type": {
"filter": {
"term": { "concepts.type": "location" }
},
"aggs": {
"entities": {
"terms": { "field": "concepts.hits.text" }
}
}
}
}
}
这样做的问题在于,它会过滤掉聚合中没有任何名为“位置”的概念实体的文档。但对于确实具有“位置”类型概念实体以及其他内容的文档,它会将所有概念实体放入同一个篮子中,而不考虑概念类型。
我还尝试通过以下方式重新构建我的文档:
{
"concepts": [
{
"type": "location",
"text": "Raleigh"
},
{
"type": "location",
"text": "Damascus"
},
{
"type": "location",
"text": "Brussels"
},
{
"type": "person",
"text": "Johnny Cash"
},
{
"type": "person",
"text": "Barack Obama"
}
{
"type": "person",
"text": "Vladimir Putin"
}
{
"type": "person",
"text": "John Hancock"
},
{
"type": "organization",
"text": "WTO"
},
{
"type": "organization",
"text": "IMF"
},
{
"type": "organization",
"text": "United States of America"
}
]
}
但是这也行不通。最终我不能将概念类型作为键(我相信它会解决我的问题),因为我还需要能够在所有概念类型上进行聚合查询(而潜在的概念类型数量可能无限且变化)。有任何想法怎么继续吗?提前感谢你的帮助。