Count documents in aliases
Counts documents across one or more aliases (and, if needed, a mix of indexes and aliases) using the Elasticsearch query DSL. Use the Index parameter to list the aliases and indexes to include, and optionally pass a query expression to count only matching documents; see the Elasticsearch query DSL documentation for details.
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='MyIndexOrAlias1,MyIndexOrAlias2') -- get count of documents in MyIndex1 and MyIndex2
SELECT * FROM count WITH(Index='MyIndex', Query='{"match": { "comment" : "TV" } }') -- get count of documents in index named MyIndex where comment field contains word "TV"
SELECT * FROM count WITH(Index='MyAlias', Query='{"match": { "comment" : "TV" } }') -- get count of documents in alias named MyAlias 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=''MyIndexOrAlias1,MyIndexOrAlias2'') -- get count of documents in MyIndex1 and MyIndex2
SELECT * FROM count WITH(Index=''MyIndex'', Query=''{"match": { "comment" : "TV" } }'') -- get count of documents in index named MyIndex where comment field contains word "TV"
SELECT * FROM count WITH(Index=''MyAlias'', Query=''{"match": { "comment" : "TV" } }'') -- get count of documents in alias named MyAlias 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=''MyIndexOrAlias1,MyIndexOrAlias2'') -- get count of documents in MyIndex1 and MyIndex2
SELECT * FROM count WITH(Index=''MyIndex'', Query=''{"match": { "comment" : "TV" } }'') -- get count of documents in index named MyIndex where comment field contains word "TV"
SELECT * FROM count WITH(Index=''MyAlias'', Query=''{"match": { "comment" : "TV" } }'') -- get count of documents in alias named MyAlias where comment field contains word "TV"'
EXEC (@MyQuery) AT [LS_TO_ELASTICSEARCH_IN_GATEWAY]