2015-10-22 23 views
0

我有文件,它看起来像如何过滤elasticsearch中的嵌套文档?

{ 
    "_id": "56161cb3cbdad2e3b437fdc3", 
    "_type": "Comunity", 
    "name": "public", 
    "data": [ 
     { 
     "title": "sonder", 
     "creationDate": "2015-08-22T03:43:28 -03:00", 
     "quantity": 0 
     }, 
     { 
     "title": "vule", 
     "creationDate": "2014-05-17T12:35:01 -03:00", 
     "quantity": 0 
     }, 
     { 
     "title": "omer", 
     "creationDate": "2015-01-31T04:54:19 -02:00", 
     "quantity": 3 
     }, 
     { 
     "title": "sonder", 
     "creationDate": "2014-05-22T05:09:36 -03:00", 
     "quantity": 3 
     } 
    ] 
    } 

映射:

 comunityDocument": { 
     "_source": { 
      "includes": [ 
      "meta.*" 
      ] 
     }, 
     "properties": { 
      "doc": { 
      "dynamic": "false", 
      "properties": { 
       "data": { 
       "type": "nested", 
       "include_in_parent": true, 
       "properties": { 
        "title": { 
        "type": "string" 
        },      
        "creationDate": { 
        "type": "date", 
        "format": "dateOptionalTime" 
        }, 
        "quantity": { 
        "type": "integer" 
        } 
       } 
       }, 
       "name": { 
       "type": "string", 
       "index": "not_analyzed" 
       } 
      } 
      }, 
      "meta": { 
      "include_in_all": false, 
      "properties": { 
       "expiration": { 
       "type": "long", 
       "include_in_all": false 
       }, 
       "flags": { 
       "type": "long", 
       "include_in_all": false 
       }, 
       "id": { 
       "type": "string", 
       "include_in_all": false 
       }, 
       "rev": { 
       "type": "string", 
       "include_in_all": false 
       } 
      } 
      } 
     } 
     } 

而且我的查询

{ 
    "size": 0, 
    "aggs": { 
    "filterAgg": { 
     "filter": { 
     "nested": { 
      "path": "comunityDocument.doc.data", 
      "filter": { 
      "terms": { 
       "comunityDocument.doc.data.quantity": [ 
       0 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 

至于结果,我必须让所有的“数据”的计数的文件,其中数量等于0但我不明白。奇怪的事情,嵌套聚合工作,但不嵌套过滤器。

+0

那是完整的映射吗? –

回答

1

如果comunityDocument是类型,那么正确的查询应该是

{ 
    "size": 0, 
    "aggs": { 
    "filterAgg": { 
     "filter": { 
     "nested": { 
      "path": "doc.data", 
      "filter": { 
      "terms": { 
       "doc.data.quantity": [ 
       0 
       ] 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

谢谢你是对的! – user2971752

1

这是正确的查询来实现这一目标:

{ 
    "size": 0, 
    "aggs": { 
    "Nest": { 
     "nested": { 
     "path": "data" 
     }, 
     "aggs": { 
     "Filtering": { 
      "filter": { 
      "term": { 
       "quantity": 0 
      } 
      } 
     } 
     } 
    } 
    } 
} 
  1. 你指定你要在嵌套工作字段
  2. 应用字词过滤条件,以便过滤子字段,其中quantity = 0
  3. 您的查询会带回类似这样的内容:

我已经使用了我在question, you previously asked中提供的测试数据。

{ 
    "took": 44, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 3, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "Nest": { 
     "doc_count": 9, 
     "Filtering": { 
      "doc_count": 3 
     } 
     } 
    } 
} 
+0

有趣的不知道你可以这样做 – keety

+0

@ keety但它确实有效,不是吗? –

相关问题