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

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

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.

Việc tìm kiếm và áp dụng bộ lọc cho nội dung do ứng dụng tạo yêu cầu Phạm vi photoslibrary.readonly.appcreateddata. Để biết thêm thông tin về phạm vi, 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 trong thư viện các mục nội dung đa phương tiện do ứng dụng tạo của người dùng cho các loại nội dung đa phương tiện. Ví dụ: bạn có thể chỉ muốn các mục có hình động vật, từ một vào một ngày nhất định hoặc bạn có thể muốn loại trừ ảnh chụp biên nhận. Bạn có thể loại trừ hoặc bao gồm các mục cụ thể bằng cách áp dụng bộ lọc cho danh sách album hoặc thư viện. Có 5 bộ lọc dựa trên thuộc tính của mục nội dung nghe nhìn:

Bạn không nên sử dụng bộ lọc trong yêu cầu mediaItems.search nếu albumId đang thiết lập. Nếu bạn dùng bộ lọc khi đặt albumId, thì sẽ xảy ra lỗi INVALID_ARGUMENT (400) được trả về.

Kết quả được sắp xếp theo thời gian tạo mục nội dung đa phương tiện. Thứ tự sắp xếp có thể sửa đổi đối với truy vấn bằng bộ lọc ngày.

Hãy chờ một thời gian để nội dung nghe nhìn mới tải lên xuất hiện trong nội dung tìm kiếm của bạn. Nội dung nghe nhìn xuất hiện ngay lập tức trong các 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 sẽ không xuất hiện trong các kết quả tìm kiếm đã lọc. Các album này xuất hiện trong kết quả tìm kiếm chưa lọc và kết quả tìm kiếm theo album.

Áp dụng bộ lọc

Để áp dụng 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)

Dưới đâ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
}

PHP

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, album và nội dung nghe nhìn trong thư viện mục.

Loại nội dung

Tất cả các mục nội dung nghe nhìn đều được xử lý và chỉ định 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 đa dạng nội dung nghe nhìn. Danh mục này thường bao gồm mục mà người dùng đã chụp để thực hiện tác vụ nào đó và không có khả năng muốn thực hiện công việc đó đã hoàn tất. Bộ nhớ tạm bao gồm tài liệu, biên nhận, ảnh chụp màn hình, ghi chú dán, 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 này chỉ chính xác ở mức độ chính xác của các nhãn tương đương trong Google Photos. Đôi khi, các mục có thể được gắn nhãn không chính xác, vì vậy, chúng tôi không đảm bảo độ chính xác của bộ lọc danh mục nội dung.

Bao gồm các danh mục

Khi bạn bao gồm nhiều danh mục, mục nội dung đa phương tiện khớp với bất kỳ danh mục được đưa vào. Bạn có thể thêm tối đa 10 danh mục cho mỗi yêu cầu. Bộ lọc ví dụ này trả về các mục bất kỳ 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);

PHP

// 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ỉ những mục nội dung đa phương tiện không khớp với bất kỳ danh mục nào đã bị loại trừ mới xuất hiện. Tương tự như các danh mục đã đưa vào, bạn có thể loại trừ tối đa 10 danh mục theo yêu cầu.

Bộ lọc này trả về bất kỳ mục nào 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);

PHP

// 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 đây trả về LANDSCAPESLANDMARKS, nhưng xoá mọi mục nội dung đa phương tiện chứa PEOPLE hoặc là SELFIES:

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);

PHP

// 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 hạn chế ngày của kết quả được trả về ở một nhóm ngày. Có hai cách để chỉ định bộ lọc ngày: ngày hoặc phạm vi. Ngày và có thể sử dụng cùng nhau. Mục nội dung đa phương tiện khớp với một ngày hoặc ngày bất kỳ dải ô được trả về. Thứ tự sắp xếp kết quả (không bắt buộc) chỉnh sửa được.

Ngày

Ngày chứa năm, tháng và ngày. Các định dạng sau đây đượ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 nhưng không đặt năm, yêu cầu 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);

PHP

// 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()]
);

Phạm vi ngày

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

Phạm vi ngày có startDateendDate, cả hai đều phải được đặt. Một ngày trong phạm vi có cùng giới hạn định dạng như được mô tả trong Ngày. Ngày phải có cùng định dạng: nếu ngày bắt đầu là năm và tháng, thì ngày kết thúc cũng phải là năm và tháng. Các phạm vi là áp dụng toàn diện, ngày bắt đầu và ngày kết thúc đều có trong bộ lọc đượ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);

PHP

// 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ặt hàng thuộc bất kỳ ngày nào trong số các ngày này đều được đưa vào kết quả. Phân cách ngày và phạm vi ngày không cần phải theo cùng một định dạng, nhưng ngày bắt đầu và ngày kết thúc của dải ô riêng lẻ làm:

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);

PHP

// 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()]
);

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

Bộ lọc tính năng giới hạn kết quả ở những mục có những tính năng cụ thể, cho ví dụ được đánh dấu là ảnh yêu thích trong ứng dụng Google Photos.

Yêu thích

Thêm tính năng mục FAVORITES vào FeatureFilter để chỉ trả về các mục nội dung nghe nhìn mà người dùng đã đánh dấu là mục yêu thích:

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);

PHP

// 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ả theo 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
HEIC TIFF
ICO WEBP

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

Video

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

3GP MMV
3G2 Sửa đổi
ASF MOV
AVI MP4
Hàm DIVX MPG
M2T MTS
Hàm M2TS TOD
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.

Ví dụ sau đây 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);

PHP

// 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.

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 các kết quả tìm kiếm. Để bao gồm các mục đã lưu trữ, bạn có thể đặt cờ trong như trong ví dụ sau đây:

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);

PHP

// 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ả tính năng đã yêu cầu được trả về.

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

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);

PHP

// 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 những truy vấn bằng bộ lọc ngày.

Nếu bạn không chỉ định tùy chọn sắp xếp, thì kết quả của bạn sẽ được sắp xếp trong 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 nghe nhìn theo thứ tự tăng dần (các mục cũ nhất trước)

Ví dụ sau đây trả về tất cả các mục nội dung nghe nhìn từ năm 2017, hiển thị mục cũ nhất trước và mục mới nhất sau cùng.

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);

PHP

// 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
    ]
);