elasticsearch java_java Elasticsearch详细介绍及使用实例分析

集群健康检查

取得健康状态

GET /_cat/health?v

返回:

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent1535248805 10:00:05 elasticsearch yellow 1 1 1 1 0 0 1 0 - 50.0%

健康状态分类

green:索引的primary shard和replica shard都是active状态的

yellow:索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态、

red:不是所有索引的primary shard都是active状态的,部分索引有数据丢失了

当前处于yellow状态是因为目前只启动了一个es进程,只有一个节点node,这是不安全的。这个节点的primary shard 有分配,但是replica shard 没有节点分配,因此是yellow状态。

快速查看集群有哪些节点

GET /_cat/indices?v

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

yellow open .kibana xSbUffi_SYGYIhPtiMxH5w1 1 1 0 3.1kb 3.1kb

简单的索引操作

创建索引:PUT /test_index?pretty

返回:

{"acknowledged": true,"shards_acknowledged": true}

删除索引:DELETE /test_index?pretty

返回:

{"acknowledged": true}

CURD 操作Demo

(1)新增商品:新增文档,建立索引

PUT /ecommerce/product/1{"name" : "xi jie jing","desc" : "gaoxiao qing jie","price" : 30,"producer" : "xijiejing producer","tags": [ "qingxi", "wuzhi"]

}

返回:

{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"created": true}

新增

PUT /ecommerce/product/2{"name" : "niunai","desc" : "mengniu niunai","price" : 25,"producer" : "mengniuniunai producer","tags": [ "niunai"]

}

返回:

{"_index": "ecommerce","_type": "product","_id": "2","_version": 1,"result": "created","_shards": {"total": 2,"successful": 1,"failed": 0},"created": true}

(2)查询商品:检索文档

GET /ecommerce/product/1

返回:

{"_index": "ecommerce","_type": "product","_id": "1","_version": 1,"found": true,"_source": {"name": "xi jie jing","desc": "gaoxiao qing jie","price": 30,"producer": "xijiejing producer","tags": ["qingxi","wuzhi"]

}

}

(3)修改商品:替换文档

PUT /ecommerce/product/1{"name" : "jiaqiangban gaolujie yagao","desc" : "gaoxiao meibai","price" : 30,"producer" : "gaolujie producer","tags": [ "meibai", "fangzhu"]

}

返回:

{"_index": "ecommerce","_type": "product","_id": "1","_version": 2,"result": "updated","_shards": {"total": 2,"successful": 1,"failed": 0},"created": false}

(5)删除商品:删除文档

DELETE /ecommerce/product/1

{"found": true,"_index": "ecommerce","_type": "product","_id": "1","_version": 3,"result": "deleted","_shards": {"total": 2,"successful": 1,"failed": 0}

}

多种搜索方式

1 query string search

GET /ecommerce/product/_search

{"took": 12,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 1,"max_score": 1,"hits": [

{"_index": "ecommerce","_type": "product","_id": "2","_score": 1,"_source": {"name": "niunai","desc": "mengniu niunai","price": 25,"producer": "mengniuniunai producer","tags": ["niunai"]

}

}

]

}

}

字段说明:

took:耗时

timed_out:是否超时

_shards:数据拆成了5个分片

hits.total:查询结果的数量,3个document

hits.max_score:匹配分数,越相关,就越匹配,分数也高

hits.hits:包含了匹配搜索的document的详细数据

此方法只是临时在命令行使用,例如crul,不适用于复杂查询。

2 query DSL

可以用json语法查询语法

查询所有的商品:

GET /ecommerce/product/_search

{"query": { "match_all": {} }

}

按照价格排序:

GET /ecommerce/product/_search

{"query": {"match": {"name" : "yagao"}

},"sort": [

{"price": "desc"}

]

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值