Tìm kiếm và áp dụng bộ lọc

Phạm vi uỷ quyền bắt buộc

API thư viện Google Photos chứa nhiều phạm vi dùng để truy cập vào các mục và album nội dung nghe nhìn. Các mục nội dung nghe nhìn được trả về từ nhiều lệnh gọi sẽ khác nhau dựa trên phạm vi mà nhà phát triển đã yêu cầu.

Phạm vi photoslibrary.readonly cho phép truy cập vào tất cả các mục nội dung đa phương tiện trong thư viện của người dùng. Phạm vi photoslibrary.readonly.appcreateddata chỉ cho phép truy cập vào các mục nội dung đa phương tiện mà ứng dụng tạo. Để biết thêm thông tin, hãy xem bài viết Phạm vi uỷ quyền.

Các bộ lọc có sẵn

Bạn có thể tìm kiếm các loại nội dung nghe nhìn cụ thể trong thư viện của người dùng. Ví dụ: bạn có thể chỉ muốn các mục là động vật vào một ngày nhất định hoặc bạn có thể muốn loại trừ ảnh biên nhận. Bạn có thể loại trừ hoặc thêm các mục cụ thể bằng cách áp dụng bộ lọc cho trang thông tin trên một đĩa nhạc hoặc thư viện. Hiện có 5 bộ lọc dựa trên các thuộc tính của mục nội dung đa phương tiện:

Bạn không nên sử dụng bộ lọc trong yêu cầu mediaItems:search nếu đã đặt albumId. Nếu bạn dùng bộ lọc khi đặt albumId, thì hàm sẽ trả về lỗi INVALID_ARGUMENT (400).

Kết quả được sắp xếp theo thời gian tạo mục nội dung nghe nhìn. Bạn có thể sửa đổi thứ tự sắp xếp cho các truy vấn sử dụng bộ lọc ngày.

Hãy chờ một lúc để nội dung nghe nhìn mới tải lên xuất hiện trong kết quả tìm kiếm. Nội dung nghe nhìn sẽ xuất hiện ngay lập tức trong các kết quả tìm kiếm chưa được lọc.

Các mục nội dung đa phương tiện có ngày trong tương lai không xuất hiện trong các lượt tìm kiếm đã lọc. Chúng xuất hiện trong các kết quả tìm kiếm và tìm kiếm đĩa nhạc chưa lọc.

Áp dụng bộ lọc

Để áp dụng một bộ lọc, hãy gọi mediaItems.search và chỉ định thuộc tính filter.

Kiến trúc chuyển trạng thái đại diện (REST)

Sau đây là yêu cầu POST:

POST https://photoslibrary.googleapis.com/v1/mediaItems:search
Content-type: application/json
Authorization: Bearer oauth2-token
{
  "pageSize": "100",
  "filters": {
    ...
  }
}

Yêu cầu POST trả về phản hồi sau:

{
  "mediaItems": [
    ...
  ],
  "nextPageToken": "token-for-pagination"
}

Java

try {
  // Create a new Filter object
  Filters filters = Filters.newBuilder()
      // .setContentFilter(...)
      // .setDateFilter(...)
      // ...
      .build();

  // Specify the Filter object in the searchMediaItems call
  SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);
  for (MediaItem item : response.iterateAll()) {
    // ...
  }
} catch (ApiException e) {
  // Handle error
}

1.199

try {
    $filtersBuilder = new FiltersBuilder();
    // $filtersBuilder->addIncludedCategory(...);
    // $filtersBuilder->addDate(...);
    // ...
    // Make a search call with the options set in the filters builder
    $response = $photosLibraryClient->searchMediaItems(
        ['filters' => $filtersBuilder->build()]
    );
    foreach ($response->iterateAllElements() as $element) {
        // ...
    }
} catch (\Google\ApiCore\ApiException $e) {
    // Handle error
}

Để biết thông tin chi tiết, hãy xem phần Liệt kê nội dung thư viện, album và mục nội dung nghe nhìn.

Loại nội dung

Tất cả các mục nội dung đa phương tiện đều được xử lý và gán nhãn. Bạn có thể bao gồm và loại trừ bất kỳ danh mục nào sau đây.

ANIMALS FASHION LANDMARKS RECEIPTS WEDDINGS
ARTS FLOWERS LANDSCAPES SCREENSHOTS WHITEBOARDS
BIRTHDAYS FOOD NIGHT SELFIES  
CITYSCAPES GARDENS PEOPLE SPORT  
CRAFTS HOLIDAYS PERFORMANCES TRAVEL  
DOCUMENTS HOUSES PETS UTILITY  

Ảnh tiện ích bao gồm nhiều loại nội dung nghe nhìn. Danh mục này thường bao gồm các mục mà người dùng đã thu thập để thực hiện một số tác vụ mà có khả năng họ sẽ không muốn dùng sau khi tác vụ đó hoàn tất. Mục này bao gồm tài liệu, biên nhận, ảnh chụp màn hình, ghi chú cố định, trình đơn và các mục nội dung đa phương tiện tương tự khác.

Các danh mục chỉ chính xác như các nhãn tương đương trong Google Photos. Đôi khi, các mục có thể bị gắn nhãn sai. Vì vậy, chúng tôi không đảm bảo tính chính xác của các bộ lọc danh mục nội dung.

Bao gồm danh mục

Khi bạn bao gồm nhiều danh mục, các mục nội dung nghe nhìn khớp với bất kỳ danh mục nào sẽ được đưa vào. Mỗi yêu cầu có thể thêm tối đa 10 danh mục. Bộ lọc ví dụ này trả về bất kỳ mục nào là LANDSCAPES hoặc LANDMARKS.

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "contentFilter": {
      "includedContentCategories": [
        "LANDSCAPES",
        "LANDMARKS"
      ]
    }
  }
}

Java

// Create a content filter that includes landmarks and landscapes
ContentFilter contentFilter = ContentFilter.newBuilder()
    .addIncludedContentCategories(ContentCategory.LANDMARKS)
    .addIncludedContentCategories(ContentCategory.LANDSCAPES)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setContentFilter(contentFilter)
    .build();
// Specify the Filter object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a content filter that includes landmarks and landscapes
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->addIncludedCategory(ContentCategory::LANDMARKS);
$filtersBuilder->addIncludedCategory(ContentCategory::LANDSCAPES);
// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Loại trừ danh mục

Chỉ hiển thị các mục nội dung nghe nhìn không khớp với bất kỳ danh mục bị loại trừ nào. Tương tự như các danh mục đã bao gồm, bạn có thể loại trừ tối đa 10 danh mục cho mỗi yêu cầu.

Bộ lọc này trả về các mục không phải là PEOPLE hoặc SELFIES:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "contentFilter": {
      "excludedContentCategories": [
        "PEOPLE",
        "SELFIES"
      ]
    }
  }
}

Java

// Create a content filter that excludes people and selfies
ContentFilter contentFilter = ContentFilter.newBuilder()
    .addExcludedContentCategories(ContentCategory.PEOPLE)
    .addExcludedContentCategories(ContentCategory.SELFIES)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setContentFilter(contentFilter)
    .build();
// Specify the Filter object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a content filter that excludes people and selfies
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->addExcludedCategory(ContentCategory::PEOPLE);
$filtersBuilder->addExcludedCategory(ContentCategory::SELFIES);
// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Bao gồm và loại trừ nhiều danh mục

Bạn có thể bao gồm một số danh mục và loại trừ các danh mục khác. Ví dụ sau trả về LANDSCAPESLANDMARKS, nhưng xoá mọi mục nội dung nghe nhìn có chứa PEOPLE hoặc SELFIES:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "contentFilter": {
      "includedContentCategories": [
        "LANDSCAPES",
        "LANDMARKS"
      ],
      "excludedContentCategories": [
        "PEOPLE",
        "SELFIES"
      ]
    }
  }
}

Java

// Create a content filter that excludes people and selfies and includes landmarks and landscapes
ContentFilter contentFilter = ContentFilter.newBuilder()
    .addIncludedContentCategories(ContentCategory.LANDSCAPES)
    .addIncludedContentCategories(ContentCategory.LANDMARKS)
    .addExcludedContentCategories(ContentCategory.PEOPLE)
    .addExcludedContentCategories(ContentCategory.SELFIES)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setContentFilter(contentFilter)
    .build();
// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a content filter that excludes people and selfies and includes landmarks and landscapes
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->addIncludedCategory(ContentCategory::LANDMARKS);
$filtersBuilder->addIncludedCategory(ContentCategory::LANDSCAPES);
$filtersBuilder->addExcludedCategory(ContentCategory::PEOPLE);
$filtersBuilder->addExcludedCategory(ContentCategory::SELFIES);
// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Ngày và phạm vi ngày

Bộ lọc ngày giới hạn ngày của kết quả được trả về trong một nhóm ngày được chỉ định. Có 2 cách để chỉ định bộ lọc ngày: ngày hoặc phạm vi. Bạn có thể sử dụng cả ngày và phạm vi. Các mục nội dung đa phương tiện khớp với bất kỳ ngày hoặc phạm vi ngày nào đều sẽ được trả về. Bạn có thể sửa đổi thứ tự sắp xếp của kết quả (không bắt buộc).

Ngày

Ngày có chứa năm, tháng và ngày. Các định dạng sau có thể được chấp nhận:

  • Năm
  • Năm, tháng
  • Năm, tháng, ngày
  • Tháng, ngày
  • Tháng

Khi một thành phần của ngày trống hoặc được đặt thành 0, thì thành phần đó được coi là một ký tự đại diện. Ví dụ: nếu bạn đặt ngày và tháng, chứ không phải năm, thì bạn đang yêu cầu các mục từ ngày và tháng đó của năm bất kỳ:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "dateFilter": {
      "dates": [
        {
          "month": 2,
          "day": 15
        }
      ]
    }
  }
}

Java

// Create a new com.google.type.Date object using a builder
// Note that there are different valid combinations as described above
Date dayFebruary15 = Date.newBuilder()
    .setDay(15)
    .setMonth(2)
    .build();
// Create a new dateFilter. You can also set multiple dates here
DateFilter dateFilter = DateFilter.newBuilder()
    .addDates(dayFebruary15)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setDateFilter(dateFilter)
    .build();
// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new Google\Type\Date object with a day and a month
// Note that there are different valid combinations as described above
$dateFebruary15 = new Date();
$dateFebruary15->setDay(15);
$dateFebruary15->setMonth(2);
$filtersBuilder = new FiltersBuilder();
// Add the date to the filter. You can also set multiple dates here
$filtersBuilder->addDate($dateFebruary15);
// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Các phạm vi ngày

Phạm vi ngày linh hoạt hơn so với phạm vi ngày. Ví dụ: thay vì thêm nhiều ngày, bạn có thể sử dụng phạm vi ngày để xem một tập hợp các ngày trong một tháng.

Phạm vi ngày có startDateendDate, cả hai đều phải được đặt. Mỗi ngày trong dải ô có cùng quy tắc ràng buộc về định dạng như mô tả trong phần Ngày. Ngày phải có cùng định dạng: nếu ngày bắt đầu là một năm và một tháng, thì ngày kết thúc cũng phải là một năm và một tháng. Các phạm vi được áp dụng toàn diện, ngày bắt đầu và ngày kết thúc sẽ được đưa vào bộ lọc áp dụng:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "dateFilter": {
      "ranges": [
        {
          "startDate": {
            "year": 2014,
            "month": 6,
            "day": 12
          },
          "endDate": {
            "year": 2014,
            "month": 7,
            "day": 13
          }
        }
      ]
    }
  }
}

Java

// Create new com.google.type.Date objects for two dates
Date day2014June12 = Date.newBuilder()
    .setDay(12)
    .setMonth(6)
    .setYear(2014)
    .build();
Date day2014July13 = Date.newBuilder()
    .setDay(13)
    .setMonth(7)
    .setYear(2014)
    .build();
// Create a DateRange from these two dates
DateRange dateRange = DateRange.newBuilder()
    .setStartDate(day2014June12)
    .setEndDate(day2014July13)
    .build();
// Create a new dateFilter with the date range. You can also set multiple date ranges here
DateFilter dateFilter = DateFilter.newBuilder()
    .addRanges(dateRange)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setDateFilter(dateFilter)
    .build();
// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create two new Google\Type\Date objects
    $date2014June12 = new Date();
    $date2014June12->setDay(12);
    $date2014June12->setMonth(6);
    $date2014June12->setYear(2014);

    $date2014July13 = new Date();
    $date2014July13->setDay(13);
    $date2014July13->setMonth(7);
    $date2014July13->setYear(2014);

    // Add the two dates as a date range to the filter
    // You can also set multiple date ranges here
    $filtersBuilder = new FiltersBuilder();
    $filtersBuilder->addDateRange($date2014June12, $date2014July13);

    // Make a search call with the options set in the filters builder
    $response = $photosLibraryClient->searchMediaItems(
        ['filters' => $filtersBuilder->build()]
    );

Kết hợp ngày và phạm vi ngày

Bạn có thể sử dụng nhiều ngày và nhiều phạm vi ngày cùng một lúc. Các mục thuộc bất kỳ ngày nào trong số này sẽ được đưa vào kết quả. Phạm vi ngày và phạm vi ngày riêng biệt không cần phải tuân theo cùng một định dạng, nhưng ngày bắt đầu và ngày kết thúc của từng phạm vi riêng lẻ là:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "dateFilter": {
      "dates": [
        {
          "year": 2013
        },
        {
          "year": 2011,
          "month": 11
        }
      ],
      "ranges": [
        {
          "startDate": {
            "month": 1
          },
          "endDate": {
            "month": 3
          }
        },
        {
          "startDate": {
            "month": 3,
            "day": 24
          },
          "endDate": {
            "month": 5,
            "day": 2
          }
        }
      ]
    }
  }
}

Java

// Create a new com.google.type.Date object for the year 2013
Date day2013 = Date.newBuilder()
    .setYear(2013)
    .build();
// Create a new com.google.type.Date object for November 2011
Date day2011November = Date.newBuilder()
    .setMonth(11)
    .setYear(2011)
    .build();
// Create a date range for January to March
DateRange dateRangeJanuaryToMarch = DateRange.newBuilder()
    .setStartDate(Date.newBuilder().setMonth(1).build())
    .setEndDate(Date.newBuilder().setMonth(3).build())
    .build();
// Create a date range for March 24 to May 2
DateRange dateRangeMarch24toMay2 = DateRange.newBuilder()
    .setStartDate(Date.newBuilder().setMonth(3).setDay(24).build())
    .setEndDate(Date.newBuilder().setMonth(5).setDay(2).build())
    .build();
// Create a new dateFilter with the dates and date ranges
DateFilter dateFilter = DateFilter.newBuilder()
    .addDates(day2013)
    .addDates(day2011November)
    .addRanges(dateRangeJanuaryToMarch)
    .addRanges(dateRangeMarch24toMay2)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setDateFilter(dateFilter)
    .build();
// Specify the Filter object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new Google\Type\Date object for the year 2013
$date2013 = new Date();
$date2013->setYear(2013);

// Create a new Google\Type\Date object for November 2011
$dateNovember2011 = new Date();
$dateNovember2011->setMonth(11);
$dateNovember2011->setYear(2011);

$filtersBuilder = new FiltersBuilder();

// Create a date range for January to March
$filtersBuilder->addDateRange((new Date())->setMonth(1),
    (new Date())->setMonth(3));

// Create a date range for March 24 to May 2
$filtersBuilder->addDateRange((new Date())->setMonth(3)->setDay(24),
    (new Date())->setMonth(5)->setDay(2));

$filtersBuilder->addDate($date2013);
$filtersBuilder->addDate($dateNovember2011);

// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Các tính năng của mục nội dung đa phương tiện

Các bộ lọc tính năng chỉ cung cấp kết quả cho những mục có một số tính năng cụ thể, chẳng hạn như những mục đã được đánh dấu là yêu thích trong ứng dụng Google Photos.

Mục ưa thích

Bao gồm tính năng mục FAVORITES trong FeatureFilter để chỉ trả về các mục nội dung đa phương tiện đã được người dùng đánh dấu là yêu thích:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters" : {
    "featureFilter": {
      "includedFeatures": [
        "FAVORITES"
      ]
    }
  }
}

Java

// Create a new FeatureFilter for favorite media items
FeatureFilter featureFilter = FeatureFilter.newBuilder()
    .addIncludedFeatures(Feature.FAVORITES)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setFeatureFilter(featureFilter)
    .build();
// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new FeatureFilter for favorite media items
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->addIncludedFeature(Feature::FAVORITES);

// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Loại nội dung nghe nhìn

Bạn có thể giới hạn kết quả cho loại nội dung nghe nhìn: ảnh hoặc video.

Ảnh

PHOTO có thể ở bất kỳ định dạng hình ảnh nào:

BMP JPG
GIF PNG
HÉ LỘ TIFF
ICO WEBP

API này cũng bao gồm các loại ảnh đặc biệt như ảnh trực tiếp, ảnh chuyển động, ảnh toàn cảnh, ảnh toàn cảnh 360 độ và ảnh thực tế ảo trên iOS.

Video

VIDEO có thể ở nhiều định dạng video:

3 điểm MMV
3G2 Sửa đổi
ASF MOV
AVI MP4
Hàm DIVX MPG
M2T MTS
M2TS Điều khoản sử dụng
M4V WMV
MKV  

VIDEO cũng bao gồm các định dạng video đặc biệt như sau: video thực tế ảo, video chuyển động chậm và ảnh động được tạo trong ứng dụng Google Photos.

Sau đây là ví dụ về bộ lọc theo PHOTO:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "mediaTypeFilter": {
      "mediaTypes": [
        "PHOTO"
      ]
    }
  }
}

Java

// Create a new MediaTypeFilter for Photo media items
MediaTypeFilter mediaType = MediaTypeFilter.newBuilder()
    .addMediaTypes(MediaType.PHOTO)
    .build();
// Create a new Filters object
Filters filters = Filters.newBuilder()
    .setMediaTypeFilter(mediaType)
    .build();
// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new MediaTypeFilter for Photo media items
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->setMediaType(MediaType::PHOTO);

// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Bạn không thể kết hợp nhiều bộ lọc loại nội dung nghe nhìn.

Loại trừ dữ liệu được tạo không phải ứng dụng

Để loại trừ các mục nội dung đa phương tiện không phải do ứng dụng của bạn tạo, bạn có thể đặt bộ lọc excludeNonAppCreatedData như minh hoạ trong ví dụ sau:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "excludeNonAppCreatedData": true
  }
}

Java

// Create a new Filters object that excludes media not created by your app
Filters filters = Filters.newBuilder()
    .setExcludeNonAppCreatedData(true)
    .build();

// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new Filters object that excludes media not created by your app
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->setExcludeNonAppCreatedData(true);

// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Xin lưu ý rằng nếu bạn đang sử dụng phạm vi .readonly.appcreateddata, thì bộ lọc này sẽ bị bỏ qua.

Trạng thái đã lưu trữ

Người dùng của bạn có thể đã lưu trữ một số ảnh của họ. Theo mặc định, ảnh đã lưu trữ sẽ không được trả về trong kết quả tìm kiếm. Để bao gồm các mục đã lưu trữ, bạn có thể đặt cờ trong bộ lọc như minh hoạ trong ví dụ sau:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "includeArchivedMedia": true
  }
}

Java

// Create a new Filters object that includes archived media
Filters filters = Filters.newBuilder()
    .setIncludeArchivedMedia(true)
    .build();

// Specify the Filters object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new Filters object that includes archived media
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->setIncludeArchivedMedia(true);

// Make a search call with the options set in the filters builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Kết hợp các bộ lọc

Bạn có thể kết hợp các loại bộ lọc khác nhau. Chỉ những mục phù hợp với tất cả các tính năng được yêu cầu mới được trả về.

Khi bạn kết hợp các bộ lọc, các giới hạn định dạng đối với từng loại bộ lọc sẽ giống như khi bạn sử dụng riêng từng loại bộ lọc. Trong ví dụ sau, chỉ các ảnh đã được phân loại là SPORT và được trả về từ năm 2014 hoặc 2010:

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "contentFilter": {
      "includedContentCategories": [
        "SPORT"
      ]
    },
    "dateFilter": {
      "dates": [
        {
          "year": 2014
        },
        {
          "year": 2010
        }
      ]
    },
    "mediaTypeFilter": {
      "mediaTypes": [
        "PHOTO"
      ]
    }
  }
}

Java

// Create a new ContentFilter that only includes SPORT items
ContentFilter contentFilter = ContentFilter.newBuilder()
    .addIncludedContentCategories(ContentCategory.SPORT)
    .build();
// Create a new media type filter that only includes PHOTO media items
MediaTypeFilter mediaTypeFilter = MediaTypeFilter.newBuilder()
    .addMediaTypes(MediaType.PHOTO)
    .build();
// Create a new DateFilter that only includes items from 2010 or 2014
Date year2014 = Date.newBuilder().setYear(2014).build();
Date year2010 = Date.newBuilder().setYear(2010).build();
DateFilter dateFilter = DateFilter.newBuilder()
    .addDates(year2010)
    .addDates(year2014)
    .build();
// Create a new Filters object combining these filters
Filters filters = Filters.newBuilder()
    .setDateFilter(dateFilter)
    .setMediaTypeFilter(mediaTypeFilter)
    .setContentFilter(contentFilter)
    .build();
// Specify the Filter object in the searchMediaItems call
SearchMediaItemsPagedResponse response = photosLibraryClient.searchMediaItems(filters);

1.199

// Create a new ContentFilter
$filtersBuilder = new FiltersBuilder();
// Only include SPORT items
$filtersBuilder->addIncludedCategory(ContentCategory::SPORT);
// Only include PHOTO media items
$filtersBuilder->setMediaType(MediaType::PHOTO);
// Only include items from 2010 or 2014
$year2014 = new Date();
$year2014->setYear(2014);
$year2010 = new Date();
$year2010->setYear(2010);
$filtersBuilder->addDateRange($year2010, $year2014);

// Make a search call with the options set in the filters builder
// Filters have been combined in the filter builder
$response = $photosLibraryClient->searchMediaItems(
    ['filters' => $filtersBuilder->build()]
);

Sắp xếp kết quả

Chỉ có thể sắp xếp các truy vấn sử dụng bộ lọc ngày.

Nếu bạn không chỉ định chế độ sắp xếp, thì kết quả sẽ được sắp xếp theo thứ tự giảm dần (mới nhất xếp trước).

Bảng này cho thấy các tuỳ chọn được hỗ trợ cho tham số orderBy:

Tham số orderBy
MediaMetadata.creation_time desc Trả về các mục nội dung đa phương tiện theo thứ tự giảm dần (các mục mới nhất xếp trước)
MediaMetadata.creation_time Trả về các mục nội dung đa phương tiện theo thứ tự tăng dần (mục cũ nhất xếp trước)

Ví dụ sau đây trả về tất cả các mục nội dung đa phương tiện của năm 2017, cho thấy mục cũ nhất và mới nhất gần đây nhất.

Kiến trúc chuyển trạng thái đại diện (REST)

{
  "filters": {
    "dateFilter": {
      "dates": [
        {
          "year": 2017
        }
      ]
    }
  },
  "orderBy": "MediaMetadata.creation_time"
}

Java

// Create a new dateFilter for the year 2017.
DateFilter dateFilter = DateFilter.newBuilder()
        .addDates(Date.newBuilder().setYear(2017))
        .build();

// Create a new Filters object
Filters filters = Filters.newBuilder()
        .setDateFilter(dateFilter)
        .build();

// Sort results by oldest item first.
final OrderBy newestFirstOrder = OrderBy.MEDIAMETADATA_CREATION_TIME;

// Specify the filter and sort order in the searchMediaItems call.
SearchMediaItemsPagedResponse response
        = photosLibraryClient.searchMediaItems(filters, newestFirstOrder);

1.199

// Create a new dateFilter for the year 2017.
$filtersBuilder = new FiltersBuilder();
$filtersBuilder->addDate((new Date())->setYear(2017));

// Make a search call with the options set in the filters builder and sort
// the results by oldest item first.
$response = $photosLibraryClient->searchMediaItems(
    [
        'filters' => $filtersBuilder->build(),
        'orderBy' => OrderBy::MEDIAMETADATA_CREATION_TIME
    ]
);