Java是面向对象的,都是Java类。Mysql是面向数据行的,一行一行的。而我们的Elasticsearch是面向document的(文档,理解成json格式就行)。
有三种状态:green、yellow、red
每个索引的primary shard和replica shard都是active状态的
每个索引的primary shard都是active状态的,但是部分replica shard不是active状态,处于不可用的状态
不是所有索引的primary shard都是active状态的,部分索引有数据丢失了
三个索引
PUT test_index?pretty
再次查看 GET _cat/indices?v
DELETE test_index?pretty
再次查看 GET _cat/indices?v
我们的test_index没了,被删了。
ES的搜索语法贼拉多,贼拉牛逼。这里只演示
PUT index/type/id
{
"key", "value"
}
Elasticsearch7.x已经将type的概念移除了,不能自定义,每个index只能是默认的不可修改的名叫_doc的type,所以语法变成了如下
PUT index/_doc/id
{
"key", "value"
}
PUT test_index/_doc/1
{
"name": "mobile",
"desc": "shouji",
"price": 3000,
"brandName": "xiaomi",
"tags": ["5G", "niubi"]
}
GET index/_doc/id
GET test_index/_doc/1
PUT index/_doc/id
{
"key", "value"
}
其实就是添加文档的语法。如果index/doc/id存在,则会覆盖掉老的【这个覆盖需要注意的是比如之前五个字段,你这次只PUT了一个字段,那么其余四个会被删除,相当于删除重建】。不存在,则会创建。如果只想更新某个字段怎么办?看下面的动态更新。
PUT test_index/_doc/1
{
"name": "xiaomi mobile"
}
我们在把数据还原回去,这样方便下面的测试。
POST index/_update/id
{
"doc": {
"key", "value"
}
}
将name的mobile改为shouhuan
POST test_index/_update/1
{
"doc": {
"name": "shouhuan"
}
}
DELETE index/_doc/id
DELETE test_index/_doc/1
GET index/_search
再准备一条id=2的数据
PUT test_index/_doc/2
{
"name": "mobile2",
"desc": "shouji2",
"price": 30002
}
进行搜索
GET test_index/_search
返回结果
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "mobile2",
"desc" : "shouji2",
"price" : 30002
}
},
{
"_index" : "test_index",
"_type" : "test_type",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "mobile",
"desc" : "shouji",
"price" : 3000,
"brandName" : "xiaomi",
"tags" : [
"5G",
"niubi"
]
}
}
]
}
}