Apply boost values ​​to search queries

master
tesshucom 5 years ago committed by tesshu
parent d4a26ed691
commit bb464f15a8
  1. 12
      airsonic-main/src/main/java/org/airsonic/player/service/search/IndexType.java
  2. 12
      airsonic-main/src/main/java/org/airsonic/player/service/search/QueryFactory.java
  3. 12
      airsonic-main/src/test/java/org/airsonic/player/service/search/QueryFactoryTestCase.java

@ -39,7 +39,7 @@ public enum IndexType {
FieldNames.TITLE, FieldNames.TITLE,
FieldNames.ARTIST), FieldNames.ARTIST),
boosts( boosts(
entry(FieldNames.TITLE, 2F))), entry(FieldNames.TITLE, 1.1F))),
ALBUM( ALBUM(
fieldNames( fieldNames(
@ -47,7 +47,7 @@ public enum IndexType {
FieldNames.ARTIST), FieldNames.ARTIST),
// FieldNames.FOLDER), // XXX 3.x -> 8.x : Remove folder from multi-field search condition // FieldNames.FOLDER), // XXX 3.x -> 8.x : Remove folder from multi-field search condition
boosts( boosts(
entry(FieldNames.ALBUM, 2F))), entry(FieldNames.ALBUM, 1.1F))),
ALBUM_ID3( ALBUM_ID3(
fieldNames( fieldNames(
@ -55,20 +55,18 @@ public enum IndexType {
FieldNames.ARTIST), FieldNames.ARTIST),
// FieldNames.FOLDER_ID), // XXX 3.x -> 8.x : Remove folder from multi-field search condition // FieldNames.FOLDER_ID), // XXX 3.x -> 8.x : Remove folder from multi-field search condition
boosts( boosts(
entry(FieldNames.ALBUM, 2F))), entry(FieldNames.ALBUM, 1.1F))),
ARTIST( ARTIST(
fieldNames( fieldNames(
FieldNames.ARTIST), FieldNames.ARTIST),
// FieldNames.FOLDER), // XXX 3.x -> 8.x : Remove folder from multi-field search condition // FieldNames.FOLDER), // XXX 3.x -> 8.x : Remove folder from multi-field search condition
boosts( boosts()),
entry(FieldNames.ARTIST, 1F))),
ARTIST_ID3( ARTIST_ID3(
fieldNames( fieldNames(
FieldNames.ARTIST), FieldNames.ARTIST),
boosts( boosts()),
entry(FieldNames.ARTIST, 2F))),
; ;

@ -31,6 +31,7 @@ import org.apache.lucene.document.IntPoint;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.search.WildcardQuery;
@ -101,7 +102,8 @@ public class QueryFactory {
* - Self made parser process reduces one library dependency. * - Self made parser process reduces one library dependency.
* - It is easy to make corrections later when changing the query to improve search accuracy. * - 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 { throws IOException {
BooleanQuery.Builder mainQuery = new BooleanQuery.Builder(); BooleanQuery.Builder mainQuery = new BooleanQuery.Builder();
@ -117,7 +119,11 @@ public class QueryFactory {
while (stream.incrementToken()) { while (stream.incrementToken()) {
String token = stream.getAttribute(CharTermAttribute.class).toString(); String token = stream.getAttribute(CharTermAttribute.class).toString();
WildcardQuery wildcardQuery = new WildcardQuery(new Term(fieldName, token.concat(ASTERISK))); 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); fieldsQuerys.add(fieldQuerys);
} }
@ -169,7 +175,7 @@ public class QueryFactory {
BooleanQuery.Builder mainQuery = new BooleanQuery.Builder(); 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); mainQuery.add(multiFieldQuery, Occur.MUST);
boolean isId3 = indexType == IndexType.ALBUM_ID3 || indexType == IndexType.ARTIST_ID3; boolean isId3 = indexType == IndexType.ALBUM_ID3 || indexType == IndexType.ARTIST_ID3;

@ -123,13 +123,13 @@ public class QueryFactoryTestCase {
Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.ALBUM); Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.ALBUM);
assertEquals("SearchAlbum", 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.toString());
query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.ALBUM); query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.ALBUM);
assertEquals("SearchAlbum", 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 + ")", + " folder:" + PATH2 + ")",
query.toString()); query.toString());
} }
@ -143,11 +143,11 @@ public class QueryFactoryTestCase {
Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.SONG); Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.SONG);
assertEquals("SearchSong", 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.toString());
query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.SONG); 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()); + " folder:" + PATH2 + ")", query.toString());
} }
@ -178,13 +178,13 @@ public class QueryFactoryTestCase {
Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.ALBUM_ID3); Query query = queryFactory.search(criteria, SINGLE_FOLDERS, IndexType.ALBUM_ID3);
assertEquals( 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 + ")", + "+(folderId:" + FID1 + ")",
query.toString()); query.toString());
query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.ALBUM_ID3); query = queryFactory.search(criteria, MULTI_FOLDERS, IndexType.ALBUM_ID3);
assertEquals("SearchAlbumId3", 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:" + FID1 + " folderId:"
+ FID2 + ")", + FID2 + ")",
query.toString()); query.toString());

Loading…
Cancel
Save