【问题标题】:How to get count of documents that match a certain condition in Elasticsearch如何在 Elasticsearch 中获取与特定条件匹配的文档数
【发布时间】:2021-05-18 08:41:29
【问题描述】:

假设,我有一个 MongoDB 查询

db.tm_watch.count({trademark: {$in: [ObjectId('1'), ObjectId('2')]}});

返回商标等于 1 或 2 的文档的计数。 我已尝试将此查询转换为弹性搜索。

 es_query = {
    "query": {
       "bool": {
          "must": [
                     {"terms": {"trademark": ids}},
                     {"term": {"team": req.user.team.id}},
                  ],
                }
              }
            }
 esClient.count({
    index: 'tm_watch',
    type: 'docs',
    body: es_query
}

但我不知道这是否正确,因为我是 Elasticsearch 的新手。 谢谢!

【问题讨论】:

    标签: mongodb elasticsearch


    【解决方案1】:

    相当于mongodb的.count方法的ES是Count API

    假设您的索引名称是 tm_watch 并且字段 trademark 具有 .keyword multi-field 映射,您可以使用 terms query

    POST tm_watch/_count
    {
      "query": {
        "terms": {
          "trademark.keyword": [ "1", "2" ]
        }
      }
    }
    

    【讨论】:

    • 这个@Jayesh 运气好吗?
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-03
    • 2013-03-10
    • 1970-01-01
    相关资源
    最近更新 更多