什么是elasticsearch?说白了是做搜索和分析的,搜索像我们的电商的站内搜索,分析像我们把一些多维度数据写入es 做聚合分析。
百度!=搜索。它的应用的像国内的电商网站,招聘网站,新闻网站,各种app里的搜索栏。
es的特点
1.可以作为一个大型分布式集群(数百台服务器)技术,处理PB级数据,服务大公司;也可以运行在单机上,服务小公司
2.将全文检索、数据分析以及分布式技术,合并在了一起,并能对海量数据进行近实时处理
说到es不得不说lucene,lucene号称最先进、功能最强大的搜索库,直接基于lucene开发,非常复杂,api复杂。有了es ,纯用lucene得人也越来越少。es封装了lucene的开源项目。
es得核心概念:
1.近实时 :秒级别
2 es集群和node节点
3Document:es中得最小数据单元
4index 索引,一个index包含很多documen
5 type 比如商品得index ,有多个type,如日用tpye,电器type。等等多个种类tpye。
6 shard 单台机器无法存储大量数据,es可以将一个索引中的数据切分为多个shard,分布在多台服务器上存储 shard得好处,横向扩展。
7 replica 任何一个服务器随时可能故障或宕机,此时shard可能就会丢失,因此可以为每个shard创建多个replica副本。保证数据不丢。
像index 像数据库得库,tpye像表,Document像行。
document数据格式:es 面向文档,用json来表达。
举个例子:
es提供了一套api,叫做cat api,可以查看es中各种各样的数据
查状态:GET /_cat/health?v
快速查看集群中有哪些索引:GET /_cat/indices?v
简单的索引操作
创建索引:PUT/test_index?pretty
删除索引:DELETE /test_index?pretty
商品的CRUD操作
(1)新增商品:新增文档,建立索引
PUT /index/type/id
{ "json数据"
}
查询商品:检索文档
GET /index/type/id
GET /ecommerce/product/1
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "gaolujie yagao",
"desc": "gaoxiao meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai",
"fangzhu"
]
}
}
修改商品:替换文档
PUT /ecommerce/product/1
{
"name" : "jiaqiangban gaolujie yagao",
"desc" : "gaoxiao meibai",
"price" : 30,
"producer" : "gaolujie producer",
"tags": [ "meibai", "fangzhu" ]
}
修改商品:更新文档
POST /ecommerce/product/1/_update
{
"doc": {
"name": "jiaqiangban gaolujie yagao"
}
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 8,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
删除商品:删除文档
DELETE /ecommerce/product/1
{
"found": true,
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 9,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"found": false
}
PUT /ecommerce/product/1
{ "name" : "gaolujie yagao", "desc" : "gaoxiao meibai", "price" : 30, "producer" : "gaolujie producer", "tags": [ "meibai", "fangzhu" ] }
{ "_index": "ecommerce", "_type": "product", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true }
PUT /ecommerce/product/2
{ "name" : "jiajieshi yagao", "desc" : "youxiao fangzhu", "price" : 25, "producer" : "jiajieshi producer", "tags": [ "fangzhu" ] }
PUT /ecommerce/product/3
{ "name" : "zhonghua yagao", "desc" : "caoben zhiwu", "price" : 40, "producer" : "zhonghua producer", "tags": [ "qingxin" ] }