Я создаю глобальный поиск, который требует нескольких запросов на основе индекса, используя Elastic Search 6.7 и bodybuilder . Я основал свой запрос и фильтры на основе этой записи .
Единственный фильтр, который мне еще предстоит выяснить, как добавить запрос range
для доклета publishDate
. Фильтры отдельных индексов перечислены в комментариях. (Я использовал их для индивидуальной проверки по глобальному поиску.)
let body = bodybuilder()
.query('multi_match', 'query', query)
.size(size)
.from(offset)
.orFilter('bool', b =>
b
.filter('term', 'published', true)
.filter('terms', 'communities', communityIds)
)
.orFilter('bool', b =>
b
.orFilter('terms', 'communities', nonAnonCommunityIds)
.orFilter('terms', '_id', peopleIds)
)
.orFilter('terms', 'community', communityIds)
.orFilter('terms', '_id', eventIds)
// construct the query for individual indicies
// if (index === 'doclet') {
// body = body
// .query('range', 'publishDate', {
// lte: new Date()
// })
// .filter('term', 'published', true)
// .filter('terms', 'communities', communityIds)
// } else if (index === 'person') {
// body = body.filter('terms', 'communities', nonAnonCommunityIds)
// } else if (index === 'post') {
// body = body.filter('terms', 'community', communityIds)
// } else if (index === 'event') {
// body = body.filter('terms', '_id', eventIds)
// }
В глобальном запросе используется следующее тело:
{ size: undefined,
from: undefined,
query:
{ bool:
{ filter:
{ bool:
{ should:
[ { bool:
{ must:
[ { term: { published: true } },
{ terms:
{ communities:
[ '827bbfbd-ab87-4486-9653-2510803351ed',
'8e054f71-75ef-4019-80b9-7349008a39fe',
'98ab80e1-50a6-4300-8916-6d3fd2ce33d9' ]
} } ] } },
{ bool:
{ should:
[ { terms:
{ communities:
[ '827bbfbd-ab87-4486-9653-2510803351ed',
'98ab80e1-50a6-4300-8916-6d3fd2ce33d9',
'8e054f71-75ef-4019-80b9-7349008a39fe' ] } },
{ terms: { _id: [] } } ] } },
{ terms:
{ community:
[ '827bbfbd-ab87-4486-9653-2510803351ed',
'8e054f71-75ef-4019-80b9-7349008a39fe',
'98ab80e1-50a6-4300-8916-6d3fd2ce33d9' ] } },
{ terms:
{ _id:
[ '1b34459d-3aa1-485f-a853-cb0c7db70044',
'98bdde42-a5f3-44eb-9428-118b04df5939' ] } } ] } },
must: { multi_match: { query: 'roundtable' } } } } }