es 入门

简介: **注意事项-------使用ealsticsearch要配置java的开发环境JDK(1.8以上)ealsticsearch:               索引(Index)    类型(type)    文档(Document)   字段(Fields)关系型数据库:          ...

**注意事项-------使用ealsticsearch要配置java的开发环境JDK(1.8以上)

ealsticsearch:               索引(Index)    类型(type)    文档(Document)   字段(Fields)

关系型数据库:                 数据库         表            行               列


from elasticsearch import Elasticsearch
 

默认host为localhost,port为9200.但也可以指定host与port

es = Elasticsearch("http://127.0.0.1:9200")
 

插入数据,index,doc_type名称可以自定义,id可以根据需求赋值,body为内容

es.index(index="my_index",doc_type="test_type",id=0,body={"name":"python","addr":"深圳"})
es.index(index="my_index",doc_type="test_type",id=1,body={"name":"java","addr":"北京"})
 

同样是插入数据,create() 方法需要我们指定 id 字段来唯一标识该条数据,而 index() 方法则不需要,如果不指定 id,会自动生成一个 id

es.create(index="my_index",doc_type="test_type",id=1,body={"name":"python","addr":"深圳"})
 

删除指定的index、type、id的文档

es.delete(index='indexName', doc_type='typeName', id=1)
 

删除index

es.indices.delete(index='news', ignore=[400, 404])
 
query = {'query': {'match_all': {}}}# 查找所有文档
query1 = {'query': {'match': {'sex': 'famale'}}}# 删除性别为女性的所有文档
query2 = {'query': {'range': {'age': {'lt': 11}}}}# 删除年龄小于11的所有文档
query3 = {'query': {'term': {'name': 'jack'}}}# 查找名字叫做jack的所有文档
 
 

删除所有文档

es.delete_by_query(index="my_index",doc_type="test_type",body=query)
 

get:获取指定index、type、id所对应的文档

es.get(index="my_index",doc_type="test_type",id=1)
 

search:查询满足条件的所有文档,没有id属性,且index,type和body均可为None

result = es.search(index="my_index",doc_type="test_type",body=query)
print(result'hits'[0])# 返回第一个文档的内容
 

update:更新指定index、type、id所对应的文档

更新的主要点:

1. 需要指定 id

2. body={"doc": } , 这个doc是必须的

es.update(index="my_index",doc_type="test_type",id=1,body={"doc":{"name":"python1","addr":"深圳1"}})


---查询所有数据
 # 搜索所有数据
es.search(index="my_index",doc_type="test_type")
 

或者

body = {
    "query":{
        "match_all":{}
    }
}
es.search(index="my_index",doc_type="test_type",body=body)

---term与terms

term

body = {
    "query":{
        "term":{
            "name":"python"
        }
    }
}

查询name="python"的所有数据

es.search(index="my_index",doc_type="test_type",body=body)
 
 

terms

body = {
    "query":{
        "terms":{
            "name":[
                "python","android"
            ]
        }
    }
}

搜索出name="python"或name="android"的所有数据

es.search(index="my_index",doc_type="test_type",body=body)

---match与multi_match

match:匹配name包含python关键字的数据

body = {
    "query":{
        "match":{
            "name":"python"
        }
    }
}

查询name包含python关键字的数据

es.search(index="my_index",doc_type="test_type",body=body)
 

multi_match:在name和addr里匹配包含深圳关键字的数据

body = {
    "query":{
        "multi_match":{
            "query":"深圳",
            "fields":["name","addr"]
        }
    }
}

查询name和addr包含"深圳"关键字的数据

es.search(index="my_index",doc_type="test_type",body=body)

---ids
body = {
    "query":{
        "ids":{
            "type":"test_type",
            "values":[
                "1","2"
            ]
        }
    }
}

搜索出id为1或2d的所有数据

es.search(index="my_index",doc_type="test_type",body=body)

---复合查询bool
body = {
    "query":{
        "bool":{
            "must":[
                {
                    "term":{
                        "name":"python"
                    }
                },
                {
                    "term":{
                        "age":18
                    }
                }
            ]
        }
    }
}

获取name="python"并且age=18的所有数据

es.search(index="my_index",doc_type="test_type",body=body)

---切片式查询
body = {
    "query":{
        "match_all":{}
    }
    "from":2    # 从第二条数据开始
    "size":4    # 获取4条数据
}

从第2条数据开始,获取4条数据

es.search(index="my_index",doc_type="test_type",body=body)

---范围查询

body = {
    "query":{
        "range":{
            "age":{
                "gte":18,       # >=18
                "lte":30        # <=30
            }
        }
    }
}

查询18<=age<=30的所有数据

es.search(index="my_index",doc_type="test_type",body=body)

---前缀查询
body = {
    "query":{
        "prefix":{
            "name":"p"
        }
    }
}

查询前缀为"赵"的所有数据

es.search(index="my_index",doc_type="test_type",body=body)

---通配符查询

body = {
    "query":{
        "wildcard":{
            "name":"*id"
        }
    }
}

查询name以id为后缀的所有数据

es.search(index="my_index",doc_type="test_type",body=body)

---排序
body = {
    "query":{
        "match_all":{}
    }
    "sort":{
        "age":{                 # 根据age字段升序排序
            "order":"asc"       # asc升序,desc降序
        }
    }
}

---filter_path
响应过滤

只需要获取_id数据,多个条件用逗号隔开

es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"])
 

获取所有数据

es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"])

---count
执行查询并获取该查询的匹配数

获取数据量

es.count(index="my_index",doc_type="test_type")

---度量类聚合
获取最小值
body = {
    "query":{
        "match_all":{}
    },
    "aggs":{                        # 聚合查询
        "min_age":{                 # 最小值的key
            "min":{                 # 最小
                "field":"age"       # 查询"age"的最小值
            }
        }
    }
}

搜索所有数据,并获取age最小的值

es.search(index="my_index",doc_type="test_type",body=body)

---获取最大值
body = {
    "query":{
        "match_all":{}
    },
    "aggs":{                        # 聚合查询
        "max_age":{                 # 最大值的key
            "max":{                 # 最大
                "field":"age"       # 查询"age"的最大值
            }
        }
    }
}

搜索所有数据,并获取age最大的值

es.search(index="my_index",doc_type="test_type",body=body)

---获取和
body = {
    "query":{
        "match_all":{}
    },
    "aggs":{                        # 聚合查询
        "sum_age":{                 # 和的key
            "sum":{                 # 和
                "field":"age"       # 获取所有age的和
            }
        }
    }
}

搜索所有数据,并获取所有age的和

es.search(index="my_index",doc_type="test_type",body=body)

---获取平均值
body = {
    "query":{
        "match_all":{}
    },
    "aggs":{                        # 聚合查询
        "avg_age":{                 # 平均值的key
            "sum":{                 # 平均值
                "field":"age"       # 获取所有age的平均值
            }
        }
    }
}

搜索所有数据,获取所有age的平均值

es.search(index="my_index",doc_type="test_type",body=body)

目录
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8076 2
|
11天前
|
域名解析 人工智能 Linux
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10205 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1533 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2692 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2326 0