【问题标题】:Number of docs without nested docs没有嵌套文档的文档数
【发布时间】:2014-12-23 13:46:18
【问题描述】:

我正试图弄清楚如何做类似这个例子的事情。假设我有 3 个具有以下结构的文档:

{
    "name": "test one",
    "images": [
        {
            "id": 1
        }
    ]
}


{
    "name": "test two",
    "images": []
}


{
    "name": "test three",
    "images": [
        {
            "id": 2
        }
    ]
}

我想获取 images 字段中的文档 WITH 对象的计数(在本例中为 2),或者(不太理想)文档计数 images 字段中没有 对象(在本例中为 1)。这是针对聚合查询之一,以防不明显。我已经尝试了大约 100 种不同的聚合类型,包括这个

... 
"withoutPhotos": {
  "nested": {
    "path": "images"
  },
  "aggs": {
    "noPhoto": {
      "missing": {
        "field": "images.id"
      }
    }
  }
}

这个,

... 
"withoutPhotos": {
  "missing": {
    "field": "images"
  }
}

还有很多其他的。有什么想法吗?

【问题讨论】:

    标签: elasticsearch facet


    【解决方案1】:

    这是一个查询,用于返回缺少 images.id 字段的结果(看起来与您的非常相似):

    curl -XGET 'http://localhost:9200/index/test/_search?search_type=count&pretty' -d '
    > { "query" :{
    > "match_all": { } 
    >    },
    >    "aggs": {
    >     "noPhoto": { "missing": {"field": "images.id"}  }
    >   }
    > }'
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 3,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "noPhoto" : {
          "doc_count" : 1
        }
      }
    }
    

    这是一个返回实例数量 images.id 的查询 - 不完全确定这是否是您想要的(它返回的是字段计数而不是文档计数)。

    olly@HomePC:~$ curl -XGET 'http://localhost:9200/index/test/_search?search_type=count&pretty' -d '
    > { "query" :{
    > "match_all": { } 
    >    },
    >    "aggs": {
    >  "images_count" : { "value_count" : { "field" : "images.id" } }
    >   }
    > }'
    {
      "took" : 4,
      "timed_out" : false,
      "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
      },
      "hits" : {
        "total" : 3,
        "max_score" : 0.0,
        "hits" : [ ]
      },
      "aggregations" : {
        "images_count" : {
          "value" : 2
        }
      }
    }
    

    其他选项是在查找“images.id”的查询中添加一些内容 - 例如通配符。

    【讨论】:

      【解决方案2】:

      我会建议这种方法。

      curl -XGET 'http://localhost:9200/test/test/_search?search_type=count&pretty' -d '
       { "query" :{
        "match_all": { } 
           },
          "aggs": {
           "noImage": { "missing": {"field": "images.id"}  }
         }
       }'
      

      结果

      {
        "took" : 4,
        "timed_out" : false,
        "_shards" : {
          "total" : 5,
          "successful" : 5,
          "failed" : 0
        },
        "hits" : {
          "total" : 3,
          "max_score" : 0.0,
          "hits" : [ ]
        },
        "aggregations" : {
          "noImage" : {
            "doc_count" : 1
          }
        }
      }
      

      这里的 hits.total 是该索引中的文档总数。 aggregations.noImage.doc_count 是没有图像的文档数。 因此,具有图像字段的文档数将是 hits.total - aggregations.noImage.doc_count

      1. 具有 image = hits.total - aggregations.noImage.doc_count 的文档
      2. 文档没有 image = aggregations.noImage.doc_count

      【讨论】:

        猜你喜欢
        • 2021-11-12
        • 2019-02-20
        • 2018-09-19
        • 2018-07-24
        • 2018-03-09
        • 2020-11-29
        • 1970-01-01
        相关资源
        最近更新 更多