<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-Hans-CN">
	<id>http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B</id>
	<title>ElasticSearch SQL：简介 - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.eijux.com/index.php?action=history&amp;feed=atom&amp;title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B"/>
	<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B&amp;action=history"/>
	<updated>2026-05-15T14:20:41Z</updated>
	<subtitle>本wiki上该页面的版本历史</subtitle>
	<generator>MediaWiki 1.38.2</generator>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B&amp;diff=6471&amp;oldid=prev</id>
		<title>2023年3月31日 (五) 13:51 Eijux</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B&amp;diff=6471&amp;oldid=prev"/>
		<updated>2023-03-31T13:51:15Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;zh-Hans-CN&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;←上一版本&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;2023年3月31日 (五) 21:51的版本&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;第1行：&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;第1行：&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;ElasticSearch&lt;/del&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;[[category:&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;ElasticSearch教程&lt;/ins&gt;]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 关于 ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== 关于 ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
	<entry>
		<id>http://wiki.eijux.com/index.php?title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B&amp;diff=3693&amp;oldid=prev</id>
		<title>Eijux：​建立内容为“category:ElasticSearch  == 关于 == 除了使用 RESTful API 查询 Elasticsearch 索引数据，也可以使用 SQL 语句查询索引数据。   * 需要…”的新页面</title>
		<link rel="alternate" type="text/html" href="http://wiki.eijux.com/index.php?title=ElasticSearch_SQL%EF%BC%9A%E7%AE%80%E4%BB%8B&amp;diff=3693&amp;oldid=prev"/>
		<updated>2021-05-21T11:25:57Z</updated>

		<summary type="html">&lt;p&gt;建立内容为“&lt;a href=&quot;/%E5%88%86%E7%B1%BB:ElasticSearch&quot; title=&quot;分类:ElasticSearch&quot;&gt;category:ElasticSearch&lt;/a&gt;  == 关于 == 除了使用 RESTful API 查询 Elasticsearch 索引数据，也可以使用 SQL 语句查询索引数据。   * 需要…”的新页面&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[category:ElasticSearch]]&lt;br /&gt;
&lt;br /&gt;
== 关于 ==&lt;br /&gt;
除了使用 RESTful API 查询 Elasticsearch 索引数据，也可以使用 SQL 语句查询索引数据。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 需要安装 '''X-Pack''' 扩展组件，才可以让 ES 支持 SQL 查询。（新版本的 ES 默认已经安装了 X-Pack）&lt;br /&gt;
&lt;br /&gt;
== SQL 和 ES ==&lt;br /&gt;
SQL 和 ES 的概念映射关系：&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! SQL !! Elasticsearch !! 描述 &lt;br /&gt;
|-&lt;br /&gt;
| column || field || SQL 中的列等价于 ES 中的字段&lt;br /&gt;
|-&lt;br /&gt;
| row || document || SQL 中的一行数据等价于 ES 的一条文档&lt;br /&gt;
|-&lt;br /&gt;
| table || index || SQL 的表跟 ES 中的索引名对应&lt;br /&gt;
|-&lt;br /&gt;
| schema || 无 || SQL 中的 schema 通常指的就是数据库，在 ES 中没有这个概念，可以简单的认为 ES 中一个 index 就是一个数据库&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SQL 语法：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SQL&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
SELECT select_expr [, ...]&lt;br /&gt;
[ FROM table_name ]&lt;br /&gt;
[ WHERE condition ]&lt;br /&gt;
[ GROUP BY grouping_element [, ...] ]&lt;br /&gt;
[ HAVING condition]&lt;br /&gt;
[ ORDER BY expression [ ASC | DESC ] [, ...] ]&lt;br /&gt;
[ LIMIT [ count ] ]&lt;br /&gt;
[ PIVOT ( aggregation_expr FOR column IN ( value [ [ AS ] alias ] [, ...] ) ) ]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
* SQL 语法结构跟 MySQL 类似都是 SQL 标准语法结构，区别就是 SQL 标准的支持程度。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== ES支持的SQL命令 ==&lt;br /&gt;
&lt;br /&gt;
=== SHOW COLUMNS ===&lt;br /&gt;
'''查询 ES 索引的字段和类型'''&lt;br /&gt;
&lt;br /&gt;
语法：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
SHOW COLUMNS [ FROM | IN ]?&lt;br /&gt;
    [table identifier | &lt;br /&gt;
    [LIKE pattern] ]    &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;SHOW COLUMNS FROM library&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
输出：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
    column     |     type      |    mapping    &lt;br /&gt;
---------------+---------------+---------------&lt;br /&gt;
author         |VARCHAR        |text           &lt;br /&gt;
author.keyword |VARCHAR        |keyword        &lt;br /&gt;
name           |VARCHAR        |text           &lt;br /&gt;
name.keyword   |VARCHAR        |keyword        &lt;br /&gt;
page_count     |BIGINT         |long           &lt;br /&gt;
release_date   |TIMESTAMP      |datetime &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SHOW TABLES ===&lt;br /&gt;
'''列出 ES 中所有索引'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;SHOW TABLES&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
输出：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
          name          |     type      |     kind      &lt;br /&gt;
------------------------+---------------+---------------&lt;br /&gt;
.apm-agent-configuration|BASE TABLE     |INDEX          &lt;br /&gt;
.kibana                 |VIEW           |ALIAS          &lt;br /&gt;
.kibana_1               |BASE TABLE     |INDEX          &lt;br /&gt;
.kibana_task_manager    |VIEW           |ALIAS          &lt;br /&gt;
.kibana_task_manager_1  |BASE TABLE     |INDEX          &lt;br /&gt;
library                 |BASE TABLE     |INDEX          &lt;br /&gt;
order                   |BASE TABLE     |INDEX                 &lt;br /&gt;
shops                   |BASE TABLE     |INDEX   &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SHOW FUNCTIONS ===&lt;br /&gt;
'''展示 ES 支持的 SQL 函数'''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;SHOW FUNCTIONS&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
输出：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
      name       |     type      &lt;br /&gt;
-----------------+---------------&lt;br /&gt;
AVG              |AGGREGATE      &lt;br /&gt;
COUNT            |AGGREGATE      &lt;br /&gt;
FIRST            |AGGREGATE      &lt;br /&gt;
FIRST_VALUE      |AGGREGATE      &lt;br /&gt;
LAST             |AGGREGATE      &lt;br /&gt;
LAST_VALUE       |AGGREGATE      &lt;br /&gt;
MAX              |AGGREGATE      &lt;br /&gt;
MIN              |AGGREGATE      &lt;br /&gt;
SUM              |AGGREGATE      &lt;br /&gt;
KURTOSIS         |AGGREGATE     &lt;br /&gt;
.....忽略.....&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ES执行SQL语句 ==&lt;br /&gt;
可以直接'''通过 REST API 执行 SQL 语句'''，语法格式如下：&lt;br /&gt;
'''&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;这里书写SQL语句&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;'''&lt;br /&gt;
其中，'''format''' 属性可以指定数据返回格式：&lt;br /&gt;
* '''txt''' 格式：返回表格形式，便于阅读；&lt;br /&gt;
* '''json''' 格式：代表返回 Json 格式，便于数据处理。&lt;br /&gt;
&lt;br /&gt;
语句说明：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
SELECT {查询字段} FROM {索引名} WHERE {查询条件}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
# 准备测试数据：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
PUT /library/_bulk?refresh&lt;br /&gt;
{&amp;quot;index&amp;quot;:{&amp;quot;_id&amp;quot;: &amp;quot;Leviathan Wakes&amp;quot;}}&lt;br /&gt;
{&amp;quot;name&amp;quot;: &amp;quot;Leviathan Wakes&amp;quot;, &amp;quot;author&amp;quot;: &amp;quot;James S.A. Corey&amp;quot;, &amp;quot;release_date&amp;quot;: &amp;quot;2011-06-02&amp;quot;, &amp;quot;page_count&amp;quot;: 561}&lt;br /&gt;
{&amp;quot;index&amp;quot;:{&amp;quot;_id&amp;quot;: &amp;quot;Hyperion&amp;quot;}}&lt;br /&gt;
{&amp;quot;name&amp;quot;: &amp;quot;Hyperion&amp;quot;, &amp;quot;author&amp;quot;: &amp;quot;Dan Simmons&amp;quot;, &amp;quot;release_date&amp;quot;: &amp;quot;1989-05-26&amp;quot;, &amp;quot;page_count&amp;quot;: 482}&lt;br /&gt;
{&amp;quot;index&amp;quot;:{&amp;quot;_id&amp;quot;: &amp;quot;Dune&amp;quot;}}&lt;br /&gt;
{&amp;quot;name&amp;quot;: &amp;quot;Dune&amp;quot;, &amp;quot;author&amp;quot;: &amp;quot;Frank Herbert&amp;quot;, &amp;quot;release_date&amp;quot;: &amp;quot;1965-06-01&amp;quot;, &amp;quot;page_count&amp;quot;: 604}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 执行SQL语句：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;SELECT * FROM library WHERE release_date &amp;lt; '2000-01-01'&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
# 执行结果：&lt;br /&gt;
#: &amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
    author     |     name      |  page_count   |      release_date      &lt;br /&gt;
---------------+---------------+---------------+------------------------&lt;br /&gt;
Dan Simmons    |Hyperion       |482            |1989-05-26T00:00:00.000Z&lt;br /&gt;
Frank Herbert  |Dune           |604            |1965-06-01T00:00:00.000Z&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== 全文搜索 ==&lt;br /&gt;
ES SQL 语句中主要通过 '''MATCH 函数'''实现全文搜索。【SQL 中的“MATCH”函数！！！不是 ES 的“MATCH”关键字】&lt;br /&gt;
* ES SQL 语法虽然支持 like 语句，但是 like 并不是使用全文搜索算法。&lt;br /&gt;
&lt;br /&gt;
=== MATCH 函数 ===&lt;br /&gt;
语法：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
MATCH(&lt;br /&gt;
    field_exp,   &lt;br /&gt;
    constant_exp &lt;br /&gt;
    [, options]) &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
说明：&lt;br /&gt;
# '''field_exp'''：需要搜索的字段&lt;br /&gt;
# '''constant_exp'''：需要搜索的关键词&lt;br /&gt;
# '''options'''：可选参数&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：查询 library 索引中 author 字段，匹配 frank 关键词的文档&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;SELECT author, name FROM library WHERE MATCH(author, 'frank')&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
返回结果：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
    author     |     name      &lt;br /&gt;
---------------+---------------&lt;br /&gt;
Frank Herbert  |Dune  &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== SQL相关度排序 ===&lt;br /&gt;
通过 '''SCORE() 函数'''实现相关度排序。&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;JSON&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
POST /_sql?format=txt&lt;br /&gt;
{&lt;br /&gt;
    &amp;quot;query&amp;quot;: &amp;quot;SELECT author, name FROM library WHERE MATCH(author, 'frank') ORDER BY SCORE()&amp;quot;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* 也可以将相关度分数返回。&lt;br /&gt;
&lt;br /&gt;
示例：&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;SQL&amp;quot; highlight=&amp;quot;&amp;quot;&amp;gt;&lt;br /&gt;
SELECT SCORE(), * FROM library WHERE MATCH(name, 'dune') ORDER BY SCORE() DESC;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eijux</name></author>
	</entry>
</feed>