【问题标题】:Is there a way to see if a query matches any element in an array in Elasticsearch?有没有办法查看查询是否匹配 Elasticsearch 中数组中的任何元素?
【发布时间】:2016-10-10 14:08:42
【问题描述】:

我有这个查询(例如“hello”)和这个 id(例如“12345”),我想搜索与“text”字段中的查询和“thread”字段中的 id 匹配的内容。但是给定的 ids 在一个数组中,所以逻辑是这样的:

function runThisQuery(query, ids) {
    client.search({        
    index: '_all',
    type: 'text',
    body: {
      query: {
        bool: {
          must: { 
            match: { text: query } 
          }, 
          should: [
            { match: { thread: { query: ids[0], operator: 'AND'} } },
            { match: { thread: { query: ids[1], operator: 'AND'} } }
          ],
          minimum_should_match: 1
        }
      }
    }
  })
}

如果线程在 'ids' 数组中,是否存在与线程匹配的 $in 运算符(如在 MongoDB 中)?谢谢!

【问题讨论】:

    标签: json node.js mongodb elasticsearch


    【解决方案1】:

    您可以像这样使用ids 查询

    {
      "filter": {
        "ids": {
          "type": "my_type",
          "values": [
            "12345","67891","12346"
          ]
        }
      }
    }
    

    【讨论】:

    • 但是如果我知道“价值观”已经是什么的话。它们将是动态的,因为我将它们添加到我的数据库中,它不是线程值的静态数组。
    • 您可以简单地从数据库中获取数组并将其附加到查询中。 { "filter": { "ids": { "type": "my_type", "values": ***json 对你的数组进行编码并放在这里**** } } }
    猜你喜欢
    • 2021-10-16
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多