Boost scores in brower language

This commit is contained in:
Chocobozzz 2020-09-23 11:17:43 +02:00
parent e586067521
commit c37b360635
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
7 changed files with 86 additions and 5 deletions

View File

@ -426,8 +426,9 @@
import { EnhancedVideoChannel } from '../../../server/types/channel.model'
import VueTagsInput from '@johmun/vue-tags-input'
import Pagination from '../components/Pagination.vue'
import { VideosSearchQuery, VideoChannelsSearchQuery, ResultList } from '../../../PeerTube/shared/models'
import { VideoChannelsSearchQuery, ResultList } from '../../../PeerTube/shared/models'
import Nprogress from 'nprogress'
import { VideosSearchQuery } from 'server/types/video-search.model'
export default Vue.extend({
components: {
@ -632,6 +633,20 @@
]
},
boostLanguagesQuery () {
const languages = new Set<string>()
for (const completeLanguage of navigator.languages) {
const language = completeLanguage.split('-')[0]
if (this.videoLanguages.find(vl => vl.id === language)) {
languages.add(language)
}
}
return Array.from(languages)
},
homeTitleMessage () {
return this.$gettext('Display homepage')
}
@ -763,6 +778,8 @@
const { durationMin, durationMax } = durationRangeToAPIParams(this.formDurationRange)
const { startDate, endDate } = publishedDateRangeToAPIParams(this.formPublishedDateRange)
const boostLanguages = this.boostLanguagesQuery
return {
search: this.formSearch,
@ -772,6 +789,8 @@
startDate,
endDate,
boostLanguages,
nsfw: this.formNSFW || false,
categoryOneOf: this.formCategoryOneOf ? [ this.formCategoryOneOf ] : undefined,

View File

@ -45,6 +45,11 @@ instances-index:
enabled: false
hosts: null
videos-search:
# Allow client to send browser language to boost results score that are in these languages
boost-languages:
enabled: true
api:
# Blacklist hosts that will not be returned by the search API
blacklist:

View File

@ -8,6 +8,10 @@ search-instance:
search_image: '/theme/framasoft/img/sepia-search.svg'
theme: 'framasoft'
videos-search:
boost-languages:
enabled: true
instances-index:
whitelist:
enabled: true
@ -19,6 +23,11 @@ instances-index:
- 'aperi.tube'
- 'peertube.datagueule.tv'
- 'thinkerview.video'
- 'replay.jres.org'
- 'tube.nah.re'
- 'peertube.parleur.net'
- 'video.passageenseine.fr'
- 'peertube.su'
api:
blacklist:

View File

@ -31,6 +31,11 @@ const CONFIG = {
LEGAL_NOTICES_URL: config.get<string>('search-instance.legal_notices_url'),
THEME: config.get<string>('search-instance.theme')
},
VIDEOS_SEARCH: {
BOOST_LANGUAGES: {
ENABLED: config.get<boolean>('videos-search.boost-languages.enabled')
}
},
INSTANCES_INDEX: {
URL: config.get<string>('instances-index.url'),
PUBLIC_URL: config.get<string>('instances-index.public_url'),
@ -72,7 +77,8 @@ const REQUESTS = {
}
const ELASTIC_SEARCH_QUERY = {
FUZZINESS: 'AUTO:4,7'
FUZZINESS: 'AUTO:4,7',
BOOST_LANGUAGE_VALUE: 2
}
function getWebserverUrl () {

View File

@ -259,8 +259,45 @@ async function queryVideos (search: VideosSearchQuery) {
const body = {
from: search.start,
size: search.count,
sort: buildSort(search.sort),
query: { bool }
sort: buildSort(search.sort)
}
// Allow to boost results depending on query languages
if (
CONFIG.VIDEOS_SEARCH.BOOST_LANGUAGES.ENABLED &&
Array.isArray(search.boostLanguages) &&
search.boostLanguages.length !== 0
) {
const boostScript = `
if (doc['language.id'].size() == 0) {
return _score;
}
String language = doc['language.id'].value;
for (String docLang: params.boostLanguages) {
if (docLang == language) return _score * params.boost;
}
return _score;
`
Object.assign(body, {
query: {
script_score: {
query: { bool },
script: {
source: boostScript,
params: {
boostLanguages: search.boostLanguages,
boost: ELASTIC_SEARCH_QUERY.BOOST_LANGUAGE_VALUE
}
}
}
}
})
} else {
Object.assign(body, { query: { bool } })
}
logger.debug({ body }, 'Will query Elastic Search for videos.')

View File

@ -68,6 +68,11 @@ const videosSearchValidator = [
check('durationMin').optional().isInt().withMessage('Should have a valid min duration'),
check('durationMax').optional().isInt().withMessage('Should have a valid max duration'),
check('boostLanguages')
.optional()
.customSanitizer(toArray)
.custom(isStringArray).withMessage('Should have a valid boostLanguages array'),
(req: express.Request, res: express.Response, next: express.NextFunction) => {
logger.debug({ query: req.query, body: req.body }, 'Checking videos search query')

View File

@ -1,4 +1,4 @@
import { VideosSearchQuery as PeerTubeVideosSearchQuery} from '../../PeerTube/shared/models/search/videos-search-query.model'
import { CommonSearch } from './common-search.model'
export type VideosSearchQuery = Omit<PeerTubeVideosSearchQuery, 'skipCount' | 'filter'> & CommonSearch
export type VideosSearchQuery = Omit<PeerTubeVideosSearchQuery, 'skipCount' | 'filter'> & CommonSearch & { boostLanguages: string[] }