sepia-search-motore-di-rice.../server/lib/elastic-search/shared/query-helpers.ts

89 lines
2.0 KiB
TypeScript
Raw Normal View History

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
}
}
]
}
})
}
function buildMultiMatchBool (search: string, fieldsObject: { default: string[], phrase: string[] }) {
2021-11-24 13:46:03 +01:00
return {
must: [
{
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
}
},
{
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'
}
},
{
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,
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
}