admin 管理员组文章数量: 887021
2024年1月5日发(作者:eclipse怎么打开jsp文件)
环境
•
•
•
•
前端:html,css,js,jQuery,bootstrap
后端:flask
搜索引擎:elasticsearch
数据源:某某之家
项目展示
项目目录
主要源码
获取数据源并写入es
from lxml import etree
from s import ThreadPoolExecutor
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import requests
headers = {
'user-agent': 'ua'
}
es = Elasticsearch()
if not (index='car'):
(index='car', mappings={
'properties': {
'url': {
'type': 'text'
},
'img': {
'type': 'text'
},
'title': {
'type': 'text'
},
'desc': {
'type': 'text'
}
}
})
def task(url,page):
res = (url, headers)
text =
tree = (text)
ul_list = ('//ul[@class="article"]')
actions = []
for ul in ul_list:
li_list = ('./li')
for li in li_list:
url = ('./a/@href'),
img = ('./a/div/img/@src'),
desc = ('./a/p/text()'),
title = ('./a/h3/text()')
if title:
doc = {
'_index': 'car',
'url': f'https:{url[0][0]}',
'img': img[0][0],
'desc': desc[0][0],
'title': title[0],
}
(doc)
(es, actions=actions)
print(f'第{page}页完成!')
def main():
with ThreadPoolExecutor() as pool:
for i in range(1, 11):
url = f'/all/{i}/'
(task, url=url,page=i)
if __name__ == '__main__':
main()
视图函数
from flask import Blueprint
from flask import request
from flask import render_template
from flask import jsonify
from import es
from pprint import pprint
search_bp = Blueprint('search', __name__, url_prefix='/search')
@search_('/', methods=['get', 'post'])
def search():
if == 'GET':
return render_template('')
elif == 'POST':
content = ('content')
size = 10
current = int(('current', '0'))
if content:
res = (index='car', query={
'match': {
"title": content
}
}, highlight={
"pre_tags": ""
,
"post_tags": ""
,
"fields": {
"title": {}
}
}, size=1000)
else:
res = (index='car', query={
'match_all': {}
}, size=1000)
new_res = res['hits']['hits']
total = int(res['hits']['total']['value'])
need_page = (total // size) + 1
data = {
'res': new_res[current * size:current * size + size],
'need_page': need_page,
'total': total
}
return jsonify(data)
前端
General为您找到相关结果约0个
app配置
from flask import Flask
from flask_session import Session
from import db
from . import search_bp
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
def create_app():
app = Flask(__name__)
_object('pmentConfig')
er_blueprint(search_bp)
Session(app)
_app(app)
app = Manager(app)
Migrate(app, db)
_command('db', MigrateCommand)
return app
总结
对比django,flask最后选用自由度较大的flask。
集合flask-script,flask_sqlalchemy,flask-migrate加快开发。
写一个小demo深化了对es接口的理解。
版权声明:本文标题:es+flask搜索小项目实现分页+高亮的示例代码 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1704418749h458912.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论