                         @searchDatabase.push createSearchIndex song
                         # @searchDatabase.push song.filename
 
-        search: (value) =>
-                keywords = createSearchList value
-                cacheKey = keywords.join " "
-                if cacheKey of @searchCache
-                        return @searchCache[cacheKey]
-
-                baseCacheKey = cacheKey
+        _getBaseCache: (key) =>
+                baseCacheKey = key
                 searchIndexes = @searchCache[""]
                 while baseCacheKey != ""
                         if baseCacheKey of @searchCache
                                 searchIndexes = @searchCache[baseCacheKey]
                                 break
                         baseCacheKey =  baseCacheKey.substring 0, baseCacheKey.length - 1
+                return searchIndexes
+
+
+        search: (value) =>
+                keywords = parseKeywords value
+                searchWords = createSearchList keywords
+
+                searchKey = searchWords.join " "
+                if searchKey of @searchCache
+                        return @searchCache[searchKey]
+
+                keywordKey = keywords.join " "
+                if keywordKey of @searchCache
+                        return @searchCache[keywordKey]
+
+                searchIndexes = @_getBaseCache searchKey
+                if keywordKey != searchKey
+                        keywordIndexes = @_getBaseCache keywordKey
+                        if keywordIndexes.length < searchIndexes.length
+                                searchIndexes = keywordIndexes
+
                 result = []
                 for index in searchIndexes
                         haystack = @searchDatabase[index]
                         found = true
-                        for keyword in keywords
+                        for keyword in searchWords
                                 if haystack.indexOf(keyword) == -1
                                         found = false
                                         break
                         if found == true
                                 result.push index
-                @searchCache[cacheKey] = result
+
+                # Save some memory by not creating a new list if all items
+                # are in the older list.
+                if result.length == searchIndexes.length
+                        result = searchIndexes
+
+                @searchCache[keywordKey] = result
+                @searchCache[searchKey] = result
                 return result
 
 

createSearchList = (value) ->
        searchValue = value.replace(/^\s+/, '')
        searchValue = searchValue.replace(/\s+$/, '')
        keywords = searchValue.split " "
        keywordsNormalized = []
        for keyword in keywords
                if keyword == ""
                        continue
                keywordsNormalized.push keyword.toLowerCase()
        keywordsNormalized.sort (a, b) ->
                if (b.length - a.length) == 0
                        if a == b
                                return 0

createSearchIndex = (song) ->
        index = song.filename.toLowerCase()
        if song.album
                album = normalizeKey song.album
                index += " #{Identifier.ALBUM}:#{album}"
        if song.title
                title = normalizeKey song.title
                index += " #{Identifier.TITLE}:#{title}"
        if song.artist
                artist = normalizeKey song.artist
                index += " #{Identifier.ARTIST}:#{artist}"
        return index


