Elasticsearch提供了Rest风格的API,即http请求接口
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。
索引(indices) | Databases 数据库 |
---|---|
类型(type) | Table 数据表 |
文档(Document) | Row 行 |
字段(Field) | Columns 列 |
Elasticsearch Guide [6.8] | Elastic
put book
{
"settings":{
"number_of_shards":1, /*分片数量*/
"number_of_replicas":0 /*副本数量*/
}
}
ES中所存数据的文件块,也是数据的最小单元块。假如有2个分片,插入10条数据,默认,每个分片存5条。
get 索引库名
delete 索引库名
创建索引库,就相当于mysql创建“数据库”。接着我们来配置映射,相当于mysql创建“表结构”。
PUT /索引库名/_mapping/类型名称
{
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"
}
}
}
/* 1.先创建索引 */
put czxy
/* 2.创建映射关系 */
put czxy/_mapping/book
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"images": {
"type": "keyword",
"index": "false"
},
"price": {
"type": "float"
}
}
}
GET /索引库名/_mapping
1)type
2)index
index影响字段的索引情况。
index的默认值就是true,也就是说你不进行任何配置,所有字段都会被索引。
但是有些字段是我们不希望被索引的,比如商品的图片信息,就需要手动设置index为false。
3)store
是否将数据进行额外存储。
Elasticsearch在创建文档索引时,会将文档中的原始数据备份,保存到一个叫做`_source`的属性中。而且我们可以通过过滤`_source`来选择哪些要显示,哪些不显示。
而如果设置store为true,就会在`_source`以外额外存储一份数据,多余,因此一般我们都会将store设置为false,事实上,**store的默认值就是false。**
POST _index/_type/
{
"属性": "值",
....
}
POST czxy/book/
{
"title": "标题",
"price": 1234
}
PUT _index/_type/_id
{
"属性": "值",
....
}
PUT czxy/book/b001
{
"title": "标题123",
"price": 1234
}
DELETE _index/_type/_id
DELETE czxy/book/b001
GET czxy/book/b001
GET czxy/book/b001?_source=title,price
PUT czxy2
PUT czxy2/_mapping/user
{
"properties": {
"username": {
"type": "text",
"analyzer": "ik_max_word"
},
"city": {
"type": "keyword"
},
"age": {
"type": "float"
}
}
}
PUT czxy2/user/u001
{
"username": "张三",
"city": "山西",
"age": 18
}
PUT czxy2/user/u002
{
"username": "张四",
"city": "山东",
"age": 20
}
PUT czxy2/user/u003
{
"username": "张三三",
"city": "山东",
"age": 22
}
GET _search
{
"query": {
"match_all": {}
}
}
GET czxy2/_search
{
"query": {
"match_all": {}
}
}
GET czxy2/_search
{
"query": {
"match_all": {}
},
"_source": ["username","city"]
}
GET czxy2/_search
{
"query": {
"match": {
"username": "张三"
}
}
}
GET czxy2/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"city": "山东"
}
}
],
"must_not": [
{
"match": {
"age": 22
}
}
]
}
}
}
GET czxy2/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"city": "山东"
}
},
{
"match": {
"city": "山西"
}
}
]
}
}
}
GET czxy2/_search
{
"query": {
"term": {
"age": 2
}
}
}
GET czxy2/_search
{
"query": {
"range": {
"age": {
"gte": 19,
"lte": 21
}
}
}
}
GET czxy2/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
GET czxy2/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 1
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有