ElasticSearch 中 MongoDB .count() 的等效方法

ElasticSearch 中 MongoDB 的 count() 的等效方法也称为 count。它可以以类似的方式使用

当你有类似这样的 ElasticSearch 查询时(Python 示例)

es_equivalent_count.py
result = es.search(index="my_index", body={
    "query": {
        "match": {
            "my_field": "my_value"
        }
    }
})
result_docs = [hit["_source"] for hit in result["hits"]["hits"]]

你可以通过将 search 替换为 count 轻松将其更改为仅计数查询:

es_count_query.py
result = es.count(index="my_index", body={
    "query": {
        "match": {
            "my_field": "my_value"
        }
    }
})
result_count = result["count"] # 例如 200

Check out similar posts by category: Databases, ElasticSearch