elastic search filter by documents count in nested document

拈花ヽ惹草 提交于 2021-01-28 21:55:00

问题


I have this schema in elastic search.

79[
    'ID' : '1233',
    Geomtries:[{
        'doc1' : 'F1',
        'doc2' : 'F2'

    },
    (optional for some of the documents)
    {
        'doc2' : 'F1',
        'doc3' : 'F2'
    }]
]

the Geometries is a nested element. I want to get all of the documents that have one object inside Geometries.

Tried so far :

"script" : {"script" : "if (Geomtries.size < 2) return true"}

But i get exceptions : no such property GEOMTRIES


回答1:


If you have the field as type nested in the mapping, the typical doc[fieldkey].values.size() approached does not seem to work. I found the following script to work:

{
  "from" : 0,
  "size" : <SIZE>,
  "query" : {
    "filtered" : {
      "filter" : {
        "script" : {
          "script" : "_source.containsKey('Geomtries') && _source['Geomtries'].size() == 1"
        }
      }
    }
  }
}

NB: You must use _source instead of doc.




回答2:


The problem is in the way you access fields in your script, use:

doc['Geometry'].size()
or
_source.Geometry.size()

By the way for performance reasons, I would denormalize and add GeometryNumber field. You can use the transform mapping to compute size at index time.



来源:https://stackoverflow.com/questions/33635074/elastic-search-filter-by-documents-count-in-nested-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!