Initial commit
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
module.exports = function (hitsQuery) {
|
||||
// A shallow copy will prevent hitsQuery from being modified.
|
||||
const queryDsl = { ...hitsQuery }
|
||||
|
||||
queryDsl.from = 0
|
||||
queryDsl.size = 0
|
||||
|
||||
queryDsl.aggs = {
|
||||
primaryDomains: {
|
||||
terms: {
|
||||
field: 'primaryDomain.id',
|
||||
size: 100
|
||||
}
|
||||
},
|
||||
dictionaries: {
|
||||
terms: {
|
||||
field: 'dictionary.id',
|
||||
size: 200
|
||||
}
|
||||
},
|
||||
sources: {
|
||||
terms: {
|
||||
field: 'source.code'
|
||||
}
|
||||
},
|
||||
foreign: {
|
||||
nested: {
|
||||
path: 'foreignEntries'
|
||||
},
|
||||
aggs: {
|
||||
targetLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
module.exports = function (searchString, hitsQuery) {
|
||||
// A shallow copy will prevent hitsQuery from being modified.
|
||||
const queryDsl = { ...hitsQuery }
|
||||
|
||||
queryDsl.from = 0
|
||||
queryDsl.size = 0
|
||||
|
||||
queryDsl.aggs = {
|
||||
primaryDomains: {
|
||||
terms: {
|
||||
field: 'primaryDomain.id',
|
||||
size: 100
|
||||
}
|
||||
},
|
||||
dictionaries: {
|
||||
terms: {
|
||||
field: 'dictionary.id',
|
||||
size: 200
|
||||
}
|
||||
},
|
||||
sources: {
|
||||
terms: {
|
||||
field: 'source.code'
|
||||
}
|
||||
},
|
||||
slovenianHits: {
|
||||
filter: {
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
type: 'phrase',
|
||||
fields: [
|
||||
'term',
|
||||
'synonyms',
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
foreign: {
|
||||
nested: {
|
||||
path: 'foreignEntries'
|
||||
},
|
||||
aggs: {
|
||||
targetLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
},
|
||||
foreignHits: {
|
||||
filter: {
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
type: 'phrase',
|
||||
fields: [
|
||||
'foreignEntries.terms',
|
||||
'foreignEntries.synonyms',
|
||||
'foreignEntries.definition'
|
||||
]
|
||||
}
|
||||
},
|
||||
aggs: {
|
||||
sourceLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
module.exports = function (
|
||||
hitsQuery,
|
||||
slovenianFieldQueries,
|
||||
foreignFieldQueries
|
||||
) {
|
||||
// A shallow copy will prevent hitsQuery from being modified.
|
||||
const queryDsl = { ...hitsQuery }
|
||||
|
||||
queryDsl.from = 0
|
||||
queryDsl.size = 0
|
||||
|
||||
queryDsl.aggs = {
|
||||
primaryDomains: {
|
||||
terms: {
|
||||
field: 'primaryDomain.id',
|
||||
size: 100
|
||||
}
|
||||
},
|
||||
dictionaries: {
|
||||
terms: {
|
||||
field: 'dictionary.id',
|
||||
size: 200
|
||||
}
|
||||
},
|
||||
sources: {
|
||||
terms: {
|
||||
field: 'source.code'
|
||||
}
|
||||
},
|
||||
slovenianHits: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: slovenianFieldQueries
|
||||
}
|
||||
}
|
||||
},
|
||||
foreign: {
|
||||
nested: {
|
||||
path: 'foreignEntries'
|
||||
},
|
||||
aggs: {
|
||||
targetLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
},
|
||||
foreignHits: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: foreignFieldQueries
|
||||
}
|
||||
},
|
||||
aggs: {
|
||||
sourceLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
module.exports = function (searchString, hitsQuery) {
|
||||
// A shallow copy will prevent hitsQuery from being modified.
|
||||
const queryDsl = { ...hitsQuery }
|
||||
|
||||
queryDsl.from = 0
|
||||
queryDsl.size = 0
|
||||
|
||||
queryDsl.aggs = {
|
||||
primaryDomains: {
|
||||
terms: {
|
||||
field: 'primaryDomain.id',
|
||||
size: 100
|
||||
}
|
||||
},
|
||||
dictionaries: {
|
||||
terms: {
|
||||
field: 'dictionary.id',
|
||||
size: 200
|
||||
}
|
||||
},
|
||||
sources: {
|
||||
terms: {
|
||||
field: 'source.code'
|
||||
}
|
||||
},
|
||||
slovenianHits: {
|
||||
filter: {
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'term',
|
||||
'synonyms',
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
foreign: {
|
||||
nested: {
|
||||
path: 'foreignEntries'
|
||||
},
|
||||
aggs: {
|
||||
targetLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
},
|
||||
foreignHits: {
|
||||
filter: {
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'foreignEntries.terms',
|
||||
'foreignEntries.synonyms',
|
||||
'foreignEntries.definition'
|
||||
]
|
||||
}
|
||||
},
|
||||
aggs: {
|
||||
sourceLanguages: {
|
||||
terms: {
|
||||
field: 'foreignEntries.lang.id',
|
||||
size: 50
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// TODO Consider alternative paging when hits exceed 10000.
|
||||
|
||||
module.exports = function (filters, hitsPerPage, page) {
|
||||
const queryDsl = {
|
||||
from: hitsPerPage * (page - 1),
|
||||
size: hitsPerPage,
|
||||
query: {
|
||||
bool: {
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
isPublished: true
|
||||
}
|
||||
},
|
||||
{
|
||||
term: {
|
||||
'dictionary.status': 'published'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: ['_score', 'term.sort', 'homonymSort']
|
||||
}
|
||||
|
||||
if (filters.primaryDomains.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'primaryDomain.id': filters.primaryDomains
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.dictionaries.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'dictionary.id': filters.dictionaries
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sources.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'source.code': filters.sources
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.targetLanguages.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
terms: {
|
||||
'foreignEntries.lang.id': filters.targetLanguages
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
const { separateSlovenianLanguage } = require('../..')
|
||||
|
||||
// TODO Consider alternative paging when hits exceed 10000.
|
||||
|
||||
module.exports = async function (searchString, filters, hitsPerPage, page) {
|
||||
const queryDsl = {
|
||||
from: hitsPerPage * (page - 1),
|
||||
size: hitsPerPage,
|
||||
query: {
|
||||
bool: {
|
||||
must: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
term: {
|
||||
'term.keyword': {
|
||||
value: searchString
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 6
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
match_phrase: {
|
||||
term: searchString
|
||||
}
|
||||
},
|
||||
boost: 5
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
term: {
|
||||
'foreignEntries.terms.keyword': {
|
||||
value: searchString
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 4
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match_phrase: {
|
||||
'foreignEntries.terms': searchString
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 3
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
match_phrase: {
|
||||
synonyms: searchString
|
||||
}
|
||||
},
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match_phrase: {
|
||||
'foreignEntries.synonyms': searchString
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 2
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
type: 'phrase',
|
||||
fields: [
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match_phrase: {
|
||||
'foreignEntries.definition': searchString
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
isPublished: true
|
||||
}
|
||||
},
|
||||
{
|
||||
term: {
|
||||
'dictionary.status': 'published'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: ['_score', 'term.sort', 'homonymSort']
|
||||
}
|
||||
|
||||
if (filters.primaryDomains.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'primaryDomain.id': filters.primaryDomains
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.dictionaries.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'dictionary.id': filters.dictionaries
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sources.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'source.code': filters.sources
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.targetLanguages.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
terms: {
|
||||
'foreignEntries.lang.id': filters.targetLanguages
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sourceLanguages.length) {
|
||||
const [foreignLanguages, isSlovenianSource] =
|
||||
await separateSlovenianLanguage(filters.sourceLanguages)
|
||||
|
||||
const sourceLanguageFilter = {
|
||||
dis_max: {
|
||||
queries: []
|
||||
}
|
||||
}
|
||||
|
||||
if (isSlovenianSource) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
type: 'phrase',
|
||||
fields: [
|
||||
'term',
|
||||
'synonyms',
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (foreignLanguages.length) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{
|
||||
terms: {
|
||||
'foreignEntries.lang.id': foreignLanguages
|
||||
}
|
||||
},
|
||||
{
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
type: 'phrase',
|
||||
fields: [
|
||||
'foreignEntries.terms',
|
||||
'foreignEntries.synonyms',
|
||||
'foreignEntries.definition'
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
queryDsl.query.bool.filter.push(sourceLanguageFilter)
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
const { separateSlovenianLanguage, queryForField } = require('../..')
|
||||
|
||||
const whitespace = /\s+/
|
||||
|
||||
// TODO Consider alternative paging when hits exceed 10000.
|
||||
|
||||
module.exports = async function (searchString, filters, hitsPerPage, page) {
|
||||
const searchTokens = searchString.split(whitespace)
|
||||
|
||||
const termQuery = queryForField('term', searchTokens)
|
||||
const foreignTermQuery = queryForField('foreignEntries.terms', searchTokens)
|
||||
const synonymsQuery = queryForField('synonyms', searchTokens)
|
||||
const foreignSynonymsQuery = queryForField(
|
||||
'foreignEntries.synonyms',
|
||||
searchTokens
|
||||
)
|
||||
const labelQuery = queryForField('label', searchTokens)
|
||||
const definitionQuery = queryForField('definition', searchTokens)
|
||||
const otherQuery = queryForField('other', searchTokens)
|
||||
const domainLabelsQuery = queryForField('domainLabels', searchTokens)
|
||||
const linksQuery = queryForField('links', searchTokens)
|
||||
const foreignDefinitionQuery = queryForField(
|
||||
'foreignEntries.definition',
|
||||
searchTokens
|
||||
)
|
||||
|
||||
const slovenianFieldQueries = [
|
||||
termQuery,
|
||||
synonymsQuery,
|
||||
labelQuery,
|
||||
definitionQuery,
|
||||
otherQuery,
|
||||
domainLabelsQuery,
|
||||
linksQuery
|
||||
]
|
||||
|
||||
const foreignFieldQueries = [
|
||||
foreignTermQuery,
|
||||
foreignSynonymsQuery,
|
||||
foreignDefinitionQuery
|
||||
]
|
||||
|
||||
const queryDsl = {
|
||||
from: hitsPerPage * (page - 1),
|
||||
size: hitsPerPage,
|
||||
query: {
|
||||
bool: {
|
||||
must: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
constant_score: {
|
||||
filter: termQuery,
|
||||
boost: 5
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: foreignTermQuery
|
||||
}
|
||||
},
|
||||
boost: 3
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
synonymsQuery,
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: foreignSynonymsQuery
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 2
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
labelQuery,
|
||||
definitionQuery,
|
||||
otherQuery,
|
||||
domainLabelsQuery,
|
||||
linksQuery,
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: foreignDefinitionQuery
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
isPublished: true
|
||||
}
|
||||
},
|
||||
{
|
||||
term: {
|
||||
'dictionary.status': 'published'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: ['_score', 'term.sort', 'homonymSort']
|
||||
}
|
||||
|
||||
if (filters.primaryDomains.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'primaryDomain.id': filters.primaryDomains
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.dictionaries.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'dictionary.id': filters.dictionaries
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sources.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'source.code': filters.sources
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.targetLanguages.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
terms: {
|
||||
'foreignEntries.lang.id': filters.targetLanguages
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sourceLanguages.length) {
|
||||
const [foreignLanguages, isSlovenianSource] =
|
||||
await separateSlovenianLanguage(filters.sourceLanguages)
|
||||
|
||||
const sourceLanguageFilter = {
|
||||
dis_max: {
|
||||
queries: []
|
||||
}
|
||||
}
|
||||
|
||||
if (isSlovenianSource) {
|
||||
sourceLanguageFilter.dis_max.queries.push(...slovenianFieldQueries)
|
||||
}
|
||||
|
||||
if (foreignLanguages.length) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{
|
||||
terms: {
|
||||
'foreignEntries.lang.id': foreignLanguages
|
||||
}
|
||||
},
|
||||
{
|
||||
dis_max: {
|
||||
queries: foreignFieldQueries
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
queryDsl.query.bool.filter.push(sourceLanguageFilter)
|
||||
}
|
||||
|
||||
return [queryDsl, slovenianFieldQueries, foreignFieldQueries]
|
||||
}
|
||||
@@ -0,0 +1,233 @@
|
||||
const { separateSlovenianLanguage } = require('../..')
|
||||
|
||||
// TODO Consider alternative paging when hits exceed 10000.
|
||||
|
||||
module.exports = async function (searchString, filters, hitsPerPage, page) {
|
||||
const queryDsl = {
|
||||
from: hitsPerPage * (page - 1),
|
||||
size: hitsPerPage,
|
||||
query: {
|
||||
bool: {
|
||||
must: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
match: {
|
||||
term: {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 5
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match: {
|
||||
'foreignEntries.terms': {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 3
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
match: {
|
||||
synonyms: {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match: {
|
||||
'foreignEntries.synonyms': {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 2
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match: {
|
||||
'foreignEntries.definition': {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
isPublished: true
|
||||
}
|
||||
},
|
||||
{
|
||||
term: {
|
||||
'dictionary.status': 'published'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: ['_score', 'term.sort', 'homonymSort']
|
||||
}
|
||||
|
||||
if (filters.primaryDomains.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'primaryDomain.id': filters.primaryDomains
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.dictionaries.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'dictionary.id': filters.dictionaries
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sources.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'source.code': filters.sources
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.targetLanguages.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
terms: {
|
||||
'foreignEntries.lang.id': filters.targetLanguages
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sourceLanguages.length) {
|
||||
const [foreignLanguages, isSlovenianSource] =
|
||||
await separateSlovenianLanguage(filters.sourceLanguages)
|
||||
|
||||
const sourceLanguageFilter = {
|
||||
dis_max: {
|
||||
queries: []
|
||||
}
|
||||
}
|
||||
|
||||
if (isSlovenianSource) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'term',
|
||||
'synonyms',
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (foreignLanguages.length) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{
|
||||
terms: {
|
||||
'foreignEntries.lang.id': foreignLanguages
|
||||
}
|
||||
},
|
||||
{
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'foreignEntries.terms',
|
||||
'foreignEntries.synonyms',
|
||||
'foreignEntries.definition'
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
queryDsl.query.bool.filter.push(sourceLanguageFilter)
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
const { separateSlovenianLanguage } = require('../..')
|
||||
|
||||
// TODO Consider alternative paging when hits exceed 10000.
|
||||
|
||||
module.exports = async function (searchString, filters, hitsPerPage, page) {
|
||||
const queryDsl = {
|
||||
from: hitsPerPage * (page - 1),
|
||||
size: hitsPerPage,
|
||||
query: {
|
||||
bool: {
|
||||
must: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
term: {
|
||||
'term.keyword': {
|
||||
value: searchString
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 6
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
match: {
|
||||
term: {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 5
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
term: {
|
||||
'foreignEntries.terms.keyword': {
|
||||
value: searchString
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 4
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match: {
|
||||
'foreignEntries.terms': {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
boost: 3
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
match: {
|
||||
synonyms: {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match: {
|
||||
'foreignEntries.synonyms': {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 2
|
||||
}
|
||||
},
|
||||
{
|
||||
constant_score: {
|
||||
filter: {
|
||||
dis_max: {
|
||||
queries: [
|
||||
{
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
match: {
|
||||
'foreignEntries.definition': {
|
||||
query: searchString,
|
||||
operator: 'and'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
boost: 1
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
filter: [
|
||||
{
|
||||
term: {
|
||||
isPublished: true
|
||||
}
|
||||
},
|
||||
{
|
||||
term: {
|
||||
'dictionary.status': 'published'
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
sort: ['_score', 'term.sort', 'homonymSort']
|
||||
}
|
||||
|
||||
if (filters.primaryDomains.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'primaryDomain.id': filters.primaryDomains
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.dictionaries.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'dictionary.id': filters.dictionaries
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sources.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
terms: {
|
||||
'source.code': filters.sources
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.targetLanguages.length) {
|
||||
queryDsl.query.bool.filter.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
terms: {
|
||||
'foreignEntries.lang.id': filters.targetLanguages
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (filters.sourceLanguages.length) {
|
||||
const [foreignLanguages, isSlovenianSource] =
|
||||
await separateSlovenianLanguage(filters.sourceLanguages)
|
||||
|
||||
const sourceLanguageFilter = {
|
||||
dis_max: {
|
||||
queries: []
|
||||
}
|
||||
}
|
||||
|
||||
if (isSlovenianSource) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'term',
|
||||
'synonyms',
|
||||
'label',
|
||||
'definition',
|
||||
'other',
|
||||
'domainLabels',
|
||||
'links'
|
||||
]
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (foreignLanguages.length) {
|
||||
sourceLanguageFilter.dis_max.queries.push({
|
||||
nested: {
|
||||
path: 'foreignEntries',
|
||||
query: {
|
||||
bool: {
|
||||
must: [
|
||||
{
|
||||
terms: {
|
||||
'foreignEntries.lang.id': foreignLanguages
|
||||
}
|
||||
},
|
||||
{
|
||||
multi_match: {
|
||||
query: searchString,
|
||||
operator: 'and',
|
||||
fields: [
|
||||
'foreignEntries.terms',
|
||||
'foreignEntries.synonyms',
|
||||
'foreignEntries.definition'
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
queryDsl.query.bool.filter.push(sourceLanguageFilter)
|
||||
}
|
||||
|
||||
return queryDsl
|
||||
}
|
||||
Reference in New Issue
Block a user