python 进行es查询

在python中,要连接elasticsearch 必须先安装:elasticsearch

pip install elasticsearch

创建elasticsearch 连接实例

#encoding:utf-8
from elasticsearch import Elasticsearch

#创建es连接实例
es = Elasticsearch(["http://192.168.11.128:9200"])

创建es索引

#创建es索引
# es.indices.create(index="account")

向es索引中写入数据

# 向es索引中写入数据
body_value={
    "id":4,
    "account_number" : "000000",
    "firstname" : "jcTang",
    "lastname" : "Tang",
    "age" : 29,
    "gender" : "1",
    "phone":18926968869,
    "address" : "深圳市南山区xx路xx号xx房",
    "email" : "jcTang1512022@163.com",
    "city" : "上海",
    "state" : "0"
}
es.index(index="account",doc_type="test-type",ignore=404,body=body_value)

查询es索引下的数据:match_all

query={
    "query":{
        "match_all":{}
    }
}
value = es.search(index="account",body=query)
print(value)

查询结果:

{
    'took': 1,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 5,
            'relation': 'eq'
        },
        'max_score': 1.0,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7exk7YEBLWmmfdpNGSu5',
            '_score': 1.0,
            '_source': {
                'id': 1,
                'account_number': '000000',
                'firstname': 'jcTang',
                'lastname': 'Tang',
                'age': 29,
                'gender': '0',
                'phone': 15100000000,
                'address': '深圳市南山区xx路xx号xx房',
                'email': 'jctang2022@euron.com',
                'city': '深圳',
                'state': '1'
            }
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7uxn7YEBLWmmfdpNmSuf',
            '_score': 1.0,
            '_source': {
                'id': 2,
                'account_number': '000001',
                'firstname': 'lilei',
                'lastname': 'lei',
                'age': 25,
                'gender': '0',
                'phone': 15900326519,
                'address': '深圳市宝安区xx路xx号xx房',
                'email': 'lilei159@gmail.com',
                'city': '深圳',
                'state': '1'
            }
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '2',
            '_score': 1.0,
            '_source': {
                'id': 3,
                'account_number': '000002',
                'firstname': 'wangfang',
                'lastname': 'wang',
                'age': 27,
                'gender': '1',
                'phone': 13156968869,
                'address': '北京市昌平区xx路xx号xx房',
                'email': 'wangfang@163.com',
                'city': '北京',
                'state': '0'
            }
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '3',
            '_score': 1.0,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房',
                'email': 'wei89548@163.com',
                'city': '上海',
                'state': '0'
            }
        }]
    }
}
指定查询某个条件的数据
#查询指定数据
query={
    "query":{
        "match":{
            '_id': '3'
        }
    }
}
value = es.search(index="account",body=query)
print(value)

查询结果

{
    'took': 11,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 1,
            'relation': 'eq'
        },
        'max_score': 1.0,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '3',
            '_score': 1.0,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房',
                'email': 'wei89548@163.com',
                'city': '上海',
                'state': '0'
            }
        }]
    }
}
查询全部并升序排序
# 查询全部并升序排序
query={
    "query":{
        "match_all":{}
    },
    "sort":[
        {"id":"asc"}
    ]
}
asc_value = es.search(index="account",body=query)
print(asc_value)

查询结果:

{
    'took': 3,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 5,
            'relation': 'eq'
        },
        'max_score': None,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7exk7YEBLWmmfdpNGSu5',
            '_score': None,
            '_source': {
                'id': 1,
                'account_number': '000000',
                'firstname': 'jcTang',
                'lastname': 'Tang',
                'age': 29,
                'gender': '0',
                'phone': 15100000000,
                'address': '深圳市南山区xx路xx号xx房',
                'email': 'jctang2022@euron.com',
                'city': '深圳',
                'state': '1'
            },
            'sort': [1]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7uxn7YEBLWmmfdpNmSuf',
            '_score': None,
            '_source': {
                'id': 2,
                'account_number': '000001',
                'firstname': 'lilei',
                'lastname': 'lei',
                'age': 25,
                'gender': '0',
                'phone': 15900326519,
                'address': '深圳市宝安区xx路xx号xx房',
                'email': 'lilei159@gmail.com',
                'city': '深圳',
                'state': '1'
            },
            'sort': [2]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '2',
            '_score': None,
            '_source': {
                'id': 3,
                'account_number': '000002',
                'firstname': 'wangfang',
                'lastname': 'wang',
                'age': 27,
                'gender': '1',
                'phone': 13156968869,
                'address': '北京市昌平区xx路xx号xx房',
                'email': 'wangfang@163.com',
                'city': '北京',
                'state': '0'
            },
            'sort': [3]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '3',
            '_score': None,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房 ',
                'email ': 'wei89548 @163.com ',
                'city ': '上海 ',
                'state ': '0 '
            },
            'sort ': [4]
        }]
    }
}
查询全部并降序排序
#查询全部并降序排序
query={
    "query":{
        "match_all":{}
    },
    "sort":[
        {"id":"desc"}
    ]
}
desc_value = es.search(index="account",body=query)
print(desc_value)

查询结果:

{
    'took': 19,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 5,
            'relation': 'eq'
        },
        'max_score': None,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '3',
            '_score': None,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房',
                'email': 'wei89548@163.com',
                'city': '上海',
                'state': '0'
            },
            'sort': [4]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '2',
            '_score': None,
            '_source': {
                'id': 3,
                'account_number': '000002',
                'firstname': 'wangfang',
                'lastname': 'wang',
                'age': 27,
                'gender': '1',
                'phone': 13156968869,
                'address': '北京市昌平区xx路xx号xx房',
                'email': 'wangfang@163.com',
                'city': '北京',
                'state': '0'
            },
            'sort': [3]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7uxn7YEBLWmmfdpNmSuf',
            '_score': None,
            '_source': {
                'id': 2,
                'account_number': '000001',
                'firstname': 'lilei',
                'lastname': 'lei',
                'age': 25,
                'gender': '0',
                'phone': 15900326519,
                'address': '深圳市宝安区xx路xx号xx房',
                'email': 'lilei159@gmail.com',
                'city': '深圳',
                'state': '1'
            },
            'sort': [2]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7exk7YEBLWmmfdpNGSu5',
            '_score': None,
            '_source': {
                'id': 1,
                'account_number': '000000',
                'firstname': 'jcTang',
                'lastname': 'Tang',
                'age': 29,
                'gender': '0',
                'phone': 15100000000,
                'address': '深圳市南山区xx路xx号xx房',
                'email': 'jctang2022@euron.com',
                'city': '深圳',
                'state': '1'
            },
            'sort': [1]
        }]
    }
}
先排序再分页查询
# 先排序再分页查询
query={
    "query":{
        "match_all":{}
    },
    "sort":[
        {"id":"desc"}
    ],
    "from":1,
    "size":2,
}

size_value = es.search(index="account",body=query)
print(size_value)

查询结果:from 分页开始位置,size从开始位置起返回多少条数据

{
    'took': 5,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 5,
            'relation': 'eq'
        },
        'max_score': None,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7-yL7YEBLWmmfdpN7ytZ',
            '_score': None,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房',
                'email': 'wei89548@163.com',
                'city': '上海',
                'state': '0'
            },
            'sort': [4]
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '2',
            '_score': None,
            '_source': {
                'id': 3,
                'account_number': '000002',
                'firstname': 'wangfang',
                'lastname': 'wang',
                'age': 27,
                'gender': '1',
                'phone': 13156968869,
                'address': '北京市昌平区xx路xx号xx房',
                'email': 'wangfang@163.com',
                'city': '北京',
                'state': '0'
            },
            'sort': [3]
        }]
    }
}
短语搜索match_phrase
# 短语搜索match_phrase
query={
    "query":{
        "match_phrase":{
            "age":29
        }
    }
}
match_phrase_value = es.search(index="account",body=query)
print(match_phrase_value)

查询结果:

{
    'took': 4,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 3,
            'relation': 'eq'
        },
        'max_score': 1.0,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7exk7YEBLWmmfdpNGSu5',
            '_score': 1.0,
            '_source': {
                'id': 1,
                'account_number': '000000',
                'firstname': 'jcTang',
                'lastname': 'Tang',
                'age': 29,
                'gender': '0',
                'phone': 15100000000,
                'address': '深圳市南山区xx路xx号xx房',
                'email': 'jctang2022@euron.com',
                'city': '深圳',
                'state': '1'
            }
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '3',
            '_score': 1.0,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房',
                'email': 'wei89548@163.com',
                'city': '上海',
                'state': '0'
            }
        }, {
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7-yL7YEBLWmmfdpN7ytZ',
            '_score': 1.0,
            '_source': {
                'id': 4,
                'account_number': '000004',
                'firstname': 'weiwu',
                'lastname': 'wei',
                'age': 29,
                'gender': '1',
                'phone': 18926968869,
                'address': '上海市黄浦区xx路xx号xx房',
                'email': 'wei89548@163.com',
                'city': '上海',
                'state': '0'
            }
        }]
    }
}
多条件查找bool:must 必须满足条件,must_not 不满足条件
#多条件查找bool
query={
    "query":{
        "bool":{
            "must":[
                {"match":{"age":29}}
            ],
            "must_not":[
                {"match":{'city':"上海"}}
            ]
        }
    }
}
bool_value = es.search(index="account",body=query)
print(bool_value)

查询结果:

{
    'took': 50,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 1,
            'relation': 'eq'
        },
        'max_score': 1.0,
        'hits': [{
            '_index': 'account',
            '_type': 'test-type',
            '_id': '7exk7YEBLWmmfdpNGSu5',
            '_score': 1.0,
            '_source': {
                'id': 1,
                'account_number': '000000',
                'firstname': 'jcTang',
                'lastname': 'Tang',
                'age': 29,
                'gender': '0',
                'phone': 15100000000,
                'address': '深圳市南山区xx路xx号xx房',
                'email': 'jctang2022@euron.com',
                'city': '深圳',
                'state': '1'
            }
        }]
    }
}
过滤器filter
# 过滤器filter
# 查找年龄在27~30之间
qurey={
    "query":{
        "bool":{
            "must":{"match_all":{}},
            "filter":{
                "range":{
                    "age":{
                        "gte":27,
                        "lte":30
                    }
                }
            }
            
        }
    }
}
filter_value = es.search(index="account",body=qurey)
print(filter_value)

查询结果:

{
    'took': 1,
    'timed_out': False,
    '_shards': {
        'total': 1,
        'successful': 1,
        'skipped': 0,
        'failed': 0
    },
    'hits': {
        'total': {
            'value': 4,
            'relation': 'eq'
        },
        'max_score': 1.0,
        'hits': [{
                '_index': 'account',
                '_type': 'test-type',
                '_id': '7exk7YEBLWmmfdpNGSu5',
                '_score': 1.0,
                '_source': {
                    'id': 1,
                    'account_number': '000000',
                    'firstname': 'jcTang',
                    'lastname': 'Tang',
                    'age': 29,
                    'gender': '0',
                    'phone': 15100000000,
                    'address': '深圳市南山区xx路xx号xx房',
                    'email': 'jctang2022@euron.com',
                    'city': '深圳',
                    'state': '1'
                }
            }, {
                '_index': 'account',
                '_type': 'test-type',
                '_id': '2',
                '_score': 1.0,
                '_source': {
                    'id': 3,
                    'account_number': '000002',
                    'firstname': 'wangfang',
                    'lastname': 'wang',
                    'age': 27,
                    'gender': '1',
                    'phone': 13156968869,
                    'address': '北京市昌平区xx路xx号xx房 ',
                    '
                    'email ': 'wangfang @163.com ',
                    '
                    'city ': '北京 ',
                    'state ': '0 '
                }
            }
        }
    }]
}
}
根据条件删除指定数据
#根据条件删除指定数据
query={
    "query":{
        "match":{
            '_id': '1'
        }
    }
}
es.delete_by_query(index="account",body=query)

 

参考来自:https://www.shuzhiduo.com/A/VGzlDZX8zb/

posted @ 2022-07-11 23:32  西夏一品唐  阅读(5493)  评论(0编辑  收藏  举报