按嵌套文档中的文档计数进行弹性搜索过滤器
elastic search filter by documents count in nested document
我在弹性搜索中有这个模式。
79[
'ID' : '1233',
Geomtries:[{
'doc1' : 'F1',
'doc2' : 'F2'
},
(optional for some of the documents)
{
'doc2' : 'F1',
'doc3' : 'F2'
}]
]
Geometries 是一个嵌套元素。
我想获取在 Geometries 中具有一个对象的所有文档。
到目前为止已经尝试过:
"script" : {"script" : "if (Geomtries.size < 2) return true"}
但我遇到例外情况:没有这样的 属性 GEOMTRIES
问题在于您在脚本中访问字段的方式,请使用:
doc['Geometry'].size()
or
_source.Geometry.size()
顺便说一句,出于性能原因,我将非规范化并添加 GeometryNumber 字段。您可以使用 transform 映射来计算索引时的大小。
如果映射中的字段类型为 nested
,则典型的 doc[fieldkey].values.size()
方法似乎不起作用。我发现以下脚本可以工作:
{
"from" : 0,
"size" : <SIZE>,
"query" : {
"filtered" : {
"filter" : {
"script" : {
"script" : "_source.containsKey('Geomtries') && _source['Geomtries'].size() == 1"
}
}
}
}
}
注意:您必须使用 _source
而不是 doc
。
我在弹性搜索中有这个模式。
79[
'ID' : '1233',
Geomtries:[{
'doc1' : 'F1',
'doc2' : 'F2'
},
(optional for some of the documents)
{
'doc2' : 'F1',
'doc3' : 'F2'
}]
]
Geometries 是一个嵌套元素。 我想获取在 Geometries 中具有一个对象的所有文档。
到目前为止已经尝试过:
"script" : {"script" : "if (Geomtries.size < 2) return true"}
但我遇到例外情况:没有这样的 属性 GEOMTRIES
问题在于您在脚本中访问字段的方式,请使用:
doc['Geometry'].size()
or
_source.Geometry.size()
顺便说一句,出于性能原因,我将非规范化并添加 GeometryNumber 字段。您可以使用 transform 映射来计算索引时的大小。
如果映射中的字段类型为 nested
,则典型的 doc[fieldkey].values.size()
方法似乎不起作用。我发现以下脚本可以工作:
{
"from" : 0,
"size" : <SIZE>,
"query" : {
"filtered" : {
"filter" : {
"script" : {
"script" : "_source.containsKey('Geomtries') && _source['Geomtries'].size() == 1"
}
}
}
}
}
注意:您必须使用 _source
而不是 doc
。