Index Api
很多地方将ElasticSearch与数据库类比,比如将index看作数据库,type看作数据库表,id看作表结构中的主键。但是,在内部的机制中却有不同,在同一个数据库中,不同的表的字段可以名称相同,类型不同,但是在ES中,由于在不同映射类型中具有相同名称的字段在内部由相同的Lucene字段支持,因此在相同的索引中,如果给不同type设置相同filed,但是filed的属性不同的话,在操作比如删除时,就可能会删除失败。除此之外,存储在同一索引中具有很少或没有共同字段的不同实体会导致数据稀疏,并影响Lucene有效压缩文档的能力。因此在es7的时,废除类mapping types 的设置。
notice:对于每一种类型的document设置一个index
比如将user和information单独设置成两个index,而不是将他们两个放到同一个index中。在es中index中,index之间是相互独立的,因此对于里面的字段将不会引起冲突。这样主要包含两个好处
数据更加稠密,有利于lucene对数据进行压缩
因为同一索引中的所有文档都表示一个实体,用于在全文搜索中得分的统计术语更可能准确。
es的rest请求用的标准的rest风格的接口
1.Index APIs
1.1添加索引,用put请求,不指定结构
添加一个索引名称为user的索引,设定分片和副本数
curl -X PUT "192.168.94.151:9200/user/" -H 'Content-Type: application/json' -d'
{
"settings": {
"index": {
"number_of_shards": "2",
"number_of_replicas": "1"
}
}
}
'
返回结果:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "user"
}
参数介绍:
settings index 固定结构
number_of_shards分片数量
number_of_replicas副本数量
1.2 添加索引,指定结构,
默认type _doc 不可以修改
put 192.168.94.151:9200/blog
{
"settings":{
"index":{
"number_of_shards":3,
"number_of_replicas":1
}
},
"mappings": {
"properties": {
"id": {
"type": "long",
"index": false,
"store": true
},
"title": {
"type": "text",
"store": true,
"index": true,
"analyzer": "standard"
},
"content": {
"type": "text",
"store": true,
"index": true,
"analyzer": "standard"
}
}
}
}
返回结果
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "blog"
}
{
"settings":{
"number_of_shards":1,
"number_of_replicas":1
},
"mappings":{
"properties":{
"author":{
"type":"keyword"
},
"publish_date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"word_count":{
"type":"integer"
}
}
}
}