Elasticsearch js查询必须和must_not

时间:2017-01-23 21:20:07

标签: node.js elasticsearch

我正在尝试使用must和must_not运行查询,但是我没有尝试使用我正在尝试的语法。我看到很多人在StackOverflow上使用双方的引号,就像他们在Curl调用中一样,但这很直接来自节点应用程序。

我将展示确实有效的查询,我只是想添加我想要包含在结果中的内容。在任何一种情况下,因为这只是本地开发环境中的垃圾数据,结果应匹配。

首先是工作查询:

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: [] } }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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);
    });