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. 10
      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.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()),
;

@ -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,8 +119,12 @@ public class QueryFactory {
while (stream.incrementToken()) {
String token = stream.getAttribute(CharTermAttribute.class).toString();
WildcardQuery wildcardQuery = new WildcardQuery(new Term(fieldName, token.concat(ASTERISK)));
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;

@ -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());

Loading…
Cancel
Save