ELK 8.0.0 数据增删改查

ELK 8.0.0 数据增删改查

1 创建文档数据

PUT fcarey/_doc/1
{
  "name":"fcarey",
  "age":18,
  "city":"sz",
  "tag":["python","elk"]
}

PUT fcarey/_doc/2
{
  "name":"user01",
  "age":18,
  "city":"xm",
  "tag":["python","go"]
}

PUT fcarey/_doc/3
{
  "name":"user02",
  "age":27,
  "city":"sh",
  "tag":["go","php"]
}

PUT fcarey/_doc/4
{
  "name":"user03",
  "age":18,
  "city":"sh",
  "tag":["go","php","python and php"]
}

2 查找所创建的文档

# 查询fcarey索引中的所有文档
GET fcarey/_search

GET fcarey/_doc/1

# 查看当前系统中的索引
GET _cat/indices?v&s=index:asc
# 其中参数 v 全称是 verbose,表示返回详细的输出;s 参数表示进行排序(sort),形式 s=column1,column2:<desc/asc>。
/*
health: 当前健康状态
status: 开启/关闭(open/close)状态
index: 索引名称
uuid: 索引 uuid
pri: 主分片数目
rep: 副本分片数目
docs.count: 可用的文档数目
docs.deleted: 已删除的文档数目
store.size: 主分片和副本分片的存储文档空间大小
pri.store.size: 主分片的存储文档空间大小
GET _cat/indices?help
*/

3 更新文档中的数据

# 将1文档中的age字段修改为20
POST fcarey/_update/1
{
  "doc": {
    "age": 20
  }
}

4 删除索引

PUT test/_doc/1
{
  "name":"fcarey",
  "age":18,
  "city":"sz",
  "tag":["python","elk"]
}

# 删除test索引
DELETE test

GET test/_search

5 match

5.1 match_all

# match_all是没有任何条件,检索全部数据
GET fcarey/_search
{
  "query": {
    "match_all": {}
  }
}

5.2 match

match用来做基本的模糊匹配,在es中会对文本进行分词,在match查询的时候也会对查询条件进行分词,然后通过倒排索引找到匹配的数据。在match中支持以下参数:

  • query:查询条件
    • operator:匹配条件(AND、OR (Default))
    • minimum_should_match:最小匹配的数量,用来指定文档中至少包含几个关键字才算匹配到
    • fuzziness:最大编辑距离,详细参考Term级别查询中fuzzy查询一节的内容。
    • prefix_length:通过最大编辑距离进行模糊查询额时候,开始的多少个字符不允许被模糊,默认值为0。下面有例子
    • fuzzy_transpositions:boolean值,默认true,表示扩展模糊选项的时候,是否包含两个相邻字符的位置互换这种手段
    • fuzzy_rewrite:可以重写查询方法,目前还没实践到,关于更多说明可以参考:rewrite parameter
    • analyzer:可以指定分词器,如果不指定,用默认的
    • max_expansions:参考Term级别查询中fuzzy查询一节的内容。
    • zero_terms_query:在实际的文档中,可能有很多这样的词,比如中文中的的,了,呢,或者英文中的or、and、is、do等。那么这样的词对我们的搜索可能是没有任何帮助的,我们把这些的词- 叫做停用词,ES中有一个停用词分析器:Stop token filter,如果我们的查询请求的关键字中包括这些词,并且用到了这个分析器,那么他会帮我们把这些停用词移除掉。如果我们的查询请求中所有的关键字都被移除掉了,就不会匹配到任何文档,那么这个时候,是否要给用户返回一个空呢?ES提供了两种策略,也就是通过这个字段去表示的:
      • none (default):不返回任何文档
      • all:返回所有文档,相当于执行了match_all
    • lenient:lenient有仁慈的,宽容的意思。这里表示是否忽略一些输入错误,比如为一个数字类型的字段输入了一个字符串去匹配,如果设置为true,会忽略,默认值是false
    • auto_generate_synonyms_phrase_query:在有些场景,可能一个意思有两种写法,比如ElasticSearch有些人可能会写成ES,虽然写法不一样,但是描述的是一个东西,那么如果我们限定查询条件为ElasticSearch,其实也是希望能搜索到ES相关内容的。我们可以把这种词叫做同义词查询。因此ES为我们提供了这个参数,表示是否开启同义词查询,默认是true,也就是开启的。但是有一个问题就是ES他怎么知道哪些词是同义词呢?Lucene中有一个概念叫Synonym Graph Token Filter,那么ES中也是有的,我们可以通过对这个进行配置来实现同义词查询
GET fcarey/_search
{
  "query": {
    "match": {
      "tag": "python elk"
    }
  }
}

5.3 match_phrase

# match_phrase(phrase: 短语) 会对输入做分词,但是需要结果中也包含所有的分词,并且顺序要求一致。这个条件其实有一点苛刻了,有时候可能我输入错了,或者一个短语,只记得其中两个单词,第三个单词死活记不起来怎么办呢?ES也提供了slop这个参数帮我们解决这个问题:slop (default 0):(slop:溢出)来指定额外加几个单词也可以命中。

GET fcarey/_search
{
  "query": {
    "match_phrase": {
      "tag": {
        "query": "python php",
        "slop": 1
      }
    }
  }
}

6 sort

POST fcarey/_update/2
{
  "doc": {
    "age":19
  }
}

# 升序
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ]
}

# 降序
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "age": {
        "order": "desc"
      }
    }
  ]
}

# 报错
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "city": [
    {
      "tag": {
        "order": "asc"
      }
    }
  ]
}

7 分页

# 从第1个数据开始,展示两个数据
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 2
}


# 从第三个数据开始,展示两个数据
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "from": 2,
  "size": 2
}

8 bool查询

# 查询city为sh的数据
GET fcarey/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "city": "sh"
          }
        }
      ]
    }
  }
}

# 查询city为sh的数据且年龄为18的人
GET fcarey/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "city": "sh"
          }
        },
        {
          "match": {
            "age": "18"
          }
        }
      ]
    }
  }
}

# 查询city为sh的数据或年龄为18的人
GET fcarey/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "city": "sh"
          }
        },
        {
          "match": {
            "age": "18"
          }
        }
      ]
    }
  }
}

# 查询city不为sh的数据,年龄不为18的人
GET fcarey/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "city": "sh"
          }
        },
        {
          "match": {
            "age": "18"
          }
        }
      ]
    }
  }
}

# 查询city为sh的数据,年龄大于16小于等于20的人
GET fcarey/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "city": "sh"
          }
        }
      ],
      "filter": [
        {
          "range": {
          "age": {
            "gt": 16,
            "lte": 20
          }
        }
        }
      ]
    }
  }
}

9 _source

结果过滤

# 查询city为sh的数据,年龄大于16小于等于20的人,并只返回名字与年龄。
GET fcarey/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "city": "sh"
          }
        }
      ],
      "filter": [
        {
          "range": {
          "age": {
            "gt": 16,
            "lte": 20
          }
        }
        }
      ]
    }
  },
  "_source": ["name","age"]
}

10 高亮

# 默认高亮
GET fcarey/_search
{
  "query": {
    "match": {
      "city": "sh"
    }
  },
  "highlight": {
    "fields": {
      "city": {}
    }
  }
}

# 输出
"highlight": {
  "city": [
    "<em>sh</em>"
  ]
}

# 自定义高亮
GET fcarey/_search
{
  "query": {
    "match": {
      "city": "sh"
    }
  },
  "highlight": {
    "pre_tags": "<b style='color:red;font-size:20px;'>",
    "post_tags": "</b>", 
    "fields": {
      "city": {}
    }
  }
}
# 输出
"highlight": {
  "city": [
    "<b style='color:red;font-size:20px;'>sh</b>"
  ]
}

11 聚合

# 计算数据中所有成员的平均年龄
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "my_aggs": {
      "avg": {
        "field": "age"
      }
    }
  }
}

# 找出前两个数据中年龄最大的数据
GET fcarey/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "my_aggs": {
      "max": {
        "field": "age"
      }
    }
  },
  "from": 0,
  "size": 2
}

12 mappings

# 获取当前索引的映射
GET fcarey/_mapping

# 默认情况下,用户可以任意新增数据字段
POST test/_doc/1
{
  "name": "user01",
  "age": 19
}

POST test/_doc/2
{
  "name": "user02",
  "age": 19,
  "city":"sh"
}

POST test/_doc/3
{
  "age":20,
  "city":"sz"
}

# 查询当前mapping
GET test/_mapping


# 自定义mapping
PUT test
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "long"
      }
    }
  }
}

POST test/_doc/1
{
  "name":"user01",
  "age":19
}
POST test/_doc/2
{
  "name":"user01"
}
POST test/_doc/3
{
  "name":"user01",
  "age":20,
  "city":"sh"
}
GET test/_search
GET test/_mapping

12.1 Elasticsearch 数据类型

Elasticsearch 可以自动检测的数据类型:

Elasticsearch data type
JSON data type "dynamic":"true" "dynamic":"runtime"
null No field added No field added
true or false boolean boolean
double float double
long long long
object object No field added
array Depends on the first non-null value in the array Depends on the first non-null value in the array
string that passes date detection date date
string that passes numeric detection float or long double or long
string that doesn’t pass date detection or numeric detection text with a .keyword sub-field keyword

12.2 date_detection

# 关闭日期检测 "date_detection": false
PUT test
{
  "mappings": {
    "date_detection": false
  }
}

GET test/_mapping

PUT test/_doc/1 
{
  "date": "2023/09/02"
}
# data字段被创建为text类型字段
DELETE test


# 指定data字段类型
PUT test
{
  "mappings": {
    "dynamic_date_formats": ["MM/dd/yyyy","yyyy/MM"]
  }
}
GET test/_mapping
PUT test/_doc/1 
{
  "date": "09/02/2023"
}
GET test/_search
DELETE test

12.3 numeric_detection

# 数字检测
PUT test
{
  "mappings": {
    "numeric_detection": true
  }
}

put test/_doc/1
{
  "num_float": "1.1",
  "num_integer": "2"
}
# num_float为float类型字段
# num_integer为长整型字段

GET test/_mapping
DELETE test
posted @ 2023-06-27 22:37  f_carey  阅读(40)  评论(0编辑  收藏  举报