sepia-search-motore-di-rice.../server/lib/elastic-search/elastic-search-videos.ts

483 lines
8.8 KiB
TypeScript
Raw Normal View History

2020-02-20 14:16:55 +01:00
2021-06-24 15:18:54 +02:00
import { exists } from '../../helpers/custom-validators/misc'
import { elasticSearch } from '../../helpers/elastic-search'
import { logger } from '../../helpers/logger'
import { buildUrl } from '../../helpers/utils'
import { CONFIG, ELASTIC_SEARCH_QUERY } from '../../initializers/constants'
import { VideosSearchQuery } from '../../types/search-query/video-search.model'
import { DBVideo, DBVideoDetails, EnhancedVideo, IndexableVideo, IndexableVideoDetails } from '../../types/video.model'
import { buildSort, extractQueryResult } from './elastic-search-queries'
import { buildChannelOrAccountSummaryMapping, formatActorForDB, formatActorSummaryForAPI } from './shared/elastic-search-actor'
2020-02-20 14:16:55 +01:00
2020-02-14 16:14:45 +01:00
async function queryVideos (search: VideosSearchQuery) {
const bool: any = {}
const filter: any[] = []
const mustNot: any[] = []
2020-02-14 16:14:45 +01:00
if (search.search) {
Object.assign(bool, {
must: [
{
multi_match: {
query: search.search,
fields: ELASTIC_SEARCH_QUERY.VIDEOS_MULTI_MATCH_FIELDS,
2020-09-23 10:35:23 +02:00
fuzziness: ELASTIC_SEARCH_QUERY.FUZZINESS
2020-02-14 16:14:45 +01:00
}
}
]
})
}
if (search.blockedAccounts) {
mustNot.push({
terms: {
'account.handle': search.blockedAccounts
}
})
}
if (search.blockedHosts) {
mustNot.push({
terms: {
host: search.blockedHosts
}
})
}
2020-02-14 16:14:45 +01:00
if (search.startDate) {
filter.push({
range: {
publishedAt: {
gte: search.startDate
}
}
})
}
if (search.endDate) {
filter.push({
range: {
publishedAt: {
lte: search.endDate
}
}
})
}
if (search.originallyPublishedStartDate) {
filter.push({
range: {
originallyPublishedAt: {
gte: search.startDate
}
}
})
}
if (search.originallyPublishedEndDate) {
filter.push({
range: {
originallyPublishedAt: {
lte: search.endDate
}
}
})
}
2020-02-18 15:33:21 +01:00
if (search.nsfw && search.nsfw !== 'both') {
2020-02-14 16:14:45 +01:00
filter.push({
term: {
2020-02-18 15:33:21 +01:00
nsfw: (search.nsfw + '') === 'true'
2020-02-14 16:14:45 +01:00
}
})
}
if (search.categoryOneOf) {
filter.push({
terms: {
2020-02-18 15:33:21 +01:00
'category.id': search.categoryOneOf
2020-02-14 16:14:45 +01:00
}
})
}
if (search.licenceOneOf) {
filter.push({
terms: {
2020-02-18 15:33:21 +01:00
'licence.id': search.licenceOneOf
2020-02-14 16:14:45 +01:00
}
})
}
if (search.languageOneOf) {
filter.push({
terms: {
2020-02-18 15:33:21 +01:00
'language.id': search.languageOneOf
2020-02-14 16:14:45 +01:00
}
})
}
if (search.tagsOneOf) {
filter.push({
terms: {
2020-02-14 16:28:11 +01:00
tags: search.tagsOneOf
2020-02-14 16:14:45 +01:00
}
})
}
if (search.tagsAllOf) {
2020-02-14 16:28:11 +01:00
for (const t of search.tagsAllOf) {
filter.push({
term: {
tags: t
}
})
}
2020-02-14 16:14:45 +01:00
}
if (search.durationMin) {
filter.push({
range: {
duration: {
gte: search.durationMin
}
}
})
}
if (search.durationMax) {
filter.push({
range: {
duration: {
lte: search.durationMax
}
}
})
}
2021-05-03 16:52:54 +02:00
if (exists(search.isLive)) {
filter.push({
term: {
isLive: search.isLive
}
})
}
2020-02-14 16:14:45 +01:00
Object.assign(bool, { filter })
if (mustNot.length !== 0) {
Object.assign(bool, { must_not: mustNot })
}
2020-02-18 15:33:21 +01:00
const body = {
from: search.start,
size: search.count,
2020-09-23 11:17:43 +02:00
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 } })
2020-02-18 15:33:21 +01:00
}
logger.debug({ body }, 'Will query Elastic Search for videos.')
2020-02-18 15:33:21 +01:00
2020-02-14 14:09:31 +01:00
const res = await elasticSearch.search({
index: CONFIG.ELASTIC_SEARCH.INDEXES.VIDEOS,
2020-02-18 15:33:21 +01:00
body
2020-02-14 14:09:31 +01:00
})
2020-02-19 15:39:35 +01:00
return extractQueryResult(res)
2020-02-14 14:09:31 +01:00
}
function buildVideosMapping () {
return {
2020-02-14 16:14:45 +01:00
id: {
type: 'long'
},
2020-02-14 14:09:31 +01:00
uuid: {
type: 'keyword'
},
createdAt: {
2020-02-18 15:33:21 +01:00
type: 'date',
format: 'date_optional_time'
2020-02-14 14:09:31 +01:00
},
updatedAt: {
2020-02-18 15:33:21 +01:00
type: 'date',
format: 'date_optional_time'
2020-02-14 14:09:31 +01:00
},
publishedAt: {
2020-02-18 15:33:21 +01:00
type: 'date',
format: 'date_optional_time'
2020-02-14 14:09:31 +01:00
},
originallyPublishedAt: {
2020-02-18 15:33:21 +01:00
type: 'date',
format: 'date_optional_time'
2020-02-14 14:09:31 +01:00
},
indexedAt: {
2020-02-18 15:33:21 +01:00
type: 'date',
format: 'date_optional_time'
2020-02-14 14:09:31 +01:00
},
category: {
properties: {
id: {
type: 'keyword'
},
label: {
type: 'text'
}
}
},
licence: {
properties: {
id: {
type: 'keyword'
},
label: {
type: 'text'
}
}
},
language: {
properties: {
id: {
type: 'keyword'
},
label: {
type: 'text'
}
}
},
privacy: {
properties: {
id: {
type: 'keyword'
},
label: {
type: 'text'
}
}
},
name: {
type: 'text'
},
description: {
type: 'text'
},
2020-02-14 16:14:45 +01:00
tags: {
type: 'text',
fields: {
raw: {
type: 'keyword'
}
}
},
2020-02-14 14:09:31 +01:00
duration: {
type: 'long'
},
thumbnailPath: {
type: 'keyword'
},
previewPath: {
type: 'keyword'
},
embedPath: {
type: 'keyword'
},
2020-06-05 14:37:39 +02:00
url: {
type: 'keyword'
},
2020-02-14 14:09:31 +01:00
views: {
type: 'long'
},
likes: {
type: 'long'
},
dislikes: {
type: 'long'
},
nsfw: {
type: 'boolean'
},
2021-01-18 15:22:14 +01:00
isLive: {
type: 'boolean'
},
2020-02-14 14:09:31 +01:00
host: {
type: 'keyword'
},
account: {
2021-06-24 15:18:54 +02:00
properties: buildChannelOrAccountSummaryMapping()
2020-02-14 14:09:31 +01:00
},
channel: {
2021-06-24 15:18:54 +02:00
properties: buildChannelOrAccountSummaryMapping()
2020-02-14 14:09:31 +01:00
}
}
}
2021-06-24 15:18:54 +02:00
function formatVideoForDB (v: IndexableVideo | IndexableVideoDetails): DBVideo | DBVideoDetails {
return {
id: v.id,
uuid: v.uuid,
indexedAt: new Date(),
createdAt: v.createdAt,
updatedAt: v.updatedAt,
publishedAt: v.publishedAt,
originallyPublishedAt: v.originallyPublishedAt,
category: {
id: v.category.id,
label: v.category.label
},
licence: {
id: v.licence.id,
label: v.licence.label
},
language: {
id: v.language.id,
label: v.language.label
},
privacy: {
id: v.privacy.id,
label: v.privacy.label
},
name: v.name,
description: v.description,
duration: v.duration,
thumbnailPath: v.thumbnailPath,
previewPath: v.previewPath,
embedPath: v.embedPath,
views: v.views,
likes: v.likes,
dislikes: v.dislikes,
isLive: v.isLive || false,
nsfw: v.nsfw,
host: v.host,
url: v.url,
tags: (v as IndexableVideoDetails).tags ? (v as IndexableVideoDetails).tags : undefined,
account: formatActorForDB(v.account),
channel: formatActorForDB(v.channel)
}
}
function formatVideoForAPI (v: DBVideoDetails, fromHost?: string): EnhancedVideo {
return {
id: v.id,
uuid: v.uuid,
score: v.score,
createdAt: new Date(v.createdAt),
updatedAt: new Date(v.updatedAt),
publishedAt: new Date(v.publishedAt),
originallyPublishedAt: v.originallyPublishedAt,
category: {
id: v.category.id,
label: v.category.label
},
licence: {
id: v.licence.id,
label: v.licence.label
},
language: {
id: v.language.id,
label: v.language.label
},
privacy: {
id: v.privacy.id,
label: v.privacy.label
},
name: v.name,
description: v.description,
duration: v.duration,
tags: v.tags,
thumbnailPath: v.thumbnailPath,
thumbnailUrl: buildUrl(v.host, v.thumbnailPath),
previewPath: v.previewPath,
previewUrl: buildUrl(v.host, v.previewPath),
embedPath: v.embedPath,
embedUrl: buildUrl(v.host, v.embedPath),
url: v.url,
isLocal: fromHost && fromHost === v.host,
views: v.views,
likes: v.likes,
dislikes: v.dislikes,
isLive: v.isLive,
nsfw: v.nsfw,
account: formatActorSummaryForAPI(v.account),
channel: formatActorSummaryForAPI(v.channel)
}
}
export {
queryVideos,
formatVideoForDB,
formatVideoForAPI,
buildVideosMapping
}