ES开箱操作
ES开箱使用
数据类型
数据类型 | |
---|---|
字符串类型 | text、keyword |
数值类型 | long、integer、short、byte、double、float、half float、scaled float |
日期类型 | date |
布尔值类型 | boolean |
二进制类型 | binary |
ES RESTful API
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
- 部件名作用VERB:HTTP方法 谓词
GET
、POST
、PUT
、HEAD
或者DELETE
。 - PROTOCOL:http 或者 https(如果你在 Elasticsearch 前面有一个 https 代理)
- HOST:ES集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。
- PORT:运行 Elasticsearch HTTP 服务的端口号,默认是 9200 。
- PATH:API 的终端路径(例如 _count 将返回集群中文档数量)。
- Path 可能包含多个组件,例如:_cluster/stats 和 _nodes/stats/jvm 。
- QUERY_STRING:任意可选的查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)
- BODY:一个 JSON 格式的请求体 (如果请求需要的话)
# 计算集群中文档的数量
curl -X GET 'http://localhost:9200/_count?pretty' -d '
{
"query": {
"match_all": {}
}
}
'
# Kibana中
GET /_count?pretty
{
"query": {
"match_all": {}
}
}
- pretty:美化输出(_source字段不会被美化,它的样子与我们输入的一致。)
CRUD
添加
PUTPOST /索引名/类型名(新版本逐步废弃,默认类型_doc代替)/文档ID
{
请求体
}
往索引为 db(SQL数据库), 类型为 user(SQL Table) 的数据库中插入一条 id 为 1(SQL row?) 的一条数据(拥有 username/password/age 三个属性的实体)。
POST /db/user/1
{
"username": "wmyskxz1",
"password": "123456",
"age": "22"
}
PUT /test3/_doc/1
{
"name": "流柚",
"age": 18,
"birth": "1999-10-10"
}
PS:PUT如果漏了一些信息,原始信息就会丢失,故现在一般使用POST来更新索引。
指定字段索引(创建规则)并映射字段类型
PUT /test2
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age":{
"type": "long"
},
"birthday":{
"type": "date"
}
}
}
}
获取建立的规则
GET test2
删除
DELETE /db/user/1
修改
put覆盖
PUT /db/user/1
{
"username": "wmyskxz3",
"password": "123456",
"age": "22"
}
使用post的update
- version不会改变
- 不会丢失字段
- 注意doc
POST /test3/_doc/1/_update
{
"doc":{
"name" : "post修改,version不会加一",
"age" : 2
}
}
查询
GET /db/user/1
GET <index>/<type>/_search
简单条件查询
GET /test3/_doc/_search?q=name:流柚
查询匹配match
- match:匹配
- _source:过滤字段
- sort:排序
- from:索引开始
- size:限制显示个数
GET /blog/user/_search
{
"query":{
"match":{
"name":"流"
}
},
"_source": ["name","desc"],
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0,
"size": 1
}
多条件查询(bool)
- must: 相当于 and
- should: 相当于 or
- must_not: 相当于 not (… and …)
- filter:过滤条件
- range:按范围过滤
GET /blog/user/_search
{
"query":{
"bool": {
"must": [
{
"match":{
"sex": "女"
}
},
{
"match": {
"name": "流"
}
}
],
"filter": {
"range": {
"age": {
"gte": 1,
"lte": 100
}
}
}
}
}
}
匹配数组:query
多关键字查(空格隔开) :match 会使用分词器解析(先分析文档,然后进行查询)
GET /blog/user/_search
{
"query":{
"match":{
"name":"流 水"
}
}
}
精确查询item(完整词查询,不可分)
term 直接通过 倒排索引 指定词条查询
GET /blog/user/_search
{
"query":{
"term":{
"name":"流 水"
}
}
}
高亮查询highlight
GET blog/user/_search
{
"query": {
"match": {
"name":"流"
}
},
"highlight": {
"fields": {
"name": {}
}
}
}
自定义前后缀
GET blog/user/_search
{
"query": {
"match": {
"name":"流"
}
},
"highlight": {
"pre_tags": "<p class='key' style='color:red'>",
"post_tags": "</p>",
"fields": {
"name": {}
}
}
}
查询DSL(条件语句)
ElasticSearch自己基于JSON的域特定语言,可以在其中表达查询和过滤器。
GET /_search
{
"query": {
//Query DSL here
"query_string": {
"query": "kill"
}
}
}
命令行请求:
curl -X GET 'http://localhost:9200/db/user/_search?q=kill&pretty'
或
curl -X GET 'http://localhost:9200/db/user/_search?pretty' -d '
{
"query" : {
"query_string": {
"query": "kill"
}
}
}
'
指定搜索的字段fields
fields:可用于指定要搜索的字段列表。如果不使用“fields”字段,ElasticSearch查询将默认自动生成的名为 “_all” 的特殊字段,来基于所有文档中的各个字段匹配搜索。
GET /_search
{
"query": {
"query_string": {
"query": "ford",
"fields": [
"title"
]
}
}
}
过滤
-
filtered
(废除)
过滤的查询:具有两个属性query
和filter
。
GET /_search
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "drama"
}
},
"filter": {
# //Filter to apply to the query
"term": {"year": 1962}
}
}
}
}
条件过滤器:term
无需查询即可进行过滤
- (废除)
GET /_search
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"term": {
"year": 1962
}
}
}
}
}
- 使用常数分数查询
GET /_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"year": 1962
}
}
}
}
}
过滤器
bool查询
Bool查询对应Lucene中的BooleanQuery,它由一个或者多个子句组成,每个子句都有特定的类型。
must:返回的文档必须满足must子句的条件,并且参与计算分值
filter:返回的文档必须满足filter子句的条件。但是不会像Must一样,参与计算分值。
should
must_not
text VS keyword
text:支持分词、全文检索,支持模糊、精确查询,不支持聚合、排序操作。最大支持的字符长度无限制,适合大字段存储。
keyword:不进行分词,直接索引,支持模糊、精确匹配,支持聚合、排序操作。keyword类型的最大支持的长度为32766个UTF-8类型的字符,可以通过设置ignore_above指定自持字符长度,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。
GET _analyze
{
"analyzer": "keyword",
"text": ["测试liu"]
}// keyword不会分词,即 测试liu
GET _analyze
{
"analyzer": "standard",
"text": ["测试liu"]
}// 分为 测 试 liu
GET _analyze
{
"analyzer":"ik_max_word",
"text": ["测试liu"]
}// 分为 测试 liu
get _cat/xxx获取当前更多信息
GET _cat/indices
GET _cat/aliases
GET _cat/allocation
GET _cat/count
GET _cat/fielddata
GET _cat/health
GET _cat/indices
GET _cat/master
GET _cat/nodeattrs
GET _cat/nodes
GET _cat/pending_tasks
GET _cat/plugins
GET _cat/recovery
GET _cat/repositories
GET _cat/segments
GET _cat/shards
GET _cat/snapshots
GET _cat/tasks
GET _cat/templates
GET _cat/thread_pool
常用命令
- 集群的健康状态:
GET _cluster/health
- 查询集群中节点的信息:
GET /_cat/nodes
、GET /_cat/nodes?v
(?v 让kibana 输出逻辑模式即verbose模式的首字母)