2017-01-23 85 views
0

我想获得一个查询运行必须和must_not,但没有任何运气与我尝试的语法。我在StackOverflow上看到很多使用双引号的人,就像他们在Curl调用中一样,但是这是直接从节点应用程序中取出的。Elasticsearch js查询必须和must_not

我将显示可以正常工作的查询,并且我只是试图添加我想要包含在结果中的而不是。在这两种情况下,因为这只是本地开发环境中的垃圾数据,所以结果应该匹配。

首先工作查询:

client.search({ 
    index: config.ES_INDEX, 
    type: "issue", 
    body: { 
     query: { 
      match: { 
       issue_state: 'Closed' 
      } 
     }, 
     size: 1000 
    } 

}).then(function(resp){ 
    console.log(util.inspect(resp, {showHidden: false, depth: null})); 
}).catch(function(err){ 
    console.log('Failed to search. ' + err.message); 
}); 

输出:

{ took: 5, 
timed_out: false, 
    _shards: { total: 5, successful: 5, failed: 0 }, 
    hits: 
    { total: 1, 
    max_score: 1, 
    hits: 
     [ { _index: 'noc_tool', 
      _type: 'issue', 
      _id: 'Sy2IQFMLe', 
      _score: 1, 
      _source: 
      { job_name: 'Job Name 1', 
      is_maintenance: 'no', 
      servicenow_id: 'lkjjklh', 
      type: 'Chase', 
      start_time: '1970-01-01T23:15:00.000Z', 
      maint_reminder: null, 
      update_duration: '4 Hours', 
      location: 'Test Group', 
      issue_state: 'Closed', 
      notes: [ { created_on: 1484063571941, body: 'lkjlkjhlkj' } ], 
      emailService: { lastEmailAt: 1484237594114 }, 
      created_on: 1484063571941, 
      updated_on: 1484240538801, 
      reason: 'because I want to' } } ] } } 

现在,失败的查询:

client.search({ 
    index: config.ES_INDEX, 
    type: "issue", 
    body: { 
     query: { 
      bool: { 
       must: [ 
        { 
         term: { 
          issue_state: 'Closed' 
         } 
        } 
       ], 
       must_not: [ 
        { 
         term: { 
          is_maintenance: 'yes' 
         } 
        } 
       ] 
      } 
     }, 
     size: 1000 
    } 

}).then(function(resp){ 
    console.log(util.inspect(resp, {showHidden: false, depth: null})); 
}).catch(function(err){ 
    console.log('Failed to search. ' + err.message); 
}); 

输出:

{ took: 6, 
timed_out: false, 
    _shards: { total: 5, successful: 5, failed: 0 }, 
    hits: { total: 0, max_score: null, hits: [] } } 

任何帮助在这里将不胜感激。

回答

0

最后我用了一点“反向逻辑”,但这里是什么是工作..

return new Promise(function(resolve, reject) { 
    client.search({ 
     index: config.ES_INDEX, 
     type: "issue", 
     body: { 
      query: { 
       bool: { 
        must:[ 
         { 
          match: { 
           issue_state: 'Closed' 
          } 
         }, 
         { 
          match: { 
           is_maintenance: 'no' 
          } 
         } 
        ] 
       } 
      }, 
      size: 1000 
     } 

    }).then(function (resp) { 
     resolve (resp.hits.hits); 
    }).catch(function (err) { 
     reject('Failed to search. ' + err.message); 
    });