diff --git a/docs/sql/endpoints.md b/docs/sql/endpoints.md new file mode 100644 index 00000000..6e11f519 --- /dev/null +++ b/docs/sql/endpoints.md @@ -0,0 +1,119 @@ +--- +layout: default +title: Endpoints +parent: SQL +nav_order: 4 +--- + + +# Endpoints + +--- + +#### Table of contents +- TOC +{:toc} + + +--- + +## Introduction + +To send query request to SQL plugin, you can either use a request +parameter in HTTP GET or request body by HTTP POST request. POST request +is recommended because it doesn't have length limitation and allows for +other parameters passed to plugin for other functionality such as +prepared statement. And also the explain endpoint is used very often for +query translation and troubleshooting. + +## GET + +### Description + +You can send HTTP GET request with your query embedded in URL parameter. + +### Example + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X GET localhost:9200/_opendistro/_sql?sql=SELECT * FROM accounts +``` + +## POST + +### Description + +You can also send HTTP POST request with your query in request body. + +### Example + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{ + "query" : "SELECT * FROM accounts" + }' +``` + +## Explain + +### Description + +To translate your query, send it to explain endpoint. The explain output +is Elasticsearch domain specific language (DSL) in JSON format. You can +just copy and paste it to your console to run it against Elasticsearch +directly. + +### Example + +Explain query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql/_explain -d '{ + "query" : "SELECT firstname, lastname FROM accounts WHERE age > 20" + }' +``` + +Explain: + +```json + { + "from" : 0, + "size" : 200, + "query" : { + "bool" : { + "filter" : [ + { + "bool" : { + "must" : [ + { + "range" : { + "age" : { + "from" : 20, + "to" : null, + "include_lower" : false, + "include_upper" : true, + "boost" : 1.0 + } + } + } + ], + "adjust_pure_negative" : true, + "boost" : 1.0 + } + } + ], + "adjust_pure_negative" : true, + "boost" : 1.0 + } + }, + "_source" : { + "includes" : [ + "firstname", + "lastname" + ], + "excludes" : [ ] + } + } +``` diff --git a/docs/sql/monitoring.md b/docs/sql/monitoring.md new file mode 100644 index 00000000..3c6fb599 --- /dev/null +++ b/docs/sql/monitoring.md @@ -0,0 +1,61 @@ +--- +layout: default +title: Plugin Monitoring +parent: SQL +nav_order: 7 +--- + +# Plugin Monitoring + +--- + +#### Table of contents +- TOC +{:toc} + + +--- + +## Introduction + +By a stats endpoint, you are able to collect metrics for the plugin +within the interval. Note that only node level statistics collecting is +implemented for now. In other words, you only get the metrics for the +node you're accessing. Cluster level statistics have yet to be +implemented. + +## Node Stats + +### Description + +The meaning of fields in the response is as follows: + +| Field name| Description| +| ------------------------- | ------------------------------------------------------------- | +| request_total| Total count of request| +| request_count| Total count of request within the interval| +|failed_request_count_syserr|Count of failed request due to system error within the interval| +|failed_request_count_cuserr| Count of failed request due to bad request within the interval| +| failed_request_count_cb| Indicate if plugin is being circuit broken within the interval| + + +### Example + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X GET localhost:9200/_opendistro/_sql/stats +``` + +Result set: + +```json + { + "failed_request_count_cb" : 0, + "failed_request_count_cuserr" : 0, + "circuit_breaker" : 0, + "request_total" : 0, + "request_count" : 0, + "failed_request_count_syserr" : 0 + } +``` diff --git a/docs/sql/protocol.md b/docs/sql/protocol.md new file mode 100644 index 00000000..0f49c414 --- /dev/null +++ b/docs/sql/protocol.md @@ -0,0 +1,353 @@ +--- +layout: default +title: Protocol +parent: SQL +nav_order: 5 +--- + +# Protocol + +--- + +#### Table of contents +- TOC +{:toc} + + +--- + +## Introduction + +For the protocol, SQL plugin provides multiple response formats for +different purposes while the request format is same for all. Among them +JDBC format is widely used because it provides schema information and +more functionality such as pagination. Besides JDBC driver, various +clients can benefit from the detailed and well formatted response. + +## Request Format + +### Description + +The body of HTTP POST request can take a few more other fields with SQL +query. + +### Example 1 + +Use `filter` to add more conditions to +Elasticsearch DSL directly. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{ + "query" : "SELECT firstname, lastname, balance FROM accounts", + "filter" : { + "range" : { + "balance" : { + "lt" : 10000 + } + } + } + }' +``` + +Explain: + +```json + { + "from" : 0, + "size" : 200, + "query" : { + "bool" : { + "filter" : [ + { + "bool" : { + "filter" : [ + { + "range" : { + "balance" : { + "from" : null, + "to" : 10000, + "include_lower" : true, + "include_upper" : false, + "boost" : 1.0 + } + } + } + ], + "adjust_pure_negative" : true, + "boost" : 1.0 + } + } + ], + "adjust_pure_negative" : true, + "boost" : 1.0 + } + }, + "_source" : { + "includes" : [ + "firstname", + "lastname", + "balance" + ], + "excludes" : [ ] + } + } +``` + +### Example 2 + +Use `parameters` for actual parameter value +in prepared SQL query. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{ + "query" : "SELECT * FROM accounts WHERE age = ?", + "parameters" : [ + { + "type" : "integer", + "value" : 30 + } + ] + }' +``` + +Explain: + +```json + { + "from" : 0, + "size" : 200, + "query" : { + "bool" : { + "filter" : [ + { + "bool" : { + "must" : [ + { + "term" : { + "age" : { + "value" : 30, + "boost" : 1.0 + } + } + } + ], + "adjust_pure_negative" : true, + "boost" : 1.0 + } + } + ], + "adjust_pure_negative" : true, + "boost" : 1.0 + } + } + } +``` + +## Elasticsearch DSL + +### Description + +By default the plugin returns original response from Elasticsearch in +JSON. Because this is the native response from Elasticsearch, extra +efforts are needed to parse and interpret it. + +### Example + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{ + "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2" + }' +``` + +Result set: + +```json + { + "_shards" : { + "total" : 5, + "failed" : 0, + "successful" : 5, + "skipped" : 0 + }, + "hits" : { + "hits" : [ + { + "_index" : "accounts", + "_type" : "account", + "_source" : { + "firstname" : "Nanette", + "age" : 28, + "lastname" : "Bates" + }, + "_id" : "13", + "sort" : [ + 28 + ], + "_score" : null + }, + { + "_index" : "accounts", + "_type" : "account", + "_source" : { + "firstname" : "Amber", + "age" : 32, + "lastname" : "Duke" + }, + "_id" : "1", + "sort" : [ + 32 + ], + "_score" : null + } + ], + "total" : { + "value" : 4, + "relation" : "eq" + }, + "max_score" : null + }, + "took" : 100, + "timed_out" : false + } +``` + +## JDBC Format + +### Description + +JDBC format is provided for JDBC driver and client side that needs both +schema and result set well formatted. + +### Example 1 + +Here is an example for normal response. The +`schema` includes field name and its type +and `datarows` includes the result set. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql?format=jdbc -d '{ + "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age LIMIT 2" + }' +``` + +Result set: + +```json + { + "schema" : [ + { + "name" : "firstname", + "type" : "text" + }, + { + "name" : "lastname", + "type" : "text" + }, + { + "name" : "age", + "type" : "long" + } + ], + "total" : 4, + "datarows" : [ + [ + "Nanette", + "Bates", + 28 + ], + [ + "Amber", + "Duke", + 32 + ] + ], + "size" : 2, + "status" : 200 + } +``` + +### Example 2 + +If any error occurred, error message and the cause will be returned +instead. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql?format=jdbc -d '{ + "query" : "SELECT unknown FROM accounts" + }' +``` + +Result set: + +```json + { + "error" : { + "reason" : "Invalid SQL query", + "details" : "Field [unknown] cannot be found or used here.", + "type" : "SemanticAnalysisException" + }, + "status" : 400 + } +``` + +## CSV Format + +### Description + +You can also use CSV format to download result set as CSV. + +### Example + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql?format=csv -d '{ + "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age" + }' +``` + +Result set: + +```csv + firstname,lastname,age + Nanette,Bates,28 + Amber,Duke,32 + Dale,Adams,33 + Hattie,Bond,36 +``` + +## Raw Format + +### Description + +Additionally raw format can be used to pipe the result to other command +line tool for post processing. + +### Example + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql?format=raw -d '{ + "query" : "SELECT firstname, lastname, age FROM accounts ORDER BY age" + }' +``` + +Result set: + +``` + Nanette|Bates|28 + Amber|Duke|32 + Dale|Adams|33 + Hattie|Bond|36 +``` diff --git a/docs/sql/settings.md b/docs/sql/settings.md new file mode 100644 index 00000000..7eef6735 --- /dev/null +++ b/docs/sql/settings.md @@ -0,0 +1,297 @@ +--- +layout: default +title: Plugin Settings +parent: SQL +nav_order: 6 +--- + +# Plugin Settings + +--- + +#### Table of contents +- TOC +{:toc} + + +--- + +## Introduction + +When Elasticsearch bootstraps, SQL plugin will register a few settings +in Elasticsearch cluster settings. Most of the settings are able to +change dynamically so you can control the behavior of SQL plugin without +need to bounce your cluster. + +## opendistro.sql.enabled + +### Description + +You can disable SQL plugin to reject all coming requests. + +1. The default value is true. +2. This setting is node scope. +3. This setting can be updated dynamically. + +### Example 1 + +You can update the setting with a new value like this. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{ + "transient" : { + "opendistro.sql.enabled" : false + } + }' +``` + +Result set: + +```json + { + "acknowledged" : true, + "persistent" : { }, + "transient" : { + "opendistro" : { + "sql" : { + "enabled" : "false" + } + } + } + } +``` + +### Example 2 + +Query result after the setting updated is like: + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{ + "query" : "SELECT * FROM accounts" + }' +``` + +Result set: + +```json + { + "error" : { + "reason" : "Invalid SQL query", + "details" : "Either opendistro.sql.enabled or rest.action.multi.allow_explicit_index setting is false", + "type" : "SQLFeatureDisabledException" + }, + "status" : 400 + } +``` + +## opendistro.sql.query.slowlog + +### Description + +You can configure the time limit (seconds) for slow query which would be +logged as 'Slow query: elapsed=xxx (ms)' in elasticsearch.log. + +1. The default value is 2. +2. This setting is node scope. +3. This setting can be updated dynamically. + +### Example + +You can update the setting with a new value like this. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{ + "transient" : { + "opendistro.sql.query.slowlog" : 10 + } + }' +``` + +Result set: + +```json + { + "acknowledged" : true, + "persistent" : { }, + "transient" : { + "opendistro" : { + "sql" : { + "query" : { + "slowlog" : "10" + } + } + } + } + } +``` + +## opendistro.sql.query.analysis.enabled + +### Description + +You can disable query analyzer to bypass strict syntactic and semantic +analysis. + +1. The default value is true. +2. This setting is node scope. +3. This setting can be updated dynamically. + +### Example + +You can update the setting with a new value like this. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{ + "transient" : { + "opendistro.sql.query.analysis.enabled" : false + } + }' +``` + +Result set: + +```json + { + "acknowledged" : true, + "persistent" : { }, + "transient" : { + "opendistro" : { + "sql" : { + "query" : { + "analysis" : { + "enabled" : "false" + } + } + } + } + } + } +``` + +## opendistro.sql.query.analysis.semantic.suggestion + +### Description + +You can enable query analyzer to suggest correct field names for quick +fix. + +1. The default value is false. +2. This setting is node scope. +3. This setting can be updated dynamically. + +### Example 1 + +You can update the setting with a new value like this. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{ + "transient" : { + "opendistro.sql.query.analysis.semantic.suggestion" : true + } + }' +``` + +Result set: + +```json + { + "acknowledged" : true, + "persistent" : { }, + "transient" : { + "opendistro" : { + "sql" : { + "query" : { + "analysis" : { + "semantic" : { + "suggestion" : "true" + } + } + } + } + } + } + } +``` + +### Example 2 + +Query result after the setting updated is like: + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X POST localhost:9200/_opendistro/_sql -d '{ + "query" : "SELECT first FROM accounts" + }' +``` + +Result set: + +```json + { + "error" : { + "reason" : "Invalid SQL query", + "details" : "Field [first] cannot be found or used here. Did you mean [firstname]?", + "type" : "SemanticAnalysisException" + }, + "status" : 400 + } +``` + +## opendistro.sql.query.analysis.semantic.threshold + +### Description + +Because query analysis needs to build semantic context in memory, index +with large number of field would be skipped. You can update it to apply +analysis to smaller or larger index as needed. + +1. The default value is 200. +2. This setting is node scope. +3. This setting can be updated dynamically. + +### Example + +You can update the setting with a new value like this. + +SQL query: + +```console + >> curl -H 'Content-Type: application/json' -X PUT localhost:9200/_cluster/settings -d '{ + "transient" : { + "opendistro.sql.query.analysis.semantic.threshold" : 50 + } + }' +``` + +Result set: + +```json + { + "acknowledged" : true, + "persistent" : { }, + "transient" : { + "opendistro" : { + "sql" : { + "query" : { + "analysis" : { + "semantic" : { + "threshold" : "50" + } + } + } + } + } + } + } +```