官网:
https://www.elastic.co/guide/en/enterprise-search-clients/index.html
1 Rest风格说明
一种软件架构风格,而不是标准,知识提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更改有层次,易于实现缓存的机制。
method |
url地址 |
描述 |
PUT |
localhost:9200/索引名称/类型名称/文档id |
创建文档(指定文档id) |
POST |
localhost:9200/索引名称/类型名称 |
修改文档(随机文档id) |
POST |
localhost:9200/索引名称/类型名称/文档id/_update |
修改文档 |
DELETE |
localhost:9200/索引名称/类型名称/文档id |
删除文档 |
GET |
localhost:9200/索引名称/类型名称/文档id |
查询文档通过文档id |
POST |
localhost:9200/索引名称/类型名称/_search |
查询所有数据 |
1.1 创建一个索引
localhost:9200/索引名称/类型名称/文档id
PUT /test1/type1/1
{
"name": "有勇气的牛排",
"age": 18
}

1.1.1 字段类型:
字符串类型:text、keyword
数值类型:long、integer、short、byte、double、float、half_float、scaled_float
日期类型:date
布尔类型:boolean
二进制类型:binary
# 创建规则 索引+字段
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
1.1.2 获取索引信息
# 获取当前索引信息
GET test2

1.1.3 查看索引默认信息
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}

GET test3

1.2 其他 _cat
# _cat 查看默认配置
# 获取数据库健康值
GET _cat/health
1.2 修改
1.2.1 PUT覆盖
重复执行
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
1.2.2 POST更新
# 创建/更新
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
# 更新
POST /test3/_doc/1/_update
{
"doc":{
"name": "大哥"
}
}
1.3 删除索引
更具路径判断删除索引还是文档
# 删除指定索引
DELETE test1
1.3 删除指定文档
DELETE users/_doc/170
2 文档基本操作
2.1 基本操作
2.1.1 添加数据
PUT /vitcloud/user/2
{
"name":"大漂亮",
"age": 17,
"desc": "女主",
"tags":["旅游","轻音乐","弹琴"]
}
2.1.2 查数据
GET /vitcloud/user/2
# 精确搜索
GET /vitcloud/user/_search?q=name:黑客
2.1.3 更新数据 PUT
如果不传值,会被覆盖
PUT /vitcloud/user/2
{
"name":"大漂亮2号",
"age": 17,
"desc": "女主",
"tags":["旅游","轻音乐","弹琴"]
}
2.1.4 更新数据 POST (推荐)
POST /vitcloud/user/1/_update
{
"doc":{
"name":"大漂亮2号"
}
}
2.2 复杂查询
2.2.1 模糊查询
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
}
}
2.2.2 _source: 结果过滤
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"_source":["name","desc"]
}
2.2.3 分组查询
GET /blog_rate/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "uid"
}
}
}
}
2.2.4 sort: 排序
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"sort": [
{
"age":{
"order": "desc"
}
}
]
}
2.2.5 分页
/search/{current}/{pagesize}
from: 从第几条开始
size: 显示多少条
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"_source":["name","desc"],
"from": 0,
"size": 2
}
2.2.6 bool 多条件精确查询
must:都满足
must_not:不满足的
should:满足一个即可
GET /vitcloud/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "有勇气"
}
},{
"match": {
"tags": "旅游"
}
}
]
}
}
}
2.2.7 范围过滤
gt
: 大于
lt
: 小于
gte
: 大于等于
lte
: 小于等于
GET /vitcloud/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "有勇气"
}
},
"filter":{
"range":{
"age":{
"gt":10,
"lt":30
}
}
}
]
}
}
}
2.2.8 匹配多个 条件多条件 空格隔开
GET /vitcloud/user/_search
{
"query":{
"match": {
"tags": "游 音乐"
}
}
}
2.2.9 精确查询
term查询是直接通过倒排索引指定的词条进程精确的查找
关于分词
- term :直接查询精确的
- mathc:会使用分词器解释(先分析文档,然后通过分析的文档进行查询)
keyword不会被分词
2.2.10 精确查询 多个值
# 添加数据
PUT /vitcloud/_doc/1
{
"t1": "22",
"t2": "2022-02-22"
}
PUT /vitcloud/_doc/2
{
"t1": "33",
"t2": "2022-02-23"
}
GET vitcloud/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "22"
}
},
{
"term": {
"t1": "33"
}
}
]
}
}
}
2.2.11 高亮查询
默认为em标签
GET vitcloud/user/_search
{
"query": {
"match":{
"name": "有勇气"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}
自定义标签
GET vitcloud/user/_search
{
"query": {
"match":{
"name": "有勇气"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name":{}
}
}
}
来源
狂神说:https://www.bilibili.com/video/BV17a4y1x7zq
<p>官网:<br />
<a href="https://www.elastic.co/guide/en/enterprise-search-clients/index.html" target="_blank">https://www.elastic.co/guide/en/enterprise-search-clients/index.html</a></p>
<h2><a id="1_Rest_3"></a>1 Rest风格说明</h2>
<p>一种软件架构风格,而不是标准,知识提供了一组设计原则和约束条件。它主要用于客户端和服务器交互类的软件。基于这个风格设计的软件可以更简洁,更改有层次,易于实现缓存的机制。</p>
<table>
<thead>
<tr>
<th>method</th>
<th>url地址</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>PUT</td>
<td>localhost:9200/索引名称/类型名称/文档id</td>
<td>创建文档(指定文档id)</td>
</tr>
<tr>
<td>POST</td>
<td>localhost:9200/索引名称/类型名称</td>
<td>修改文档(随机文档id)</td>
</tr>
<tr>
<td>POST</td>
<td>localhost:9200/索引名称/类型名称/文档id/_update</td>
<td>修改文档</td>
</tr>
<tr>
<td>DELETE</td>
<td>localhost:9200/索引名称/类型名称/文档id</td>
<td>删除文档</td>
</tr>
<tr>
<td>GET</td>
<td>localhost:9200/索引名称/类型名称/文档id</td>
<td>查询文档通过文档id</td>
</tr>
<tr>
<td>POST</td>
<td>localhost:9200/索引名称/类型名称/_search</td>
<td>查询所有数据</td>
</tr>
</tbody>
</table>
<h3><a id="11__14"></a>1.1 创建一个索引</h3>
<p><code>localhost:9200/索引名称/类型名称/文档id</code></p>
<pre><div class="hljs"><code class="lang-json">PUT /test1/type1/<span class="hljs-number">1</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">"name"</span><span class="hljs-punctuation">:</span> <span class="hljs-string">"有勇气的牛排"</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">"age"</span><span class="hljs-punctuation">:</span> <span class="hljs-number">18</span>
<span class="hljs-punctuation">}</span>
</code></div></pre>
<p><img src="https://static.couragesteak.com/article/608cb3090bbf7359cd97be5810c46a45.png" alt="image.png" /></p>
<h4><a id="111__27"></a>1.1.1 字段类型:</h4>
<p>字符串类型:text、keyword<br />
数值类型:long、integer、short、byte、double、float、half_float、scaled_float<br />
日期类型:date<br />
布尔类型:boolean<br />
二进制类型:binary</p>
<pre><code class="lang-powershell"># 创建规则 索引+字段
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
</code></pre>
<h4><a id="112__56"></a>1.1.2 获取索引信息</h4>
<pre><code class="lang-powershell"># 获取当前索引信息
GET test2
</code></pre>
<p><img src="https://static.couragesteak.com/article/76d05dcbce53081c2d0b0ae8efc93bcf.png" alt="image.png" /></p>
<h4><a id="113__66"></a>1.1.3 查看索引默认信息</h4>
<pre><code class="lang-powershell">PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
</code></pre>
<p><img src="https://static.couragesteak.com/article/36a1c14eb52a0e7d13d60d4940663802.png" alt="image.png" /></p>
<pre><code class="lang-powershell">GET test3
</code></pre>
<p><img src="https://static.couragesteak.com/article/d098db2523efba044be2a8c11f2e465e.png" alt="image.png" /></p>
<h4><a id="12___cat_88"></a>1.2 其他 _cat</h4>
<pre><code class="lang-powershell"># _cat 查看默认配置
# 获取数据库健康值
GET _cat/health
</code></pre>
<h3><a id="12__96"></a>1.2 修改</h3>
<h4><a id="121_PUT_97"></a>1.2.1 PUT覆盖</h4>
<p>重复执行</p>
<pre><code class="lang-powershell">PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
</code></pre>
<h4><a id="122_POST_108"></a>1.2.2 POST更新</h4>
<pre><code class="lang-powershell"># 创建/更新
PUT /test3/_doc/1
{
"name":"有勇气的牛排",
"age": 18,
"birthday": "2010-05-20"
}
# 更新
POST /test3/_doc/1/_update
{
"doc":{
"name": "大哥"
}
}
</code></pre>
<h3><a id="13__126"></a>1.3 删除索引</h3>
<p>更具路径判断删除索引还是文档</p>
<pre><code class="lang-powershell"># 删除指定索引
DELETE test1
</code></pre>
<h3><a id="13__132"></a>1.3 删除指定文档</h3>
<pre><code class="lang-powershell">DELETE users/_doc/170
</code></pre>
<h2><a id="2__137"></a>2 文档基本操作</h2>
<h3><a id="21__138"></a>2.1 基本操作</h3>
<h4><a id="211__139"></a>2.1.1 添加数据</h4>
<pre><code class="lang-powershell">
PUT /vitcloud/user/2
{
"name":"大漂亮",
"age": 17,
"desc": "女主",
"tags":["旅游","轻音乐","弹琴"]
}
</code></pre>
<h4><a id="212__150"></a>2.1.2 查数据</h4>
<pre><code class="lang-powershell">GET /vitcloud/user/2
# 精确搜索
GET /vitcloud/user/_search?q=name:黑客
</code></pre>
<h4><a id="213___PUT_157"></a>2.1.3 更新数据 PUT</h4>
<p>如果不传值,会被覆盖</p>
<pre><code class="lang-powershell">PUT /vitcloud/user/2
{
"name":"大漂亮2号",
"age": 17,
"desc": "女主",
"tags":["旅游","轻音乐","弹琴"]
}
</code></pre>
<h4><a id="214___POST__168"></a>2.1.4 更新数据 POST (推荐)</h4>
<pre><code class="lang-powershell">POST /vitcloud/user/1/_update
{
"doc":{
"name":"大漂亮2号"
}
}
</code></pre>
<h3><a id="22__179"></a>2.2 复杂查询</h3>
<h4><a id="221__180"></a>2.2.1 模糊查询</h4>
<pre><code class="lang-powershell">GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
}
}
</code></pre>
<h4><a id="222__source__191"></a>2.2.2 _source: 结果过滤</h4>
<pre><code class="lang-powershell">
GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"_source":["name","desc"]
}
</code></pre>
<h4><a id="223__204"></a>2.2.3 分组查询</h4>
<pre><code class="lang-powershell">GET /blog_rate/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "uid"
}
}
}
}
</code></pre>
<h4><a id="224_sort__220"></a>2.2.4 sort: 排序</h4>
<pre><code class="lang-powershell">GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"sort": [
{
"age":{
"order": "desc"
}
}
]
}
</code></pre>
<h4><a id="225__238"></a>2.2.5 分页</h4>
<p><code>/search/{current}/{pagesize}</code><br />
from: 从第几条开始<br />
size: 显示多少条</p>
<pre><code class="lang-powershell">GET /vitcloud/user/_search
{
"query":{
"match": {
"name": "有勇气"
}
},
"_source":["name","desc"],
"from": 0,
"size": 2
}
</code></pre>
<h4><a id="226_bool__255"></a>2.2.6 bool 多条件精确查询</h4>
<p>must:都满足<br />
must_not:不满足的<br />
should:满足一个即可</p>
<pre><code class="lang-powershell">GET /vitcloud/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "有勇气"
}
},{
"match": {
"tags": "旅游"
}
}
]
}
}
}
</code></pre>
<h4><a id="227__280"></a>2.2.7 范围过滤</h4>
<ul>
<li><code>gt</code>: <code>大于</code></li>
<li><code>lt</code>: <code>小于</code></li>
<li><code>gte</code>: <code>大于等于</code></li>
<li><code> lte</code>: <code>小于等于</code></li>
</ul>
<pre><code class="lang-powershell">GET /vitcloud/user/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name": "有勇气"
}
},
"filter":{
"range":{
"age":{
"gt":10,
"lt":30
}
}
}
]
}
}
}
</code></pre>
<h4><a id="228____309"></a>2.2.8 匹配多个 条件多条件 空格隔开</h4>
<pre><code class="lang-powershell">GET /vitcloud/user/_search
{
"query":{
"match": {
"tags": "游 音乐"
}
}
}
</code></pre>
<h4><a id="229__322"></a>2.2.9 精确查询</h4>
<p>term查询是直接通过倒排索引指定的词条进程精确的查找</p>
<p><strong>关于分词</strong></p>
<ul>
<li>term :直接查询精确的</li>
<li>mathc:会使用分词器解释(先分析文档,然后通过分析的文档进行查询)</li>
</ul>
<p>keyword不会被分词</p>
<h4><a id="2210___333"></a>2.2.10 精确查询 多个值</h4>
<pre><code class="lang-powershell"># 添加数据
PUT /vitcloud/_doc/1
{
"t1": "22",
"t2": "2022-02-22"
}
PUT /vitcloud/_doc/2
{
"t1": "33",
"t2": "2022-02-23"
}
</code></pre>
<pre><code class="lang-powershell">GET vitcloud/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "22"
}
},
{
"term": {
"t1": "33"
}
}
]
}
}
}
</code></pre>
<h4><a id="2211__371"></a>2.2.11 高亮查询</h4>
<p>默认为em标签</p>
<pre><code class="lang-powershell">GET vitcloud/user/_search
{
"query": {
"match":{
"name": "有勇气"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}
</code></pre>
<p>自定义标签</p>
<pre><code class="lang-powershell">GET vitcloud/user/_search
{
"query": {
"match":{
"name": "有勇气"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name":{}
}
}
}
</code></pre>
<p>来源<br />
狂神说:<a href="https://www.bilibili.com/video/BV17a4y1x7zq" target="_blank">https://www.bilibili.com/video/BV17a4y1x7zq</a></p>
留言