elasticsearch 如何在Elasticsearch中对列表中的所有元素应用布尔查询?
首先,我对Elasticsearch非常陌生。我正在使用python库来运行查询 我在其他列表中嵌入了列表,例如:elasticsearch 如何在Elasticsearch中对列表中的所有元素应用布尔查询?,elasticsearch,elasticsearch,首先,我对Elasticsearch非常陌生。我正在使用python库来运行查询 我在其他列表中嵌入了列表,例如: {"vendors": [{'id': 22603, 'name': 'Facebook', 'products': [{'id': 4469256, 'name': 'osquery', 'versions': [{'id': 169014, 'name': '3.2.7', 'affected': False,
{"vendors": [{'id': 22603,
'name': 'Facebook',
'products': [{'id': 4469256,
'name': 'osquery',
'versions': [{'id': 169014,
'name': '3.2.7',
'affected': False,},
{'id': 44084,
'name': '3.2.6',
'affected': True}]}]}]}
}
对于上下文,这是一个漏洞数据库
一个供应商可以有多个产品,除名称外,每个产品都有不同的版本
每个版本都有一个名称和一个受影响的标志
我需要得到的是:给我所有的文件,其中产品名称是xxx,版本是yyy,受影响的是zzz
例如:产品名称为osquery,版本为3.2.7,受影响为True
我发送查询的多种方式之一(没有成功)是:
问题是,即使版本3.2.7已影响=False,此查询仍会返回我在上面发布的文档
因此,它似乎在versions列表的元素中执行OR而不是and,因为它找到了一个匹配的版本,另一个不同的版本,具有匹配的受影响的值,它返回文档,但不是预期的结果
有没有办法强迫它使用AND?我在不同的查询中尝试了默认的\u操作符参数,但这似乎只适用于查询\u字符串查询。或者,有没有查询列表中元素的最佳方法?您看到的是数组扁平化的直接结果,如中所述。如果您正在寻找一个简单的解决方案,只需应用、reindex,您的bool-must查询就会“正确”工作
我建议至少将
产品
转换为嵌套
数据类型;甚至可能是母公司,供应商
。但请记住,多层次的嵌套可能会使查询变得冗长,当您试图确定顶级计数时,您可能会发现,是否值得考虑索引的基本构建块是否可能是<>代码>产品<代码> >其<代码>供应商<代码>将是一个属性而不是列出多个产品。单一供应商。您看到的是中所述阵列平坦化的直接结果。如果您正在寻找一个简单的解决方案,只需应用、reindex,您的bool-must查询就会“正确”工作
我建议至少将
产品
转换为嵌套
数据类型;甚至可能是母公司,供应商
。但请记住,多层次的嵌套可能会使查询变得冗长,当您试图确定顶级计数时,您可能会发现,是否值得考虑索引的基本构建块是否可能是<>代码>产品<代码> >其<代码>供应商<代码>将是一个属性而不是列出多个产品。单一供应商。正如@Joe正确指出的那样,多层次的嵌套可能会使您的查询非常冗长。嵌套查询会使查询速度慢几倍
但是,如果您想查询数据(以相同的格式),则需要创建嵌套类型的供应商
、父级
以及版本
<>代码>版本必须是嵌套类型,因为如果您认为不能独立于数组中的其他对象查询每个对象。
添加带有索引映射、搜索查询和搜索结果的工作示例
索引映射:
{
"mappings": {
"properties": {
"vendors": {
"type": "nested",
"properties": {
"products": {
"type": "nested",
"properties": {
"versions": {
"type": "nested"
}
}
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "vendors.products",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.name": "osquery"
}
}
]
}
}
}
},
{
"nested": {
"path": "vendors.products.versions",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.versions.affected": "True"
}
},
{
"match": {
"vendors.products.versions.name": "3.2.7"
}
}
]
}
}
}
}
]
}
}
}
"hits": []
搜索查询:
{
"mappings": {
"properties": {
"vendors": {
"type": "nested",
"properties": {
"products": {
"type": "nested",
"properties": {
"versions": {
"type": "nested"
}
}
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "vendors.products",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.name": "osquery"
}
}
]
}
}
}
},
{
"nested": {
"path": "vendors.products.versions",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.versions.affected": "True"
}
},
{
"match": {
"vendors.products.versions.name": "3.2.7"
}
}
]
}
}
}
}
]
}
}
}
"hits": []
搜索结果:
{
"mappings": {
"properties": {
"vendors": {
"type": "nested",
"properties": {
"products": {
"type": "nested",
"properties": {
"versions": {
"type": "nested"
}
}
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "vendors.products",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.name": "osquery"
}
}
]
}
}
}
},
{
"nested": {
"path": "vendors.products.versions",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.versions.affected": "True"
}
},
{
"match": {
"vendors.products.versions.name": "3.2.7"
}
}
]
}
}
}
}
]
}
}
}
"hits": []
正如@Joe正确指出的那样,多层次的嵌套可能会使您的查询非常冗长。嵌套查询会使查询速度慢几倍 但是,如果您想查询数据(以相同的格式),则需要创建嵌套类型的
供应商
、父级
以及版本
<>代码>版本必须是嵌套类型,因为如果您认为不能独立于数组中的其他对象查询每个对象。
添加带有索引映射、搜索查询和搜索结果的工作示例
索引映射:
{
"mappings": {
"properties": {
"vendors": {
"type": "nested",
"properties": {
"products": {
"type": "nested",
"properties": {
"versions": {
"type": "nested"
}
}
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "vendors.products",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.name": "osquery"
}
}
]
}
}
}
},
{
"nested": {
"path": "vendors.products.versions",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.versions.affected": "True"
}
},
{
"match": {
"vendors.products.versions.name": "3.2.7"
}
}
]
}
}
}
}
]
}
}
}
"hits": []
搜索查询:
{
"mappings": {
"properties": {
"vendors": {
"type": "nested",
"properties": {
"products": {
"type": "nested",
"properties": {
"versions": {
"type": "nested"
}
}
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "vendors.products",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.name": "osquery"
}
}
]
}
}
}
},
{
"nested": {
"path": "vendors.products.versions",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.versions.affected": "True"
}
},
{
"match": {
"vendors.products.versions.name": "3.2.7"
}
}
]
}
}
}
}
]
}
}
}
"hits": []
搜索结果:
{
"mappings": {
"properties": {
"vendors": {
"type": "nested",
"properties": {
"products": {
"type": "nested",
"properties": {
"versions": {
"type": "nested"
}
}
}
}
}
}
}
}
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "vendors.products",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.name": "osquery"
}
}
]
}
}
}
},
{
"nested": {
"path": "vendors.products.versions",
"query": {
"bool": {
"must": [
{
"match": {
"vendors.products.versions.affected": "True"
}
},
{
"match": {
"vendors.products.versions.name": "3.2.7"
}
}
]
}
}
}
}
]
}
}
}
"hits": []
哇!这确实是一个冗长的查询!但它是有效的,这才是重要的。谢谢哇!这确实是一个冗长的查询!但它是有效的,这才是重要的。谢谢