【问题标题】:How to filter nested documents in elasticsearch?如何过滤elasticsearch中的嵌套文档?
【发布时间】:2015-10-22 13:44:17
【问题描述】:

我的文件看起来像

  {
    "_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 但我不明白的所有“数据”文档。奇怪的是嵌套聚合有效但嵌套过滤器无效。

【问题讨论】:

  • 那是完整的映射吗?

标签: elasticsearch


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    {
      "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
             }
          }
       }
    }
    

    【讨论】:

    • 有趣的不知道你可以这样做
    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多