| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
下载安装包 : wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.1.tar.gz
在 home 下解压 : tar -zxvf elasticsearch-6.3.1.tar.gz
创建 ES 用户
ES 设置
修改配置文件, 备份配置文件, 进入文件目录
cd /home/elaticsearch-6.3.1/config/ cp elasticsearch.yml elasticsearch.yml.bak vi elasticsearch.yml
配置服务名称
#cluster.name: my-application cluster.name: my-application #取消注释
3. 配置节点名称
#node.name: node-1 node.name: node-1
#node.attr.rack: r1
4. 配置 es 数据 和 路径 的存放路径
#path.data: /path/to/data path.data: /home/esdata/data
#path.logs: /path/to/logs path.logs: /home/esdata/log
5. 内存锁配置
#bootstrap.memory_lock: true bootstrap.memory_lock: true
6. 配置服务的 ip 和端口
#network.host: 192.168.0.1 network.host: 0.0.0.0
#http.port: 9200 http.port: 9200
7. 多节点配置
#discovery.zen.ping.unicast.hosts: ["host1", "host2"] discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#discovery.zen.minimum_master_nodes:
8. 路由节点个数 和 requires_name 配置
#gateway.recover_after_nodes: 3 gateway.recover_after_nodes: 1
#action.destructive_requires_name: true
配置系统参数
vim /etc/security/limits.conf : 在文件最后添加
# End of file esuser hard nofile 65536 esuser soft nofile 65536 esuser soft memlock unlimited esuser hard memlock unlimited
vi /etc/sysctl.conf
vm.max_map_count=262144
3. 执行命令 : 1. `sysctl -p` 2. `visudo` : 添加允许 esuser 在任何地方执行命令 ``` ## The COMMANDS section may have other options added to it. ## ## Allow root to run any commands anywhere root ALL=(ALL) ALL esuser ALL=(ALL) ALL ``` 4. `vim /etc/security/limits.d/20-nproc.conf`
soft nproc 4096
root soft nproc unlimited
5. `vim /etc/security/limits.d/def.conf` : 创建并编写,文件尾部编写
安装 1.8 以上版本的 java (yum install java -y) 并启动
进入 elasticsearch-6.3.1 的目录下 : cd /home/elasticsearch-6.3.1/bin/
切换为 esuser 用户 : su esuser
启动 : ./elasticsearch
启动后访问 本机 ip 的 9200 端口 查看效果
Postman 介绍
区域划分
介绍
Es 和数据库可以进行类比学习
| 数据库 | database | table | rows | Columns |
|---|---|---|---|---|
| Elasticsearch | index | type | document | fields |
传统数据库只能存储基本的数据类型 (字符串、数字、小数、邮箱、时间),复杂的数据类型只能通过多表关系表述 (一对一、一对多、多对多)
Es 可以保存更为复杂的数据类型
索引 (index)
创建索引, 采用的请求方式为 put
创建步骤
创建索引和类型,需要在路由上描述索引,然后搭建请求的数据结构
| 参数 | 描述 |
|---|---|
| carlist | 索引,名称必须小写 |
| mappings | 映射,创建的开始 |
| car | 表名,自定义 |
| properties | 属性 |
| c_name | 字段名称,自定义 |
| type | 字段类型的键 |
| text | 文本类型 |
Es 常用字段类型
| 类型 | 描述 |
|---|---|
| string | 字符串类型 |
| long | 64 位存储,数字类型 |
| integer | 32 位存储,数字类型 |
| short | 16 位存储,数字类型 |
| byte | 8 位存储,数字类型 |
| double | 64 位双精度存储,数字类型 |
| float | 32 位双精度存储,数字类型 |
| date | 日期类型,必须指定格式 |
| Boolean | 布尔类型 |
| Binary | 二进制类型 |
| Array | 数组类型 |
| Object | 单个 json (字典) 对象 |
| nested | 嵌套的 json 对象 |
| text | 文本类型,用于全文本字段,文本不会被 Analyzer 分词 默认不支持聚合和排序,需要将 fielddata 设置为 true |
| Keyword | 用于 ID,枚举及不需要分词的文本 适用于 Filter 精确匹配,Sorting 和 Aggregations |
查看所有索引 : ip地址/_表名/indices/
查看当前索引 : ip地址:9200/索引名/
查看单条索引数据 : ip地址:9200/索引名/_search
删除索引: 发送 DELETE 请求
插入文档 (document 使用 post 请求) : ip地址/索引名/表名
查询文档 (document 使用 get 请求) : ip地址/索引名/表名/_search
安装模块 elasticsearch : pip install elasticsearch -i http://pypi.tuna.tsinghua.edu.cn/simple
插入数据脚本
import random
from elasticsearch import Elasticsearch
es = Elasticsearch("10.10.123.131", timeout=360) # 链接服务器,设置超时时间
# 尝试保存10000条数据
c_names = """奥迪
宾利
宝马
华晨
比亚迪
别克
凯迪知拉克
雪佛兰
克莱斯道勒
一汽
法拉利
菲亚特
福特
本田
捍马回
现代
捷豹
吉普
起亚
兰博基尼
路虎
雷克撒斯
林肯
莲花
玛莎拉蒂
迈巴赫
马自达
奔驰答
迷你
三菱
日产
欧宝
标志
保时捷
雷诺
劳斯莱斯
萨博
斯科达
世爵
斯巴鲁
铃木
丰田
大众""".split("\n")
c_citys = "郑州市、洛阳市、焦作市、商丘市、信阳市、周口市、鹤壁市、安阳市、濮阳市、驻马店市、\
南阳市、开封市、漯河市、许昌市、新乡市、济源市、灵宝市、偃师市、邓州市、登封市、三门峡市、\
新郑市、禹州市、巩义市、永城市、长葛市、义马市、林州市、项城市、汝州市、荥阳市、\
平顶山市、卫辉市、辉县市、舞钢市、新密市、孟州市、沁阳市、郏县".split("、")
for i in range(10000):
data = {
"c_name": random.choice(c_names),
"c_date": "%s-%s-%s" % (
random.randint(1983, 2020),
random.randint(1, 12),
random.randint(1, 28)
),
"c_mileage": random.randint(1, 10000),
"c_city": random.choice(c_citys),
"c_price": random.randint(8, 100),
"c_sale": random.randint(1, 3),
"c_service": random.randint(30, 100)
} # 构建插入数据的结构
res = es.index(index="carlist", doc_type="car", body=data) # 开始执行
print(res)
print('插入完成了....')查询基本格式
{
"query": {
"查询的类型": {
"查询条件": "条件的值"
}
}
}精准查询 term
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 精确查询
"query": {
"term": {
"c_name": "宾利"
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(result["hits"]["hits"]) # 获取更好格式的数据多条件查询 terms
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 精确查询
"query": {
"terms": {
"c_name": ["宾利", "大众"]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(result["hits"]["hits"]) # 获取更好格式的数据查询所有 match_all
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 精确查询
"query": {
"match_all":{
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(len(result["hits"]["hits"])) # 10 默认查询 10 条分页查询: from: 分页起始位置, size: 分页条数 (可以通过循环增加 from 值实现多次查询)
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 精确查询
"query": {
"match_all":{
}
},
"from": 0,
"size": 100
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"])) # 100
print(result["hits"]["hits"])排序查询 sort
顺序查找 "sort: [ '排序条件' ]"
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 精确查询
"query": {
"match_all":{
},
"sort": [
"c_date" # 按时间顺序顺序查找
],
"from": 0,
"size": 100
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])倒序查询 : "sort: { '排序条件': { 'order': 'desc' } }"
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 精确查询
"query": {
"match_all":{
},
},
"sort": {
"c_date": {
"order": "desc"
}
},
"from": 0,
"size": 100
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])模糊查询: match 匹配含有指定关键字的数据,采用分词进行建模,会有效果
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"match": {
"c_name": "宝马"
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])逻辑查询 bool
基本格式
{
"query": {
"bool": {
"逻辑类型": [
{
"查询类型1": {
"查询条件1": "查询条件1的值"
}
},
{
"查询类型2": {
"查询条件2": "查询条件2的值"
}
},
{
"查询类型3": {
"查询条件3": "查询条件3的值"
}
},
]
}
}
}must : 且, 多条件同时成立
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"bool": {
"must": [
{
"term": {
"c_name": "大众"
}
},
{
"term": {
"c_price": 18
}
}
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])should : 或, 多条件任一个成立
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
"query": {
"bool": {
"should": [
{
"term": {
"c_name": "大众"
}
},
{
"term": {
"c_price": 18
}
}
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])must_not : 非, 查询条件不成立
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = { "query": { "bool": { "must_not": [ { "term": { "c_name": "大众" } }, ] } } }
result = es.search(index="carlist", doc_type="car", body=body)
print(len(result["hits"]["hits"])) print(result["hits"]["hits"])
must 和 should 同时使用需要特殊语法 : 在 must 和 shoud 同时存在时,should 部分需要重写一个 bool 类型嵌套在 must 条件中
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
# 查询 18 万的宝马或大众
body = {
"query": {
"bool": {
"must": [
{
"term": {
"c_price": 18
}
},
{
"bool": {
"should": [
{
"term": {
"c_name": "宝马"
}
},
{
"term": {
"c_name": "大众"
}
}
]
}
}
]
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])范围查询 range
符号属性
| 符号 | 描述 |
|---|---|
| lt | 小于 |
| lte | 小于等于 |
| gt | 大于 |
| gte | 大于等于 |
实例
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 查询价格在 10 -15 万之间的所有车
"query": {
"range": {
"c_price": {
"lt": 15,
"gt": 10
}
}
},
"from": 0,
"size": 100,
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["hits"]["hits"])聚合查询 aggs
1. 聚合函数
| 聚合函数名称 | 描述 |
| ------------ | -------- |
| avg | 平均数 |
| sum | 求和 |
| max | 求最大值 |
| min | 求最小值 |
2. 基本格式
```
{
"query": {
"查询类型": {
"查询条件": "查询条件的值"
}
},
"aggs": {
"自定义名称": {
"聚合函数": {
"field": "查询条件"
}
}
}
}
```
3. 实例
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
body = {
# 求宝马均价
"query": {
"term": {
"c_name": "宝马"
}
},
"aggs": {
"price_avg": {
"avg": {
"field": "c_price"
}
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
print(result)
print(len(result["hits"]["hits"]))
print(result["aggregations"]["price_avg"]["value"]) # 均价
```
```python
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts="10.10.123.131", timeout=360)
# 查询各种车辆数量以及平均价格
body = {
"query": {
"match_all": {
}
},
"aggs": {
"case_sum": {
"terms": {
"field": "c_name"
},
"aggs": {
"price_avg": {
"avg": {
"field": "c_price"
}
}
}
}
}
}
result = es.search(index="carlist", doc_type="car", body=body)
# print(result)
print(len(result["hits"]["hits"]))
print(result["aggregations"]["case_sum"])
print(result["aggregations"]["case_sum"]["buckets"])
```
| Back | FazBrowse Home | New Git URL |