使用 Elasticsearch 查询 DSL 进行过滤
编辑使用 Elasticsearch 查询 DSL 进行过滤
编辑可以通过在 filter 参数中指定查询,使用标准的 Elasticsearch 查询 DSL 来过滤 SQL 将在其上运行的结果。
resp = client.sql.query( format="txt", query="SELECT * FROM library ORDER BY page_count DESC", filter={ "range": { "page_count": { "gte": 100, "lte": 200 } } }, fetch_size=5, ) print(resp)
response = client.sql.query( format: 'txt', body: { query: 'SELECT * FROM library ORDER BY page_count DESC', filter: { range: { page_count: { gte: 100, lte: 200 } } }, fetch_size: 5 } ) puts response
const response = await client.sql.query({ format: "txt", query: "SELECT * FROM library ORDER BY page_count DESC", filter: { range: { page_count: { gte: 100, lte: 200, }, }, }, fetch_size: 5, }); console.log(response);
POST /_sql?format=txt { "query": "SELECT * FROM library ORDER BY page_count DESC", "filter": { "range": { "page_count": { "gte" : 100, "lte" : 200 } } }, "fetch_size": 5 }
返回结果为:
author | name | page_count | release_date ---------------+------------------------------------+---------------+------------------------ Douglas Adams |The Hitchhiker's Guide to the Galaxy|180 |1979-10-12T00:00:00.000Z
标准查询 DSL 过滤的一个有用且不太明显的用法是通过特定的路由键搜索文档。 由于 Elasticsearch SQL 不支持 routing
参数,因此可以为 _routing
字段指定 terms
过滤器来替代
resp = client.sql.query( format="txt", query="SELECT * FROM library", filter={ "terms": { "_routing": [ "abc" ] } }, ) print(resp)
response = client.sql.query( format: 'txt', body: { query: 'SELECT * FROM library', filter: { terms: { _routing: [ 'abc' ] } } } ) puts response
const response = await client.sql.query({ format: "txt", query: "SELECT * FROM library", filter: { terms: { _routing: ["abc"], }, }, }); console.log(response);
POST /_sql?format=txt { "query": "SELECT * FROM library", "filter": { "terms": { "_routing": ["abc"] } } }