簡體   English   中英

Elasticsearch的$ in(在mongodb中)等價於什么?

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

在Elasticsearch中,我有與線程和查詢匹配的代碼。 當前它可以很好地匹配一個線程,但是我有一個線程數組。 如果字段“線程”與數組中的任何線程匹配,我希望獲得成功。 例如,如果我有一個['1','2','3']數組,那么我想匹配“ thread”字段是否匹配“ 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']
              }
            }
          ]
        }
      }
    }
  })

我認為實現此目標的最佳方法是使用另一種稱為terms查詢方法。

嘗試將您的match查詢更改為terms查詢。 是術語查詢文檔。

例:

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

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" }
            }
        ]
    }
}

希望這可以幫助 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM