TODO: 内容待完善
Elasticsearch
版本: 7.17.4
一、Elasticsearch语法记录
#! 通用查询
GET _search
{
"query": {
"match_all": {}
}
}
#! 关键字分析这个版本有点问题,很多用不了是什么原因
GET _analyze
{
"analyzer": "standard",
"text": ["中国福建沙县"]
}
#! 添加一条数据 自动创建索引(index)叫megacorp (对应MySQL的数据库),employee 等同于数据库的表名,2是指这条数据的id号 ,注意put会进行覆盖,没有设置id 会有全局唯一id自动生成
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
#! 查询名称为 megacorp的索引记录
GET /megacorp/_search
#! 往索引website引批量插入,该index(数据库)的type(表)名叫log
POST /website/_bulk
{ "index": { "_type": "log" }}
{ "event": "User logged in" }
GET /website/_search
DELETE /website
#!批量插入 往索引website引批量插入,该index(数据库)的type(表)名叫log
POST /_bulk
{ "delete": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "create": { "_index": "website", "_type": "blog", "_id": "123" }}
{ "title": "My first blog post" }
{ "index": { "_index": "website", "_type": "blog" }}
{ "title": "My second blog post" }
{ "update": { "_index": "website", "_type": "blog", "_id": "123"} }
{ "doc" : {"title" : "My updated blog post"} }
#! 创建索引 tes2,表名默认是_doc??
PUT /test2
{
"mappings": {
"properties": {
"name":{
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
GET test2/_search
POST /test2/_doc
{
"type":"",
"age":15
}
#! 创建索引test3,类型(表名是_doc) id 是1,插入一条数据
PUT /test3/_doc/1
{
"name":"吕起凤",
"age":24,
"birth":"1998-10-10"
}
GET test3
#! GET _cat 会有很多信息冒出来
GET _cat
#! 下面是创建很多内容进行一些测试
PUT /test3/_doc/3
{
"name":"吕起凤333",
"age":21,
"birthday":"1999-10-10"
}
PUT /test3/_doc/4
{
"name":"陈翔六点半之-拳王妈妈",
"age":12,
"birthday":"2022-09-03"
}
PUT /test3/_doc/5
{
"name":"陈翔六点半之-废话少说",
"age":31,
"birthday":"1998-10-10"
}
PUT /test3/_doc/6
{
"name":"陈翔六点半之-重楼梦",
"age":14,
"birthday":"2020-10-20"
}
PUT /test4/_doc/1
{
"name":"吕起凤333",
"age":21,
"birthday":"1999-10-10",
"desc":"这是test4的第一条数据"
}
#! 这里会报错 Invalid type: expecting [_doc] but got [aaa]===> 无效类型:期望 [_doc] 但得到 [aaa]
PUT /test3/aaa/1
{
"name":"aaa",
"age":21,
"birthday":"1999-10-10",
"desc":"这是test3 aaa 的第一条数据"
}
#! 这样是获取索引test3的具体数据
GET test3
#! 这样是获取索引test3里的类型为_doc里的id为1的数据
GET /test3/_doc/1/
#! post进行修改,使用 _update,和 "doc"进行包裹,防止覆盖和版本号+1,不会丢失字段
POST /test3/_doc/1/_update
{
"doc":{
"name":"post修改,version不会加一,起凤",
"age":2
}
}
#! 删除 DELETE
GET /test1
GET /test1/_doc/1
DELETE
#! 条件查询 的方式?=filed:value
GET /test3/_doc/_search?q=name:post修改
GET /test3/_doc/_search?q=name:吕起凤111
GET /test3/_doc/2
GET _search
{
"query":{
"match": {
"name": "起凤"
}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
#! 匹配查询+字段过滤+排序+分页
GET _search
{
"query":{
"match": {
"name": "起凤"
}
},
"_source": ["name","age"],
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
#! 多条件查询 bool ,must等同于 and
GET /test3
GET /test3/_doc/_search
{
"query":{
"bool":{
"must": [
{
"match":{
"name":"起凤"
}
},
{
"match":{
"age":2
}
}
]
}
}
}
#! 多条件查询 bool ,should 等同于 or match 不支持多个字段查询,只支持单个字段的查询
GET /test3/_doc/_search
{
"query":{
"bool":{
"should":[
{
"match":{
"name":"post修改"
}
},
{
"match":{
"age":27
}
}]
}
}
}
#! 多条件查询 bool ,must_not 等同于 not(...and...)
GET /test3/_doc/_search
{
"query":{
"bool":{
"must_not":[
{
"match":{
"name":"post"
}
},
{
"match":{
"age":21
}
}
]
}
}
}
#! 全表查询
GET /test3/_doc/_search
{}
#! 多条件查询 bool ,filter 过滤 是否可以用来替代掉 must,还有其它什么作用吗????
#! must,should,must_not,filter 是同一层级别,可以多次同时出现
GET /test3/_doc/_search
{
"query":{
"bool":{
"filter":[
{
"range":{
"age":{
"gte":2,
"lte":25
}
}
},
{
"match":{
"name":"333"
}
}
]
}
}
}
#! 单词的话分词器不会进行拆分
#!会使用分词器解析(先分析文档,然后进行查询)可以通过空格分开查询
GET /test3/_doc/_search
{
"query":{
"match":{
"name":"post 梦 废话"
}
}
}
#! 精确查询 term 直接通过 倒排索引 指定词条查询???
#! 适合查询 number、date、keyword ,不适合text??
GET /test3/_doc/_search
{
"query":{
"term":{
"age":21
}
}
}
#! 创建 keyword_test(索引)用于测试 text 和 keyworld的类型区别
PUT /keyword_test
{
"mappings": {
"properties": {
"desc":{
"type": "text"
},
"short_name":{
"type":"keyword"
}
}
}
}
PUT /keyword_test/_doc/1
{
"desc":"沙县小吃美食真美味!",
"short_name":"沙县小吃"
}
PUT /keyword_test/_doc/2
{
"desc":"沙县的旅游怎么说",
"short_name":"旅游来沙县吧哈哈"
}
PUT /keyword_test/_doc/3
{
"desc":"福建省的沙县全国有名!",
"short_name":"附件的莆田很坑沙县很有名!分店开遍全球"
}
PUT /keyword_test/_doc/4
{
"desc":"我少一个字段!"
}
GET /keyword_test/_search
#! match 会启用分词器,字段类型如果是text的话,含有沙县这个词语基本都可能被查出来
GET /keyword_test/_doc/_search
{
"query":{
"match":{
"desc":"沙县"
}
}
}
#! 字段类型如果是 keyword 的话,不会走分词查询,基本就不会出来,完全匹配模式
GET /keyword_test/_doc/_search
{
"query":{
"match":{
"short_name":"沙县"
}
}
}
#! 分词器分析有点问题
POST _analyze?
{ "analyzer":"keyword", "text":["沙县"]}
GET _analyze
{
"analyzer": "standard", "text": ["福建省的沙县全国有名"]
}
#!不顶用
GET /_analyze?
{"analyzer" : "whitespace", "text" : "床 前明 月光"}
#!不顶用
GET /tystock/_analyze?
{"analyzer" : "pinyin", "text" : "床 前明 月光"}
#!不顶用
GET _analyze
{
"analyzer":"ik_max_word",
"text": ["测试liu"]
}
#! 高亮显示 highlight 和 query 同一级别
GET /test4/_search
{
"query": {
"match": {
"name": "333"
}
},
"highlight":{
"fields": {
"name": {}
}
}
}
#! 高亮显示添加前置后置处理
GET /test3/_search
{
"query": {
"match": {
"name": "起凤"
}
},
"highlight":{
"pre_tags": "<a>",
"post_tags": "</a>",
"fields": {
"name": {}
}
}
}
#! SQL支持只读模式不能修改
POST /_xpack/sql?format=txt
{
"query":"select * from test3"
}
POST /_xpack/sql?format=txt
{
"query":"select * from test3 order by age asc"
}
GET _cat