SQL Server guide

Count documents in indexes


Counts documents across one or more indexes or aliases using the Elasticsearch query DSL. Use the Index parameter to target a single index or alias, multiple indexes/aliases, or all indexes (with *). Optionally supply a query expression to count only matching documents; see the Elasticsearch query DSL documentation for query syntax.

Standard SQL query example

This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.

SELECT * FROM count WITH(Index='MyIndexOrAliasName') -- get count of documents in index / alias named MyIndexOrAliasName
SELECT * FROM count WITH(Index='*') -- get count of documents in all indices (total distinct _id found across all indices + alias) 
SELECT * FROM count WITH(Index='MyIndex1,MyIndex2,MyAlias1,MyAlias2') -- get count of documents in indices named MyIndex1, MyIndex2 and alias named MyAlias1, MyAlias2
SELECT * FROM count WITH(Index='MyIndexOrAliasName', Query='{"match": { "comment" : "TV" } }') -- get count of documents in MyIndex where comment field contains word "TV"

Using OPENQUERY in SQL Server

SELECT * FROM OPENQUERY([LS_TO_ELASTICSEARCH_IN_GATEWAY], 'SELECT * FROM count WITH(Index=''MyIndexOrAliasName'') -- get count of documents in index / alias named MyIndexOrAliasName
SELECT * FROM count WITH(Index=''*'') -- get count of documents in all indices (total distinct _id found across all indices + alias) 
SELECT * FROM count WITH(Index=''MyIndex1,MyIndex2,MyAlias1,MyAlias2'') -- get count of documents in indices named MyIndex1, MyIndex2 and alias named MyAlias1, MyAlias2
SELECT * FROM count WITH(Index=''MyIndexOrAliasName'', Query=''{"match": { "comment" : "TV" } }'') -- get count of documents in MyIndex where comment field contains word "TV"')

Using EXEC in SQL Server (handling larger SQL text)

The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements. This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).

Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_ELASTICSEARCH_IN_GATEWAY] syntax.

DECLARE @MyQuery NVARCHAR(MAX) = 'SELECT * FROM count WITH(Index=''MyIndexOrAliasName'') -- get count of documents in index / alias named MyIndexOrAliasName
SELECT * FROM count WITH(Index=''*'') -- get count of documents in all indices (total distinct _id found across all indices + alias) 
SELECT * FROM count WITH(Index=''MyIndex1,MyIndex2,MyAlias1,MyAlias2'') -- get count of documents in indices named MyIndex1, MyIndex2 and alias named MyAlias1, MyAlias2
SELECT * FROM count WITH(Index=''MyIndexOrAliasName'', Query=''{"match": { "comment" : "TV" } }'') -- get count of documents in MyIndex where comment field contains word "TV"'
EXEC (@MyQuery) AT [LS_TO_ELASTICSEARCH_IN_GATEWAY]