2016-06-20 31 views
0

在Elasticsearch中,我有这个代码匹配一个线程和一个查询。它目前适用于匹配一个线程,但我有一个线程数组。如果字段“线程”匹配数组中的任何线程,我希望得到一个命中。例如,如果我有['1','2','3']的数组,如果“线程”字段匹配“1”,“2”或“3” 1。我该怎么做呢?

client.search({ 
    index: 'searchable-message', 
    body: { 
     query: { 
     bool: { 
      must: [ 
      { 
       match: { 
       thread: '1' //<--WORKS FOR ONE, BUT NOT ARRAY 
       } 
      }, 
      { 
       multi_match: { 
       query: req.query.q, 
       fields: ['message_text', 'stripped_text', 'links', 'documents.text_contents'] 
       } 
      } 
      ] 
     } 
     } 
    } 
    }) 

回答

1

我认为最好的方法是使用一种不同的查询方法,称为terms

尝试使用terms查询更改您的match查询。 Here是术语查询文档。

实施例:

{ 
    "terms": { 
     "thread": ['1', '2', '3'] 
    } 
} 

booldocumentation查询还提供了term查询,它具有与terms查询类似的语法的一个很好的例子:

{ 
    "bool" : { 
     "must" : { 
      "term" : { "user" : "kimchy" } 
     }, 
     "filter": { 
      "term" : { "tag" : "tech" } 
     }, 
     "must_not" : { 
      "range" : { 
       "age" : { "from" : 10, "to" : 20 } 
      } 
     }, 
     "should" : [ 
      { 
       "term" : { "tag" : "wow" } 
      }, 
      { 
       "term" : { "tag" : "elasticsearch" } 
      } 
     ] 
    } 
} 

希望这有助于:)

+0

这工作完美!太好了,谢谢! – user3835653

相关问题