Add ability to customize searched fields

This commit is contained in:
Chocobozzz 2020-10-23 10:14:44 +02:00
parent a478b5f893
commit f76edea5f8
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
4 changed files with 87 additions and 3 deletions

View File

@ -50,6 +50,35 @@ videos-search:
boost-languages: boost-languages:
enabled: true enabled: true
# Add ability to change videos search fields boost value
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html for more information
# If boost == 0, the field will not be part of the search
search-fields:
name:
boost: 5
description:
boost: 1
tags:
boost: 3
account-display-name:
boost: 2
channel-display-name:
boost: 2
channels-search:
# Add ability to change channels search fields boost value
# See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html for more information
# If boost == 0, the field will not be part of the search
search-fields:
name:
boost: 5
description:
boost: 1
display-name:
boost: 3
account-display-name:
boost: 2
api: api:
# Blacklist hosts that will not be returned by the search API # Blacklist hosts that will not be returned by the search API
blacklist: blacklist:

View File

@ -34,6 +34,48 @@ const CONFIG = {
VIDEOS_SEARCH: { VIDEOS_SEARCH: {
BOOST_LANGUAGES: { BOOST_LANGUAGES: {
ENABLED: config.get<boolean>('videos-search.boost-languages.enabled') ENABLED: config.get<boolean>('videos-search.boost-languages.enabled')
},
SEARCH_FIELDS: {
NAME: {
FIELD_NAME: 'name',
BOOST: config.get<number>('videos-search.search-fields.name.boost')
},
DESCRIPTION: {
FIELD_NAME: 'description',
BOOST: config.get<number>('videos-search.search-fields.description.boost')
},
TAGS: {
FIELD_NAME: 'tags',
BOOST: config.get<number>('videos-search.search-fields.tags.boost')
},
ACCOUNT_DISPLAY_NAME: {
FIELD_NAME: 'account.displayName',
BOOST: config.get<number>('videos-search.search-fields.account-display-name.boost')
},
CHANNEL_DISPLAY_NAME: {
FIELD_NAME: 'channel.displayName',
BOOST: config.get<number>('videos-search.search-fields.channel-display-name.boost')
}
}
},
CHANNELS_SEARCH: {
SEARCH_FIELDS: {
NAME: {
FIELD_NAME: 'name',
BOOST: config.get<number>('channels-search.search-fields.name.boost')
},
DESCRIPTION: {
FIELD_NAME: 'description',
BOOST: config.get<number>('channels-search.search-fields.description.boost')
},
DISPLAY_NAME: {
FIELD_NAME: 'displayName',
BOOST: config.get<number>('channels-search.search-fields.display-name.boost')
},
ACCOUNT_DISPLAY_NAME: {
FIELD_NAME: 'ownerAccount.displayName',
BOOST: config.get<number>('channels-search.search-fields.account-display-name.boost')
}
} }
}, },
INSTANCES_INDEX: { INSTANCES_INDEX: {
@ -78,7 +120,9 @@ const REQUESTS = {
const ELASTIC_SEARCH_QUERY = { const ELASTIC_SEARCH_QUERY = {
FUZZINESS: 'AUTO:4,7', FUZZINESS: 'AUTO:4,7',
BOOST_LANGUAGE_VALUE: 2 BOOST_LANGUAGE_VALUE: 2,
VIDEOS_MULTI_MATCH_FIELDS: buildMultiMatchFields(CONFIG.VIDEOS_SEARCH.SEARCH_FIELDS),
CHANNELS_MULTI_MATCH_FIELDS: buildMultiMatchFields(CONFIG.CHANNELS_SEARCH.SEARCH_FIELDS)
} }
function getWebserverUrl () { function getWebserverUrl () {
@ -89,6 +133,17 @@ function getWebserverUrl () {
return CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT return CONFIG.WEBSERVER.SCHEME + '://' + CONFIG.WEBSERVER.HOSTNAME + ':' + CONFIG.WEBSERVER.PORT
} }
function buildMultiMatchFields (fields: { [name: string]: { BOOST: number, FIELD_NAME: string } }) {
return Object.keys(fields)
.map(id => {
const obj = fields[id]
if (obj.BOOST <= 0) return ''
return `${obj.FIELD_NAME}^${obj.BOOST}`
})
.filter(v => !!v)
}
if (isTestInstance()) { if (isTestInstance()) {
SCHEDULER_INTERVALS_MS.videosIndexer = 1000 * 60 * 5 // 5 minutes SCHEDULER_INTERVALS_MS.videosIndexer = 1000 * 60 * 5 // 5 minutes
} }

View File

@ -84,7 +84,7 @@ async function queryChannels (search: ChannelsSearchQuery) {
{ {
multi_match: { multi_match: {
query: search.search, query: search.search,
fields: [ 'name^5', 'displayName^3', 'description' ], fields: ELASTIC_SEARCH_QUERY.CHANNELS_MULTI_MATCH_FIELDS,
fuzziness: ELASTIC_SEARCH_QUERY.FUZZINESS fuzziness: ELASTIC_SEARCH_QUERY.FUZZINESS
} }
} }

View File

@ -116,7 +116,7 @@ async function queryVideos (search: VideosSearchQuery) {
{ {
multi_match: { multi_match: {
query: search.search, query: search.search,
fields: [ 'name^5', 'description', 'tags^3' ], fields: ELASTIC_SEARCH_QUERY.VIDEOS_MULTI_MATCH_FIELDS,
fuzziness: ELASTIC_SEARCH_QUERY.FUZZINESS fuzziness: ELASTIC_SEARCH_QUERY.FUZZINESS
} }
} }