23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

When the query is built through a method like createFileQueryWithOptions, the application and<br />

user filter strings here are combined. What this means is that you can separately manage any filter you<br />

want to apply generally for your app (applicationSearchFilter) from user-supplied search terms<br />

(userSearchFilter). This way you can enforce some search filters without requiring the user to type<br />

them in, and without always having to combine strings yourself.<br />

As noted before, the CommonFileQuery.orderBySearchRank query is meaningful only when combined<br />

with an AQS string, which is to say that keyword-based searches return ranked results for which this<br />

common file query would apply. Returning to Scenario 1 of the Programmatic file search sample, then,<br />

we see how it uses this ordering along with the userSearchFilter property:<br />

var musicLibrary = Windows.Storage.KnownFolders.musicLibrary;<br />

var options = new Windows.Storage.Search.QueryOptions(<br />

Windows.Storage.Search.CommonFileQuery.orderBySearchRank, ["*"]);<br />

options.userSearchFilter = searchFilter;<br />

var fileQuery = musicLibrary.createFileQueryWithOptions(options);<br />

On my machine, where I have a number of songs with “Nightingale” in the title, as well as an album<br />

called “Nightingale Lullaby,” a search using the string "Nightingale" System.ItemType: "mp3" in the<br />

above code gives me results that look like this in the sample:<br />

This shows that the search ranking favors songs with “Nightingale” directly in the title, but also<br />

includes those from an album with that name.<br />

My search string here, by the way, shows how you might use the applicationSearchFilter and<br />

userSearchFilter properties together. If my app was capable of working only with mp3 or some other<br />

formats, I could store "System.Item.Type: 'mp3'" in applicationSearchFilter and store user-provided<br />

terms like "Nightingale" in userSearchFilter. This way I avoid having to join them manually in my<br />

code.<br />

Beyond the properties that you set within a QueryOptions object, it also has some information and<br />

capabilities of its own. The groupPropertyName, for one, is a string property that indicates the type of<br />

property that the query is being grouped by. You can also retrieve the query options as a string using<br />

the saveToString method and recreate the object from a string using loadFromString (that is, the<br />

analog of JSON.stringify and JSON.parse).<br />

358

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!