Execute SQL
# SQL statement support
Some users expect to use the function of RestLowLevelClient to execute existing SQL statements, achieving the effect similar to that of executing DSL on plug-ins such as xpack or es-head. So we encapsulate this by executing SQL-API out of the box.
API
//The index name of executing static SQL statement is subject to the name specified in the where condition in SQL.
String executeSQL(String dsl);
1
2
3
2
3
# Use
@Test
public void testSQL() {
//Note that from in sql is followed by the index name to be queried, or it can be an index alias (the effect is the same). Because the index name may change, I use the alias ee_default_alias to query here.
String sql = "select count(*) from ee_default_alias where star_num > 0";
String jsonResult = documentMapper.executeSQL(sql);
//Note that after execution, it is returned in JSON format, which is parsed by the user as required.
System.out.println(jsonResult);
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
提示
Although ES provides support for SQL, the function is relatively simple, and everyone who has used it can understand it. This function is only to meet the needs of a few users, just like MP has supported simple SQL queries out of the box, and users rarely go there again. Hand-written SQL, not to mention its SQL support function is too simple and poor expansibility, so it is not recommended.
Help us improve this document (opens new window)
Last update: 2024/03/29