在Python中使用Elasticsearch

本文介绍了如何在Python中使用Elasticsearch进行数据操作,包括连接Elasticsearch集群,创建索引,插入文档,查询文档,删除文档,以及使用match和bool查询条件进行复杂检索。示例展示了基本的Elasticsearch API用法,适用于数据存储和检索的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Python中使用Elasticsearch

环境

虚拟机

名称IP地址Elasticstack端口Kibana端口
node-1192.168.2.13892005601
node-2192.168.2.1399200/
node-3192.168.2.1409200/
Elasticstack没有用docker安装

Elasticstack版本

在这里插入图片描述

Kibana版本

kibana 8.3.2

Python版本

Python 3.10.2

编辑main.py文件

安装模块

pip install elasticsearch

导入模块

from elasticsearch import Elasticsearch

连接并输出结果

es = Elasticsearch([{'host':'192.168.2.138','port':9200}])#8.0之前用这句
es = Elasticsearch("http://192.168.2.138:9200")#8.x用这句
print(es)
<Elasticsearch(['http://192.168.2.138:9200'])>

创建索引并新增文档

INDEX_NAME = "infomation"
e1 = {
    "first_name":"hua",
    "last_name":"li",
    "age":20,
    "gender":"male",
    "interests":['sport','singing'],
    "about":"I like to build cabinets"
}

response = es.index(index=INDEX_NAME,id = "1",document=e1)
print(response)
{
	'_index': 'infomation',
	'_id': '1',
	'_version': 1,
	'result': 'created',
	'_shards': {
		'total': 2, 'successful': 2,
		'failed': 0
	},
	'_seq_no': 0,
	'_primary_term': 1
}

Kibana中查询 infomation 索引的结果

在这里插入图片描述

Python中查询 student 索引的结果

response = es.get(index=INDEX_NAME,id= "1")
print(response)

{'_index': 'infomation', '_id': '1', '_version': 1, '_seq_no': 0, '_primary_term': 1, 'found': True, '_source': {'first_name': 'hua', 'last_name': 'li', 'age': 20, 'gender': 'male', 'interests': ['sport', 'singing'], 'about': 'I like to build cabinets'}}

Kibana中查询 student 索引的结果

get student/_search

{
        "_index": "student",
        "_id": "1",
        "_score": 1,
        "_source": {
          "num": "1301",
          "first_name": "hua",
          "last_name": "li",
          "age": 20,
          "sex": "male",
          "score": [
            {
              "name": "art",
              "degree": "A"
            },
            {
              "name": "music",
              "degree": "B"
            },
            {
              "name": "math",
              "degree": "D"
            }
          ],
          "about": "I like to build cabinets"
        }
      },
      {
        "_index": "student",
        "_id": "2",
        "_score": 1,
        "_source": {
          "num": "1302",
          "first_name": "xiaoming",
          "last_name": "li",
          "age": 26,
          "sex": "male",
          "score": [
            {
              "name": "art",
              "degree": "C"
            },
            {
              "name": "music",
              "degree": "B"
            },
            {
              "name": "math",
              "degree": "A"
            }
          ],
          "about": "I love to go rock climbing"
        }
      },
      {
        "_index": "student",
        "_id": "3",
        "_score": 1,
        "_source": {
          "num": "1303",
          "first_name": "sara",
          "last_name": "chen",
          "age": 25,
          "sex": "female",
          "score": [
            {
              "name": "art",
              "degree": "D"
            },
            {
              "name": "music",
              "degree": "D"
            },
            {
              "name": "math",
              "degree": "B"
            }
          ],
          "about": "I like to collect rock albums"
        }
      },
      {
        "_index": "student",
        "_id": "4",
        "_score": 1,
        "_source": {
          "num": "1304",
          "first_name": "haha",
          "last_name": "chen",
          "age": 21,
          "sex": "female",
          "score": [
            {
              "name": "art",
              "degree": "B"
            },
            {
              "name": "music",
              "degree": "A"
            },
            {
              "name": "math",
              "degree": "C"
            }
          ],
          "about": "I like to build 1"
        }
      }

再增加一个文档

e2 = {
    "first_name":"lei",
    "last_name":"li",
    "age":29,
    "gender":"female",
    "interests":['music','game'],
    "about":"I love to go rock climbing"
}
response = es.index(index=INDEX_NAME,id = "2" ,document=e2)
print(response)
{'_index': 'infomation', '_id': '2', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}

查询当前已有文档

在这里插入图片描述

删除id为1的文档

response = es.delete(index=INDEX_NAME,id = "1")
print(response)
{'_index': 'infomation', '_id': '1', '_version': 2, 'result': 'deleted', '_shards': {'total': 2, 'successful': 2, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}

再次查看索引

id为1的文档已经被删除

在这里插入图片描述

search方法

查询索引内全部文档

response = es.search(index="student")
print(response)

或者

response = es.search(index="student", query = {"match_all": {}})
print(response)
{'took': 7, 'timed_out': False, '_shards': {'total': 2, 'successful': 2, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 4, 'relation': 'eq'}, 'max_score': 1.0, 'hits': [{'_index': 'student', '_id': '1', '_score': 1.0, '_source': {'num': '1301', 'first_name': 'hua', 'last_name': 'li', 'age': 20, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'A'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'D'}], 'about': 'I like to build cabinets'}}, {'_index': 'student', '_id': '2', '_score': 1.0, '_source': {'num': '1302', 'first_name': 'xiaoming', 'last_name': 'li', 'age': 26, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'C'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'A'}], 'about': 'I love to go rock climbing'}}, {'_index': 'student', '_id': '3', '_score': 1.0, '_source': {'num': '1303', 'first_name': 'sara', 'last_name': 'chen', 'age': 25, 'sex': 'female', 'score': [{'name': 'art', 'degree': 'D'}, {'name': 'music', 'degree': 'D'}, {'name': 'math', 'degree': 'B'}], 'about': 'I like to collect rock albums'}}, {'_index': 'student', '_id': '4', '_score': 1.0, '_source': {'num': '1304', 'first_name': 'haha', 'last_name': 'chen', 'age': 21, 'sex': 'female', 'score': [{'name': 'art', 'degree': 'B'}, {'name': 'music', 'degree': 'A'}, {'name': 'math', 'degree': 'C'}], 'about': 'I like to build 1'}}]}}

查询条件——match

response = es.search(index="student", query = {"match": {"last_name":"li"}})
print(print(response["hits"]["hits"]))
[{'_index': 'student', '_id': '1', '_score': 0.4700036, '_source': {'num': '1301', 'first_name': 'hua', 'last_name': 'li', 'age': 20, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'A'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'D'}], 'about': 'I like to build cabinets'}}, {'_index': 'student', '_id': '2', '_score': 0.4700036, '_source': {'num': '1302', 'first_name': 'xiaoming', 'last_name': 'li', 'age': 26, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'C'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'A'}], 'about': 'I love to go rock climbing'}}]

查询条件——bool、must、should

找出姓李并且年龄大于等于25的

response = es.search(index="student", query = {
    "bool":{
        "must":[
            {
                "match":{"last_name":"li"}}
        ],
        "should":{
            "range":{
                "age":{
                    "gte":25
                }
            }
        }

    }
})
print(response)

发现有一个文档年龄不符合,说明should没生效

{'took': 3, 'timed_out': False, '_shards': {'total': 2, 'successful': 2, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 2, 'relation': 'eq'}, 'max_score': 1.4700036, 'hits': [{'_index': 'student', '_id': '2', '_score': 1.4700036, '_source': {'num': '1302', 'first_name': 'xiaoming', 'last_name': 'li', 'age': 26, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'C'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'A'}], 'about': 'I love to go rock climbing'}}, {'_index': 'student', '_id': '1', '_score': 0.4700036, '_source': {'num': '1301', 'first_name': 'hua', 'last_name': 'li', 'age': 20, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'A'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'D'}], 'about': 'I like to build cabinets'}}]}}
改用嵌套写法
response = es.search(index="student", query = {
    "bool":{
        "must":[
            {
                "match":{"last_name":"li"}},
            {
                "bool":{
                    "should":{
                        "range":{
                            "age":{
                                "gte":25
                            }
                        }
                    }
                }
            }
        ]


    }
})
print(response["hits"]["hits"])

这样就生效了

{'took': 4, 'timed_out': False, '_shards': {'total': 2, 'successful': 2, 'skipped': 0, 'failed': 0}, 'hits': {'total': {'value': 1, 'relation': 'eq'}, 'max_score': 1.4700036, 'hits': [{'_index': 'student', '_id': '2', '_score': 1.4700036, '_source': {'num': '1302', 'first_name': 'xiaoming', 'last_name': 'li', 'age': 26, 'sex': 'male', 'score': [{'name': 'art', 'degree': 'C'}, {'name': 'music', 'degree': 'B'}, {'name': 'math', 'degree': 'A'}], 'about': 'I love to go rock climbing'}}]}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值