from elasticsearch import Elasticsearch
# 创建es 实例
es = Elasticsearch("http://101.42.224.35:9200/")
class ES(object):
"""
es 对象
"""
# 初始化连接一个 Elasticsearch 操作对象
def __init__(self, index_name: str):
self.es = es
self.index_name = index_name
# 根据 ID 获取文档数据
def get_doc(self, uid):
return self.es.get(index=self.index_name, id=uid)
# 插入文档数据
def insert_one(self, doc: dict):
self.es.index(index=self.index_name, body=doc)
def insert_array(self, docs: list):
for doc in docs:
self.es.index(index=self.index_name, body=doc)
# 搜索文档数据 query 字段 count 30 秒
# 三元运算符 :条件成立执行语句,if条件 ,else 条件不成立执行语句
def search(self, query, count: int = 30, fields=None):
fields = fields if fields else ["title", 'pub_date']
dsl = {
"query": {
"multi_match": {
"query": query,
"fields": fields
},
# 'wildcard': {
# 'content': {
# 'value': '*' + query + '*'
# }
# }
},
"highlight": {
"fields": {
"title": {}
}
}
}
match_data = self.es.search(index=self.index_name, body=dsl, size=count)
return match_data
def _search(self, query: dict, count: int = 20, fields=None): # count: 返回的数据大小
results = []
match_data = self.search(query, count, fields)
for hit in match_data['hits']['hits']:
results.append(hit['_source'])
return results
# 添加文档数据
def create_index(self):
if self.es.indices.exists(index=self.index_name) is True:
self.es.indices.delete(index=self.index_name)
self.es.indices.create(index=self.index_name, ignore=400)
# 删除文档数据
def delete_index(self):
try:
self.es.indices.delete(index=self.index_name)
except:
pass
```
数据库内容导入es
```python
"""
将数据库数据导入es
"""
import pymysql
import traceback
from elasticsearch import Elasticsearch
def get_db_data():
# 打开数据库连接(ip/数据库用户名/登录密码/数据库名)
db = pymysql.connect(host="127.0.0.1:3306", user="root", password="admin123",
database="p8_song", charset='utf8')
# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()
# sql 查询语句
sql = "SELECT * FROM course"
# 使用 execute() 方法执行 SQL 查询
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
# 关闭数据库连接
db.close()
return results
def insert_data_to_es():
es = Elasticsearch("http://101.42.224.35:9200/")
# 清空数据
es.indices.delete(index='course')
try:
i = -1
for row in get_db_data():
print(row)
print(row[1], row[2])
i += 1
es.index(index='course', body={
'id': i,
'table_name': 'table_name',
'pid': row[4],
'title': row[5],
'desc': str(row[6]),
})
except:
error = traceback.format_exc()
print("Error: unable to fecth data", error)
if __name__ == "__main__":
insert_data_to_es()
```
调用es
```python
import traceback
# 导入es实例对象
from common.es.es import ES
# 在方法中调用
class CourseSearch(Resource):
def get(self):
#获取前端传递数据
parser = reqparse.RequestParser()
parser.add_argument('q')
args = parser.parse_args()
q = args.get('q')
#尝试es查询
try:
#链接es操作对象>>表名
es = ES(index_name='course')
result = es._search(q, fields=['title', 'desc'])
return marshal(result, course_fields)
except:
error = traceback.format_exc()
print('111111111111', error)
return {'message': error}, 500
es搜索引擎
最新推荐文章于 2024-07-23 22:02:14 发布