mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-05-28 14:21:34 +00:00
Fix punctuation-insensitive library search
This commit is contained in:
parent
2d0a5462d2
commit
9491db51de
2 changed files with 63 additions and 3 deletions
|
|
@ -966,6 +966,20 @@ WHERE EXISTS (
|
|||
return `unaccent(${value})`
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an expression that strips punctuation the older in-memory search ignored.
|
||||
*
|
||||
* @param {string} expression
|
||||
* @returns {string}
|
||||
*/
|
||||
stripPunctuation(expression) {
|
||||
const punctuationChars = ["'", '.', '`', '"', ',']
|
||||
punctuationChars.forEach((char) => {
|
||||
expression = `replace(${expression}, ${this.sequelize.escape(char)}, '')`
|
||||
})
|
||||
return expression
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the text query.
|
||||
*
|
||||
|
|
@ -983,15 +997,17 @@ WHERE EXISTS (
|
|||
* Get match expression for the specified column.
|
||||
* If the query contains accents, match against the column as-is (case-insensitive exact match).
|
||||
* otherwise match against a normalized column (case-insensitive match with accents removed).
|
||||
* Punctuation is stripped from both sides in all cases.
|
||||
*
|
||||
* @param {string} column
|
||||
* @returns {string}
|
||||
*/
|
||||
matchExpression(column) {
|
||||
const pattern = this.sequelize.escape(`%${this.query}%`)
|
||||
if (!this.supportsUnaccent) return `${column} LIKE ${pattern}`
|
||||
const queryExpr = this.stripPunctuation(this.sequelize.escape(this.query))
|
||||
const pattern = `'%' || ${queryExpr} || '%'`
|
||||
if (!this.supportsUnaccent) return `${this.stripPunctuation(column)} LIKE ${pattern}`
|
||||
const normalizedColumn = this.hasAccents ? column : this.normalize(column)
|
||||
return `${normalizedColumn} LIKE ${pattern}`
|
||||
return `${this.stripPunctuation(normalizedColumn)} LIKE ${pattern}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
44
test/server/utils/queries/libraryItemsBookFilters.test.js
Normal file
44
test/server/utils/queries/libraryItemsBookFilters.test.js
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
const { expect } = require('chai')
|
||||
const { Sequelize } = require('sequelize')
|
||||
|
||||
const Database = require('../../../../server/Database')
|
||||
const libraryItemsBookFilters = require('../../../../server/utils/queries/libraryItemsBookFilters')
|
||||
|
||||
describe('libraryItemsBookFilters.search', () => {
|
||||
beforeEach(async () => {
|
||||
global.ServerSettings = {}
|
||||
Database.sequelize = new Sequelize({ dialect: 'sqlite', storage: ':memory:', logging: false })
|
||||
Database.sequelize.uppercaseFirst = (str) => (str ? `${str[0].toUpperCase()}${str.substr(1)}` : '')
|
||||
Database.supportsUnaccent = false
|
||||
await Database.buildModels()
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
await Database.sequelize.close()
|
||||
})
|
||||
|
||||
it('matches titles when the query omits commas', async () => {
|
||||
const library = await Database.libraryModel.create({ name: 'Test Library', mediaType: 'book' })
|
||||
const libraryFolder = await Database.libraryFolderModel.create({ path: '/test', libraryId: library.id })
|
||||
const book = await Database.bookModel.create({
|
||||
title: 'And Now, Back to You',
|
||||
audioFiles: [],
|
||||
tags: [],
|
||||
narrators: [],
|
||||
genres: [],
|
||||
chapters: []
|
||||
})
|
||||
await Database.libraryItemModel.create({
|
||||
libraryFiles: [],
|
||||
mediaId: book.id,
|
||||
mediaType: 'book',
|
||||
libraryId: library.id,
|
||||
libraryFolderId: libraryFolder.id
|
||||
})
|
||||
|
||||
const results = await libraryItemsBookFilters.search(null, library, 'And Now Back to You', 10, 0)
|
||||
|
||||
expect(results.book).to.have.length(1)
|
||||
expect(results.book[0].libraryItem.media.metadata.title).to.equal('And Now, Back to You')
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue