使用Elastic Search将范围查询添加到多重过滤器全局搜索

时间:2019-04-11 17:46:46

标签: elasticsearch bodybuilder.js

我正在使用Elastic Search 6.7和bodybuilder创建一个基于索引的全局搜索,该搜索需要多个查询。我基于this post查询和过滤器。

我还没有弄清楚如何为doclet的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' } } } } }

0 个答案:

没有答案