2017-08-31 83 views
0

我正在寻找一个等同于下面的伪代码:Elasticsearch相当于其中x = 1和(y = 2或y = 3)

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

任何想法?

提前致谢!

+1

看一看这个https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_multiple_exact_values。 html –

回答

1

尝试:

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

我很惊讶这项工作,因为匹配查询不支持正则表达式。 – Val

1

试试这个:

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