diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/search/IndexType.java b/airsonic-main/src/main/java/org/airsonic/player/service/search/IndexType.java index 2edcf28e..a433a5ec 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/search/IndexType.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/search/IndexType.java @@ -39,7 +39,7 @@ public enum IndexType { FieldNames.TITLE, FieldNames.ARTIST), boosts( - entry(FieldNames.TITLE, 2F))), + entry(FieldNames.TITLE, 1.1F))), ALBUM( fieldNames( @@ -47,7 +47,7 @@ public enum IndexType { FieldNames.ARTIST), // FieldNames.FOLDER), // XXX 3.x -> 8.x : Remove folder from multi-field search condition boosts( - entry(FieldNames.ALBUM, 2F))), + entry(FieldNames.ALBUM, 1.1F))), ALBUM_ID3( fieldNames( @@ -55,20 +55,18 @@ public enum IndexType { FieldNames.ARTIST), // FieldNames.FOLDER_ID), // XXX 3.x -> 8.x : Remove folder from multi-field search condition boosts( - entry(FieldNames.ALBUM, 2F))), + entry(FieldNames.ALBUM, 1.1F))), ARTIST( fieldNames( FieldNames.ARTIST), // FieldNames.FOLDER), // XXX 3.x -> 8.x : Remove folder from multi-field search condition - boosts( - entry(FieldNames.ARTIST, 1F))), + boosts()), ARTIST_ID3( fieldNames( FieldNames.ARTIST), - boosts( - entry(FieldNames.ARTIST, 2F))), + boosts()), ; diff --git a/airsonic-main/src/main/java/org/airsonic/player/service/search/QueryFactory.java b/airsonic-main/src/main/java/org/airsonic/player/service/search/QueryFactory.java index b5155868..74236762 100644 --- a/airsonic-main/src/main/java/org/airsonic/player/service/search/QueryFactory.java +++ b/airsonic-main/src/main/java/org/airsonic/player/service/search/QueryFactory.java @@ -31,6 +31,7 @@ import org.apache.lucene.document.IntPoint; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.WildcardQuery; @@ -101,7 +102,8 @@ public class QueryFactory { * - Self made parser process reduces one library dependency. * - It is easy to make corrections later when changing the query to improve search accuracy. */ - private Query createMultiFieldWildQuery(@NonNull String[] fieldNames, @NonNull String queryString) + private Query createMultiFieldWildQuery(@NonNull String[] fieldNames, @NonNull String queryString, + @NonNull IndexType indexType) throws IOException { BooleanQuery.Builder mainQuery = new BooleanQuery.Builder(); @@ -117,7 +119,11 @@ public class QueryFactory { while (stream.incrementToken()) { String token = stream.getAttribute(CharTermAttribute.class).toString(); WildcardQuery wildcardQuery = new WildcardQuery(new Term(fieldName, token.concat(ASTERISK))); - fieldQuerys.add(wildcardQuery); + if (indexType.getBoosts().containsKey(fieldName)) { + fieldQuerys.add(new BoostQuery(wildcardQuery, indexType.getBoosts().get(fieldName))); + } else { + fieldQuerys.add(wildcardQuery); + } } fieldsQuerys.add(fieldQuerys); } @@ -169,7 +175,7 @@ public class QueryFactory { BooleanQuery.Builder mainQuery = new BooleanQuery.Builder(); - Query multiFieldQuery = createMultiFieldWildQuery(indexType.getFields(), criteria.getQuery()); + Query multiFieldQuery = createMultiFieldWildQuery(indexType.getFields(), criteria.getQuery(), indexType); mainQuery.add(multiFieldQuery, Occur.MUST); boolean isId3 = indexType == IndexType.ALBUM_ID3 || indexType == IndexType.ARTIST_ID3; diff --git a/airsonic-main/src/test/java/org/airsonic/player/service/search/QueryFactoryTestCase.java b/airsonic-main/src/test/java/org/airsonic/player/service/search/QueryFactoryTestCase.java index dda0d023..42107686 100644 --- a/airsonic-main/src/test/java/org/airsonic/player/service/search/QueryFactoryTestCase.java +++ b/airsonic-main/src/test/java/org/airsonic/player/service/search/QueryFactoryTestCase.java @@ -123,13 +123,13 @@ public class QueryFactoryTestCase { Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.ALBUM); assertEquals("SearchAlbum", - "+((album:abc* artist:abc*) (album:def* artist:def*)) +(folder:" + PATH1 + "+(((album:abc*)^1.1 artist:abc*) ((album:def*)^1.1 artist:def*)) +(folder:" + PATH1 + ")", query.toString()); query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.ALBUM); assertEquals("SearchAlbum", - "+((album:abc* artist:abc*) (album:def* artist:def*)) +(folder:" + PATH1 + "+(((album:abc*)^1.1 artist:abc*) ((album:def*)^1.1 artist:def*)) +(folder:" + PATH1 + " folder:" + PATH2 + ")", query.toString()); } @@ -143,11 +143,11 @@ public class QueryFactoryTestCase { Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.SONG); assertEquals("SearchSong", - "+((title:abc* artist:abc*) (title:def* artist:def*)) +(folder:" + PATH1 + ")", + "+(((title:abc*)^1.1 artist:abc*) ((title:def*)^1.1 artist:def*)) +(folder:" + PATH1 + ")", query.toString()); query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.SONG); - assertEquals("SearchSong", "+((title:abc* artist:abc*) (title:def* artist:def*)) +(folder:" + PATH1 + assertEquals("SearchSong", "+(((title:abc*)^1.1 artist:abc*) ((title:def*)^1.1 artist:def*)) +(folder:" + PATH1 + " folder:" + PATH2 + ")", query.toString()); } @@ -178,13 +178,13 @@ public class QueryFactoryTestCase { Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.ALBUM_ID3); assertEquals( - "SearchAlbumId3", "+((album:abc* artist:abc*) (album:def* artist:def*)) " + "SearchAlbumId3", "+(((album:abc*)^1.1 artist:abc*) ((album:def*)^1.1 artist:def*)) " + "+(folderId:" + FID1 + ")", query.toString()); query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.ALBUM_ID3); assertEquals("SearchAlbumId3", - "+((album:abc* artist:abc*) (album:def* artist:def*)) +(folderId:" + "+(((album:abc*)^1.1 artist:abc*) ((album:def*)^1.1 artist:def*)) +(folderId:" + FID1 + " folderId:" + FID2 + ")", query.toString());