2021-07-29 10:57:33 +02:00
|
|
|
import validator from 'validator'
|
|
|
|
|
2022-06-03 10:54:30 +02:00
|
|
|
import { ELASTIC_SEARCH_QUERY } from '../../../initializers/constants'
|
|
|
|
|
2021-07-29 10:57:33 +02:00
|
|
|
function addUUIDFilters (filters: any[], uuids: string[]) {
|
|
|
|
if (!filters) return
|
|
|
|
|
|
|
|
const result = {
|
|
|
|
shortUUIDs: [] as string[],
|
|
|
|
uuids: [] as string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const uuid of uuids) {
|
|
|
|
if (validator.isUUID(uuid)) result.uuids.push(uuid)
|
|
|
|
else result.shortUUIDs.push(uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
filters.push({
|
|
|
|
bool: {
|
|
|
|
should: [
|
|
|
|
{
|
|
|
|
terms: {
|
|
|
|
uuid: result.uuids
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
terms: {
|
|
|
|
shortUUID: result.shortUUIDs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-03 10:10:14 +02:00
|
|
|
function buildMultiMatchBool (search: string, fieldsObject: { default: string[], phrase: string[] }) {
|
2021-11-24 13:46:03 +01:00
|
|
|
return {
|
|
|
|
must: [
|
|
|
|
{
|
2022-06-03 10:10:14 +02:00
|
|
|
bool: {
|
|
|
|
should: [
|
|
|
|
{
|
|
|
|
multi_match: {
|
|
|
|
query: search,
|
|
|
|
fields: fieldsObject.default,
|
|
|
|
fuzziness: ELASTIC_SEARCH_QUERY.FUZZINESS,
|
|
|
|
operator: ELASTIC_SEARCH_QUERY.OPERATOR,
|
|
|
|
minimum_should_match: ELASTIC_SEARCH_QUERY.MINIMUM_SHOULD_MATCH
|
|
|
|
}
|
|
|
|
},
|
2022-11-16 16:29:51 +01:00
|
|
|
{
|
|
|
|
multi_match: {
|
|
|
|
query: search,
|
|
|
|
fields: fieldsObject.default,
|
|
|
|
operator: ELASTIC_SEARCH_QUERY.OPERATOR,
|
|
|
|
minimum_should_match: ELASTIC_SEARCH_QUERY.MINIMUM_SHOULD_MATCH,
|
2022-12-05 13:49:24 +01:00
|
|
|
type: 'cross_fields'
|
2022-11-16 16:29:51 +01:00
|
|
|
}
|
|
|
|
},
|
2022-06-03 10:10:14 +02:00
|
|
|
{
|
|
|
|
multi_match: {
|
|
|
|
query: search,
|
|
|
|
fields: fieldsObject.phrase,
|
|
|
|
type: 'phrase'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2021-11-24 13:46:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
should: [
|
|
|
|
// Better score for exact search
|
|
|
|
{
|
|
|
|
multi_match: {
|
|
|
|
query: search,
|
2022-06-03 10:10:14 +02:00
|
|
|
fields: [ ...fieldsObject.default, ...fieldsObject.phrase ],
|
2021-11-24 13:46:03 +01:00
|
|
|
operator: ELASTIC_SEARCH_QUERY.OPERATOR,
|
|
|
|
minimum_should_match: ELASTIC_SEARCH_QUERY.MINIMUM_SHOULD_MATCH
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 10:57:33 +02:00
|
|
|
export {
|
2021-11-24 13:46:03 +01:00
|
|
|
addUUIDFilters,
|
|
|
|
buildMultiMatchBool
|
2021-07-29 10:57:33 +02:00
|
|
|
}
|