在Elasticsearch中过滤和选择组

时间:2015-08-10 11:44:26

标签: elasticsearch

长问题简介:如何在聚合形成的组中进行选择?

有用的细节:我已将1800张照片的EXIF数据编入索引,并使用geohash单元格过滤器(即位置)和日期直方图聚合(即时间)对它们进行分组。

因此,我成功地将在一定距离内彼此接近的照片分组并在特定时间内拍摄。

但现在,我还想在组中选择组( NOT 单个文档),其中包含字段/值对“Image Software”:“Instagram”。如果我使用过滤器,它将只返回特定文档,而不是组。我可以采取什么方法?

(如果我可以添加另一个条件来在组中进行选择,例如另一个字段/值对,则会更好。

下面的POST返回上述组。我现在想要在包含字段/值对“Image Software”:“Instagram”的组中进行选择并返回这些组。

POST  account_index/_search?search_type=count
{
  "query": {
    "filtered": {
      "filter": {
        "geohash_cell": {
          "location": {
            "geohash": "swtcw"
          },
          "neighbors": true,
          "precision": 5
        }
      }
    }
  },
  "aggs": {
    "zamanlar": {
      "date_histogram": {
        "field": "EXIF DateTimeOriginal",
        "interval": "7d"
      },
      "aggs": {
        "grouped": {
          "top_hits": {
            "size": 10000
          }
        }
      }
    }
  }
}

以下是因上述POST而返回的众多群组之一的元素示例:

    {
 "took": 3,
 "timed_out": false,
 "_shards": {
  "total": 5,
  "successful": 5,
   "failed": 0
 },
 "hits": {
  "total": 136,
  "max_score": 0,
  "hits": []
  },
  "aggregations": {
  "zamanlar": {
     "buckets": [
        {
           "key_as_string": "2015:06:11 00:00:00",
           "key": 1433980800000,
           "doc_count": 30,
           "ohy": {
              "hits": {
                 "total": 30,
                 "max_score": 1,
                 "hits": [
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1356",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[16, 35, 697/20]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 2249/50]",
                          "EXIF DateTimeOriginal": "2015:06:15 19:35:35",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0613.JPG",
                          "CalculatedLatitude": 36.85336388888889,
                          "CalculatedLongitude": 30.879161111111113,
                          "location": {
                             "lat": 36.85336388888889,
                             "lon": 30.879161111111113
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 1211/100]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "6217/1147",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1363",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[12, 28, 1356/25]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 2193/50]",
                          "EXIF DateTimeOriginal": "2015:06:16 15:28:55",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0620.JPG",
                          "CalculatedLatitude": 36.85424166666667,
                          "CalculatedLongitude": 30.87885,
                          "location": {
                             "lat": 36.85424166666667,
                             "lon": 30.87885
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 1527/100]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "0",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1368",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[17, 28, 1767/50]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 1097/25]",
                          "EXIF DateTimeOriginal": "2015:06:16 20:28:36",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0625.JPG",
                          "CalculatedLatitude": 36.85438888888889,
                          "CalculatedLongitude": 30.878855555555557,
                          "location": {
                             "lat": 36.85438888888889,
                             "lon": 30.878855555555557
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 79/5]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "0",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1370",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[17, 28, 749/20]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 1097/25]",
                          "EXIF DateTimeOriginal": "2015:06:16 20:28:38",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0627.JPG",
                          "CalculatedLatitude": 36.85438888888889,
                          "CalculatedLongitude": 30.878855555555557,
                          "location": {
                             "lat": 36.85438888888889,
                             "lon": 30.878855555555557
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 79/5]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "0",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1375",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[19, 19, 112/5]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 4469/100]",
                          "EXIF DateTimeOriginal": "2015:06:16 22:19:23",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0632.JPG",
                          "CalculatedLatitude": 36.85251944444445,
                          "CalculatedLongitude": 30.879080555555557,
                          "location": {
                             "lat": 36.85251944444445,
                             "lon": 30.879080555555557
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 907/100]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "8555/2048",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1382",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[19, 22, 119/5]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 4487/100]",
                          "EXIF DateTimeOriginal": "2015:06:16 22:22:24",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0639.JPG",
                          "CalculatedLatitude": 36.852516666666666,
                          "CalculatedLongitude": 30.879130555555555,
                          "location": {
                             "lat": 36.852516666666666,
                             "lon": 30.879130555555555
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 453/50]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "0",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1357",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[16, 35, 921/25]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 2249/50]",
                          "EXIF DateTimeOriginal": "2015:06:15 19:35:37",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0614.JPG",
                          "CalculatedLatitude": 36.85336388888889,
                          "CalculatedLongitude": 30.879161111111113,
                          "location": {
                             "lat": 36.85336388888889,
                             "lon": 30.879161111111113
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 1211/100]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "5153/1024",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1364",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[12, 29, 29/25]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 877/20]",
                          "EXIF DateTimeOriginal": "2015:06:16 15:29:01",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0621.JPG",
                          "CalculatedLatitude": 36.85424722222222,
                          "CalculatedLongitude": 30.878847222222223,
                          "location": {
                             "lat": 36.85424722222222,
                             "lon": 30.878847222222223
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 1529/100]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "0",
                          "Thumbnail XResolution": "72"
                       }
                    },
                    {
                       "_index": "account_index",
                       "_type": "image",
                       "_id": "1369",
                       "_score": 1,
                       "_source": {
                          "GPS GPSTimeStamp": "[17, 28, 749/20]",
                          "Thumbnail YResolution": "72",
                          "GPS GPSLongitudeRef": "E",
                          "GPS GPSLongitude": "[30, 52, 1097/25]",
                          "EXIF DateTimeOriginal": "2015:06:16 20:28:38",
                          "Image Model": "iPhone 5",
                          "EXIF LensModel": "iPhone 5 back camera 4.12mm f/2.4",
                          "GPS GPSLatitudeRef": "N",
                          "FileName": "IMG_0626.JPG",
                          "CalculatedLatitude": 36.85438888888889,
                          "CalculatedLongitude": 30.878855555555557,
                          "location": {
                             "lat": 36.85438888888889,
                             "lon": 30.878855555555557
                          },
                          "EXIF ExifImageLength": "2448",
                          "Image Make": "Apple",
                          "EXIF ExifImageWidth": "3264",
                          "GPS GPSLatitude": "[36, 51, 79/5]",
                          "Image Software": "7.1.2",
                          "GPS GPSAltitude": "0",
                          "Thumbnail XResolution": "72"
                       }
                    }

1 个答案:

答案 0 :(得分:0)

您应该能够通过将现有聚合包装成filter aggregation这样的方式使其正常工作:

POST  account_index/_search?search_type=count
{
  "query": {
    "filtered": {
      "filter": {
        "geohash_cell": {
          "location": {
            "geohash": "swtcw"
          },
          "neighbors": true,
          "precision": 5
        }
      }
    }
  },
  "aggs": {
    "zamanlar": {
      "filter": {               <------ wrap your aggregation into this new filter aggregation
        "term": {
          "Image Software": "Instagram"
        }
      },
      "aggs": {
        "zamanlar": {
          "date_histogram": {
            "field": "EXIF DateTimeOriginal",
            "interval": "7d"
          },
          "aggs": {
            "grouped": {
              "top_hits": {
                "size": 10000
              }
            }
          }
        }
      }
    }
  }
}