【发布时间】:2020-09-02 12:38:20
【问题描述】:
我正在用烧瓶和 Elasticsearch 做一个项目。用户通过 url 将查询参数传递给 elasticsearch 以执行搜索。我目前有两个字段:短语和日期。
@app.route('/search')
def get_search_article():
phrase = request.args.get('phrase')
from_date = request.args.get('from')
to_date = request.args.get('to')
doc = {
"query": {
"bool": {
"must": [
{
"match": {
"title": phrase
}
}
],
"filter": [
{
"range": {
"pubDate": {
"gte": from_date + ' 00:00:00',
"lte": to_date + ' 23:59:59'
}
}
}
]
}
}
}
我想知道有没有办法,如果用户没有通过url传递值,例如短语的url,elasticsearch查询可以遍历所有标题值。我实现的解决方案是使用 ifs 检查值是否已填充,并对每个 if 进行不同的查询。但是当我为查询实现更多参数时,代码变得非常大。
【问题讨论】:
-
您可以考虑使用elasticsearch dsl (elasticsearch-dsl.readthedocs.io/en/latest),它将帮助您减少编写查询的逻辑量!
标签: python elasticsearch flask