Elasticsearch的安装及Python对es的常用操作

一 Elasticsearch的安装
1:先确保linux已安装docker
2:启动Docker
systemctl start docker
3:打开docker官方仓库,拉取自己需要的elasticsearch镜像版本,拉取镜像的时候,需要指定其版本号
Docker官网: https://hub.docker.com
4:拉取镜像
docker pull docker.elastic.co/elasticsearch/elasticsearch:7.3.0(7.3.0为最新版本号)
5:使用镜像创建容器,启动elasticsearch服务
分两种方式,开发者模式和生产模式,开发者不需要配置太多,直接一行命令搞定,生产模式需要更多的配置
开发者模式
1:创建网络
docker network create esnet
2 创建并启动elasticsearch容器
查看镜像:docker images
在这里插入图片描述
创建运行
docker run --name es -p 9200:9200 -p 9300:9300 --network esnet -e “discovery.type=single-node” 095b6487fb77
-name 名称 给容器起个名字

-p 外部访问端口:容器端口 9200是供htpp访问端口,9300是供tcp访问的端口,如果不做端口映射,浏览器就不能访问elasticsearch的服务

–network 网络名 用于多个服务通信与隔离,例如用kibana连接elasticsearch就需要他们在同一个网络下

–095b6487fb77 通过docker images命令查看到需要创建的容器id,此处用镜像名也可以
使用docker ps查看elasticsearch容器是否创建成功
在这里插入图片描述在这里插入图片描述
二 Python对Elasticsearch的常用操作
安装es相关模块: pip install elasticsearch5

#encoding:utf-8
from elasticsearch5 import Elasticsearch
import datetime
#es集群服务器地址
ES = [
    '172.18.0.2:9200'
]

# 创建elasticsearch客户端
es = Elasticsearch(
    ES,
    # 启动前嗅探es集群服务器
    sniff_on_start=True,
    # es集群服务器结点连接异常时是否刷新es节点信息
    sniff_on_connection_fail=True,
    # 每60秒刷新节点信息
    sniffer_timeout=60
)

# 向es中插入插入数据
# 不指定id,自动生成
es.index(index="megacorp", doc_type="error_code", body={"first_name":"wanger","last_name":"wanger", 'age': 28, 'about': 'I love to cooking', 'interests': ['book', 'play']})
# 指定id
es.index(index="megacorp1",id=4, doc_type="error_code",body={"first_name":"xiao","last_name":"wu", 'age': 66, 'about': 'I love to go rock climbing', 'interests': ['sleep', 'eat']})
# 指定id删除
es.delete(index="megacorp", id="AXUcfxddceyKQQupUaHK", doc_type="error_code")
# delete_by_query:删除满足条件的所有数据,查询条件必须符合DLS格式
query = {
    "query": {
        "match": {
            "first_name": "xiao"
        }
    }
}
result = es.delete_by_query(index="megacorp", body=query)
print(result)
# match_all 查询--可以查询到所有文档,是没有查询条件下的默认语句。
query = {
  "query": {
    "match_all": {}
  }
}
result = es.search(index="megacorp", body=query)
print(result)
# 使用dsl查询
# term 过滤--term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串(未经切词的文本数据类型)
query = {
    "query":{
        "term":{
            "age":28
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
query = {
    "query":{
        "term":{
            "first_name":"ls"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# terms 过滤--terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配
query = {
    "query":{
        "terms":{
            # "age": [25, 28]
            "first_name": ["ls", "zhangsan"]
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# range 过滤--按照指定范围查找一批数据
"""
gt : 大于gte : 大于等于lt : 小于lte : 小于等于
"""
query = {
    "query":{
        "range":{
            "age": {
                    "gt": 28
                    }
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# exists 和 missing 过滤--查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件
query = {
    "query":{
        "exists": {
            "field": "first_name"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# bool 过滤--合并多个过滤条件查询结果的布尔逻辑
# must :: 多个查询条件的完全匹配,相当于 and, must_not :: 多个查询条件的相反匹配,相当于 not,should :: 至少有一个查询条件匹配, 相当于 or。
query = {
    "query":{
        "bool":{
            "must":{
                "term": {"_score": 1.0},
                "term": {"age": 25}

            }
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
query = {
    "query":{
        "bool":{
            "must":{
                "term": {"age": 25}

            },
            "must_not":{
                "exists":{
                    "field": "name"
                }
            }
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# match 查询--标准查询,不管你需要全文本查询还是精确查询基本上都要用到它
# 做精确匹配搜索时,你最好用过滤语句,因为过滤语句可以缓存数据。
# match查询只能就指定某个确切字段某个确切的值进行搜索,而你要做的就是为它指定正确的字段名以避免语法错误。
query = {
    "query":{
        "match": {
            "about": "rock"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# multi_match 查询--match查询的基础上同时搜索多个字段,在多个字段中同时查一个
query = {
    "query":{
        "multi_match":{
            "query": "game",
            "fields": ["about", "interests"]
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# bool 查询--与 bool 过滤相似,用于合并多个查询子句。不同的是,bool 过滤可以直接给出是否匹配成功, 而bool 查询要计算每一个查询子句的 _score (相关性分值)。
query = {
    "query":{
        "bool":{
            "must": {
                "match": {"last_name": 'ls'}
            },
            "must_not":{
                "exists":{
                    "field": "name"
                }
            }
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# wildcards 查询--使用标准的shell通配符查询
query = {
    "query": {
        "wildcard": {
            "about": "ro*"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# regexp 查询
query = {
    "query": {
        "regexp": {
            "about": "*.ov.*"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# prefix 查询 -- 以什么字符开头的
query = {
    "query": {
        "prefix": {
            "about": "I"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# 短语匹配(Phrase Matching) -- 寻找邻近的几个单词
query = {
    "query": {
        "match_phrase": {
            "about": "I love"
        }
    }
}
result = es.search(index="megacorp", body=query)
print(result)
# 统计功能
query = {
    "query": {
        "match_phrase": {
            "about": "I love"
        }
    }
}
result = es.count(index="megacorp", body=query)
print(result)
# 更新
query = {
    "query": {
        "match": {
            "last_name": "xiao"
        }
    },
    "script":{
        "source": "ctx._source.last_name = params.name;ctx._source.age = params.age",
        "lang": "painless",
        "params" : {
            "name" : "wang",
            "age": 100,
        },
    }

}
result = es.update_by_query(index="megacorp", body=query)
print(result)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Enougme

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值