安装
docker network create elastic
docker run -it -d --name es01-test --net elastic -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.15
docker run -it -d --name kib01-test --net elastic -p 127.0.0.1:5601:5601 -e "ELASTICSEARCH_HOSTS=http://es01-test:9200" docker.elastic.co/kibana/kibana:7.17.15
什么是Elasticsearch?
Elasticsearch 是一个分布式、RESTful 风格的搜索和数据分析引擎。
组成
- 索引(Index):Elasticsearch中的索引是文档的集合,类似于关系型数据库中的数据库。每个索引都有其自己的映射(mapping),定义了索引中文档的字段和类型。
- 类型(Type):在Elasticsearch中,类型是索引中的一个分类,用于将数据划分为不同的类别。例如,可以将博客文章分为“文章”类型,将产品描述分为“产品”类型。
- 文档(Document):文档是Elasticsearch中的基本数据单元,它包含了要索引的数据。每个文档都是一个JSON对象,具有一些元数据(如_index、_type、_id)和字段值。
- 字段(Field):字段是文档中的属性,类似于关系型数据库中的列。每个字段都有其自己的数据类型(如字符串、整数、布尔值等)和索引选项(如是否被索引、是否区分大小写等)。
语法
新建索引(不带配置信息的话以默认配置创建)
PUT /product
新建索引(带配置信息的话以配置信息创建)
PUT /product
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 2
},
"mappings": {
"properties": {
"name":{
"type": "text"
},
"value":{
"type": "integer"
}
}
}
}
新增字段
PUT /product2/_mapping
{
"properties": {
"hight": {
"type": "integer"
}
}
}
查询索引
GET /product
删除索引
DELETE /product
修改索引
PUT /product/_settings
{
"index":{
"number_of_replicas":2
}
}
关闭索引
POST /product/_close
打开索引
POST /product/_open
别名
POST /_aliases
{
"actions": [
{
"add": {
"index": "product",
"alias": "alias1",
"is_write_index":true
}
}
]
}
插入数据
POST /product/_doc
{
"name":"测试_ES222",
"value":1222
}
修改数据
POST /product/_doc/{id}
{
"name":"测试_ES2223",
"value":12223
}
查询
GET /product/_search
{
"query": {
"term": {
"value": {
"value": 1
}
}
}
}
查询(别名)
GET /alias1/_search
{
"query": {
"term": {
"value": {
"value": 1
}
}
}
}
match:匹配查询
match_phrase:连续的字段查找
multi_match:从多个字段中去寻找我们要查找的
term:不分词查询
terms:多个term查询取一个交集
fuzzy:是ES里面的模糊搜索,它可以借助term查询来进行理解。fuzzy和term一样,但是不同的是它在进行匹配时可以容忍你的词语拼写有错误,至于容忍度如何,是根据参数fuzziness决定的。fuzziness默认是2
range:范围查询
bool:查询是上面查询的一个综合,它可以用多个上面的查询去组合出一个大的查询语句