Use SQL statement to query the index data Elasticsearch

Elasticsearch query language is the official Query DSL, after all, there is truth exists, there is reasonable. As a SQL database query language syntax is simple, easy to write and most of the server-side programmers are familiar with it and a clear understanding of the wording. But as a new ES Meng who, even if he was already a programming industry old traveler, but if he was not familiar ES, so if he wants to use the company was set for the ES service, he must first learn Query DSL, learning cost is also a factor affecting the progress of technology development and stability is not high. But if ES SQL query support, then maybe even if he was working a year or two of the students, he did not understand the concept of ES complex, he can be a good use of ES and successfully participate in the development of the team, after all, who SQL not write it?

Elasticsearch-SQL does not belong Elasticsearch official, it is NLPChina (Chinese natural language processing open source) open an ES plug-in, the main function is to query the ES via SQL, in fact, the bottom is by explaining SQL, SQL conversion to DSL grammar and then query via DSL.

Github Address: https://github.com/NLPchina/elasticsearch-sql

Install plug

After 5.0 (including 6.x) installation of:elasticsearch-plugin install

Because elasticsearch using version 6.2.4, you must use the same version of the plug-in, you can view the specific URLs on github

./bin/elasticsearch-plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/6.2.4.0/elasticsearch-sql-6.2.4.0.zip

If we are not successful installation, we can directly download Elasticsearch-SQL plug-in compression package, then extract, after the completion rename folder sql, put the installation path of the ES pluginsdirectory, for example: ..\elasticsearch-6.2.4\plugins\sql.

After you do this, you need to restart the server Elasticsearch, otherwise it will error: Invalid index name [sql], must not start with '']; ","status":400}.

Visual front end interface

Elasticsearch-SQL plug-in provides a visual interface, allowing you to execute SQL queries

In elasticsearch 5.x / 6.x, which requires the installation node.js and download and unzip Site , and then start the web front end like this:

wget https://github.com/NLPchina/elasticsearch-sql/releases/download/5.4.1.0/es-sql-site-standalone.zip
unzip es-sql-site-standalone.zip
cd site-server
# 需要事先安装好nodejs
npm install express --save 
node node-server.js  # 可以通过查看下面的后台运行命令使用

# 端口被占用的话修改端口号
# 后台启动

# 浏览器访问:http://ip:8080进行访问

# 右上角为es的地址和端口,最后的/不能省略

problem:

  1. Port number is occupied

The default port is 8080, needs to be modified, then you can change the site-server/site_configuration.jsonfile

{
   "port":8880
}
  1. If the X-Pack, it also relates to issues such as user name and password. Refer to the following configuration:
    modify the ES configuration file /etc/elasticsearch/elasticsearch.yml, add the following lines:
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-origin: "/.*/"
http.cors.allow-headers: WWW-Authenticate,X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization

Restart the cluster
to access the site elasticsearch-sql, and bring the user name, password, ES address as a parameter, for example: HTTP: // es-SQL-Site: 8080 / username = the Hello & password = o11eh & base_uri = HTTP:? // es: 9200

Is not configured properly address the ES:

Error: "<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n<meta charset=\"utf-8\">\n<title>Error</title>\n</head>\n<body>\n<pre>Cannot POST /_sql</pre>\n</body>\n</html>\n"
  1. elasticsearch.yml not configured correctly http.cors:
报错提示:Error: Error occured! response is not avalible.

解决办法:
找到config文件夹下的elasticsearch.yml,在文件的末尾添加如下内容:
http.cors.enabled: true
http.cors.allow-origin: "*"
  1. wrong user name or password:
Error: {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication token for REST request [/_sql]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication token for REST request [/_sql]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401} 
  1. Connection top right corner of the web page address text box es node address inquiry

The address of the last got to be /the job

  1. nodejs program running in the background
# 安装forever
npm install -g forever
# 启动
forever start node-server.js    //node-server.js 是你要启动的js文件
# 查看
forever list    //会列出forever正在运行的服务脚本
# 停止
forever stop index.js   //停止服务运行
# 获取帮助信息
forever --help

Use

查询方式1:
直接在浏览器中输入sql查询语句
http://192.168.0.254:9200/_sql?sql=SELECT * FROM rforder-idx limit 6

查询方式2:
使用web界面,输入sql查询语句
http://192.168.0.254:8880/

查询方式3:
使用谷歌浏览器插件
https://github.com/shi-yuan/elasticsearch-sql-site-chrome

Guess you like

Origin www.cnblogs.com/sanduzxcvbnm/p/11583759.html