// Using http://blog.ctaggart.com/2010/08/query-zune-music-collection-with-f.html#!/2010/08/query-zune-music-collection-with-f.html as a base // Also found some help at http://averagedeveloper.blogspot.com/2012/04/querying-zunedbapidll.html /// <summary> /// Reindexes the music by iterating through all music in the Zune Library and saving it in the local Lucene database. Required for search to work correctly /// </summary> /// <seealso cref="Search"/> public void ReIndexMusic() { var library = new ZuneLibrary(); var dbReloaded = false; int returnValue = library.Initialize(null, out dbReloaded); if (returnValue >= 0) { library.Phase2Initialization(out returnValue); if (returnValue >= 0) { library.CleanupTransientMedia(); ZuneQueryList searchResult = library.QueryDatabase(EQueryType.eQueryTypeAllTracks, 0, EQuerySortType.eQuerySortOrderNone, 0, null); if (null != searchResult) { // we have results, index them var writer = new IndexWriter(new SimpleFSDirectory(new DirectoryInfo(LUCENE_INDEX_DIRECTORY)), new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29), true, IndexWriter.MaxFieldLength.UNLIMITED); for (uint i = 0; i < searchResult.Count; i++) { // add a document for each song. We will only store the mediaId and mediaTypeId and store+index the title, artist, and album var doc = new Document(); var mediaId = (UInt32)searchResult.GetFieldValue(i, typeof(UInt32), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_MediaID); var mediaTypeId = (UInt32)searchResult.GetFieldValue(i, typeof(UInt32), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_MediaType); var duration = (UInt32)searchResult.GetFieldValue(i, typeof(UInt32), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_Duration); var artist = (String)searchResult.GetFieldValue(i, typeof(String), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_DisplayArtist); var title = (String)searchResult.GetFieldValue(i, typeof(String), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_Title); var album = (String)searchResult.GetFieldValue(i, typeof(String), (uint)MicrosoftZuneLibrary.SchemaMap.kiIndex_WMAlbumTitle); doc.Add(new Field("mediaId", mediaId.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("mediaTypeId", mediaTypeId.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("duration", duration.ToString(), Field.Store.YES, Field.Index.NO)); doc.Add(new Field("artist", artist, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED)); doc.Add(new Field("album", album, Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc); } writer.Commit(); writer.Close(); } } } }
private ZuneQueryList GetAlbumQueryList() { return(_zuneLibrary.QueryDatabase(EQueryType.eQueryTypeAllAlbums, 0, EQuerySortType.eQuerySortOrderAscending, (uint)SchemaMap.kiIndex_AlbumID, new QueryPropertyBag())); }