Elasticsearch相当于WHERE x = 1 AND(y = 2或y = 3)

时间:2017-08-31 09:34:13

标签: javascript elasticsearch

我正在寻找以下伪代码的等价物:

SELECT id, age                                  // works
  FROM students                                 // works
 WHERE firstname = 'John'                       // works
   AND gender = 'm'                             // works   
   AND (lastname = 'Doe' OR lastname = 'Wayne') // no idea

我目前的代码:

{
    // ...
    query: {
        bool: {
            must: [
                { match: { firstname: 'John' }},
                { match: { gender: 'm' }},
            ]
        }
    }
}

我正在与OR挣扎。

有什么想法吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

试试这个:

{
    // ...
    query: {
        bool: {
            must: [
                { match: { firstname: 'John' }},
                { match: { gender: 'm' }},
                {
                  bool: {
                    minimum_should_match: 1, 
                    should: [
                      { match: { lastname: 'Doe' }},
                      { match: { lastname: 'Wayne' }}
                    ]
                  }
                }
            ]
        }
    }
}

答案 1 :(得分:1)

尝试:

{ match: { lastname: /^(Doe|Wayne)$/ }}