如何在elasticsearch中过滤嵌套文档?

时间:2015-10-22 13:44:17

标签: 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的所有“数据”文件,但我不明白。嵌套聚合的奇怪之处在于它的工作原理,但不是嵌套的过滤器。

2 个答案:

答案 0 :(得分:1)

如果comunityDocument是类型,则正确的查询应为

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

答案 1 :(得分:1)

实现这一点的正确查询:

{
  "size": 0,
  "aggs": {
    "Nest": {
      "nested": {
        "path": "data"
      },
      "aggs": {
        "Filtering": {
          "filter": {
            "term": {
              "quantity": 0
            }
          }
        }
      }
    }
  }
}
  1. 您指定您将继续使用嵌套字段
  2. 应用术语过滤器,以便过滤quantity = 0
  3. 所在的子网
  4. 您的查询将带回以下内容:
  5. 我使用了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
             }
          }
       }
    }
    
相关问题