Samsung Electronics logo

Making Complex Queries Using Filters



Learning how to use filters allows you effectively incorporate complex query methods in your application. You can create queries using AND and OR conditions, like in SQL queries. The following example shows how to make the following query:

"where ((type='VIDEO' OR type='IMAGE') AND title like '%special%')"

Basically, you search in the content of the device for media items where the media type is video or image, and the title contains the word "special".

  1. Create attribute filters to include all content whose media type is either video or image:

    function makeQueryAndFire()
    {
       /* Filter for the video media type */
       var typeVideoFilter = new webapis.AttributeFilter("type", "EXACTLY", "VIDEO");
    
       /* Filter for the image media type */
       var typeImageFilter = new webapis.AttributeFilter("type", "EXACTLY", "IMAGE");
  2. Create a composite filter that finds all content that matches one of the media type filters:

       var typeFilter = new webapis.CompositeFilter("UNION", [typeVideoFilter, typeImageFilter]);
  3. Create another attribute filter that includes content containing the word "special" in its title:

       var titleFilter = new webapis.AttributeFilter("title", "CONTAINS", "special");
  4. Create the final composite filter that finds all content that matches both the composite media type filter and the title filter:

       var finalFilter = new webapis.CompositeFilter("INTERSECTION", [typeFilter, titleFilter]);
  5. Call the findItems() method of the Content API's MediaSource interface to retrieve the media items that match the final filter:

       webapis.mediacontent.getLocalMediaSource().findItems(findMediaContentsCallback, 
                                                          onError, finalFilter);
    }

    The findMediaContentsCallback() event handler returns the query result.