优势:不用一列一列的检索数据,组装为对象,用其他话来说它是面向对象的
参考链接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html

https://mp.weixin.qq.com/s/YwV9kRNNJ8OSmHJ7u4f_Yw

1.基本概念介绍

索引 index : 它包括了分词列表及文档列表。
映射 mapping: 定义数据处理的方式和规则,比如分析器、是否被索引、数据类型、默认值、
字段 Field: 文档的分类标识
类型 Type: 例如:Text、Keyword、Byte 等
文档 document: 相当于mysql表中的数据,用JSON(Javascript Object Notation)格式来表示

分片和副本 shards&replicas:
分片:索引划分成多份的能力,这些份就叫做分片
副本:索引的复制,用于故障转移

es索引可以理解为库也可以理解为表,取决于你是 type 的定义,你统一定义为无业务相关的_doc 索引就是表,否则索引为库,注意新版本已经删掉了type自定义

  1. 操作

es中POST和PUT的区别
POST非幂等的,没有精确到某一资源文件,执行会生成多个多个UUID不同的文档
PUT是幂等的,必须精确到某一个资源文件,执行多次和第一次一致

创建索引

POST /phone
{
"settings":{
    "index":{
      "number_of_shards":1,
      "number_of_replicas":1
    }
  }
}

指定了分片为1,副本为1 ,但是分片数量创建之后不能更改。 默认是 分片数是1,副本数是1

查看分片数数信息

GET /phone/_settings

调整副本数

PUT phone_demo/_settings
{
  "number_of_replicas":5
}

创建文档

PUT phone/_doc/1(索引/文档类型/ID)
{
 "name":"iphone",
 "price":"998"
}

文档类型,也叫映射类型(type),一个映射类型下的文档(document),都有相同的字段(field)。

搜索文档

GET _search?q=name:iphone

返回信息:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 7,
        "successful": 7,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 0.13353139,
        "hits": [
            {
                "_index": "phone",
                "_id": "1",
                "_score": 0.13353139,
                "_source": {
                    "name": "iphone",
                    "price": "998"
                }
            },
            {
                "_index": "phone",
                "_id": "zEUI_YwBJjjxlHIcf2Ns",
                "_score": 0.13353139,
                "_source": {
                    "name": "iphone",
                    "price": "998"
                }
            },
            {
                "_index": "phone",
                "_id": "2",
                "_score": 0.13353139,
                "_source": {
                    "name": "iphone",
                    "price": "998"
                }
            }
        ]
    }
 }

删除文档

DELETE phone/_doc/2

刪除索引

DELETE /person

创建映射就是向索引库中创建字段及字段类型等的过程(比如c语言中的 int a)
映射字段类型有 text(非结构化数据) keyword(结构化数据,不能分词) date long/short/integer float/double boolean ip object nested(json数组) geo_point(经纬度)

创建映射

PUT  /person
{
  "mappings":{
    "properties":{
      "name":{"type":"text"},
      "age":{"type":"short"}
    }
  }
}

获取映射字段

GET /person/_mapping/field/name

批量插入

POST book-demo/_bulk
{"index": {"_id": 100}}
{"name": "author0","price":100}
{"index": {"_id": 101}}
{"name": "author1","price":101}
{"index": {"_id": 102}}
{"name": "author2","price":102}
{"index": {"_id": 103}}
{"name": "author3","price":103}
{"index": {"_id": 104}}
{"name": "author4","price":104}
{"index": {"_id": 105}}
{"name": "author5","price":100}

解释:
create 如果文档不存在就创建,但如果文档存在就返回错误
index 如果文档不存在就创建,如果文档存在就更新
update 更新一个文档,如果文档不存在就返回错误
delete 删除一个文档,如果要删除的文档id不存在,就返回错误
或者在json中指定索引名字

POST _bulk
{"index": {"_id": 100,"_index": "example"}}
{"name": "author0","price":100}
{"index": {"_id": 101,"_index": "example"}}
{"name": "author1","price":101}
{"index": {"_id": 102,"_index": "example"}}
{"name": "author2","price":102}
{"index": {"_id": 103,"_index": "example"}}
{"name": "author3","price":103}
{"index": {"_id": 104,"_index": "example"}}
{"name": "author4","price":104}
{"index": {"_id": 105,"_index": "example"}}
{"name": "author5","price":100}

查询

1.查询所有数据

GET example/_search?pretty
{
   "query":{
      "match_all":{}
   }
}

2.查询指定数据

GET /example/_search?pretty
{
   "query":{
      "match":{
         "字段名":"字段值"
      }
   }
}

需要注意的是不支持多字段查询!

3.多条件布尔复合查询

GET example/_search?pretty
{
   "query":{
      "bool":{
         "must":{"match":{"price":"100"}},
         "must_not":{"match":{"name":"author0"}}
         
      }
   }
}

其他关键字:
should 相当于可有可无的条件 相当于or
term 精确匹配
terms 匹配多个值

4.range过滤

GET /example/_search?pretty
{
   "query":{
      "range":{
         "price":{"gt":102}
      }
   }
}

5.包含和不包含(某)

exists 和 missing

GET /example/_search?pretty
{
   "query":{
      "exists":{
         "field":"price"
      }
   }
}

6.bool 的多条件过滤

GET /example/_search?pretty
{
   "query":{
      "bool":{
         "must":[
            {"term":{"price":"100"}},
            {"range":{"age":{"gt":18}}}
         ]
         
      }
   }
}

7.bool过滤查询

POST /example/_search?pretty 
{
   "query":{
      "bool":{
         "must": {"term":{"price":"100"}},
         "filter":{"range":{"age":{"gt":18}}}
         
         
      }
   }
}

迁移索引数据

1.查询当前索引的settings,mappings

GET /example?pretty

2.利用这个新建索引
PUT /example2

{
  "settings": {
                "number_of_shards": "3",
                "number_of_replicas": "10"
            },
  "mappings": {
            "properties": {
                "age": {
                    "type": "long"
                },
                "detail": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
        "price": {
                    "type": "long"
                }
            }
        }
    
}

3.拷贝数据

POST _reindex
{
  "source": {
    "index": "example"
  },
  "dest": {
    "index": "example2"
  }
}

end!

最后修改:2024 年 01 月 17 日
如果觉得我的文章对你有用,请随意赞赏