简体   繁体   English

Elasticsearch的$ in(在mongodb中)等价于什么?

[英]What's the equivalent of $in (in mongodb) for Elasticsearch?

In Elasticsearch, I have this code that matches a thread and a query. 在Elasticsearch中,我有与线程和查询匹配的代码。 It currently works fine for matching one thread, but I have an array of threads. 当前它可以很好地匹配一个线程,但是我有一个线程数组。 And I want to get a hit if the field "thread" matches any of the threads in the array. 如果字段“线程”与数组中的任何线程匹配,我希望获得成功。 For example, if I have an array of ['1', '2', '3'], I want to match if the "thread" field matches '1', '2', or '3', not just '1'. 例如,如果我有一个['1','2','3']数组,那么我想匹配“ thread”字段是否匹配“ 1”,“ 2”或“ 3”,而不仅仅是“ 1。 How do I do this? 我该怎么做呢?

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']
              }
            }
          ]
        }
      }
    }
  })

I think the best way to achieve this would be to use a different query method called terms . 我认为实现此目标的最佳方法是使用另一种称为terms查询方法。

Try changing you match query with a terms query. 尝试将您的match查询更改为terms查询。 Here is the terms queries documentation. 是术语查询文档。

Example: 例:

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

The bool query documentation also provides a nice example of the term query, which has a similar syntax with the terms query: bool查询文档还提供了一个很好的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" }
            }
        ]
    }
}

Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM