- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试使用ElasticSearch查询一个索引而不查询另一个索引时,我不断收到此错误:
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'failed to
create query: {\n "multi_match" : {\n "query" : "bob",\n "fields" : [\n
"additional_comments^1.0",\n "age^1.0",\n "first_name^1.0",\n "gender^1.0",\n
"needs^1.0",\n "program_number^1.0",\n "read^1.0",\n "sheet_size^1.0",\n
"shoe_size_category^1.0",\n "shoe_sock_size^1.0",\n "shoe_type^1.0",\n
"time_chosen^1.0",\n "wants^1.0",\n "wear^1.0"\n ],\n "type" : "best_fields",\n
"operator" : "OR",\n "slop" : 0,\n "fuzziness" : "50",\n "prefix_length" : 0,\n
"max_expansions" : 50,\n "zero_terms_query" : "NONE",\n "auto_generate_synonyms_phrase_query" :
true,\n "fuzzy_transpositions" : true,\n "boost" : 1.0\n }\n}')
def query_object(index, fields, query, page, per_page, fuzziness=0):
print(index)
print(query)
print(fields)
search = current_app.elasticsearch.search(
index=index,
body={'query': {'multi_match': {'query': query, 'fields': fields, 'fuzziness': fuzziness}},
'from': (page - 1) * per_page, 'size': per_page}
)
ids = [int(hit['_id']) for hit in search['hits']['hits']]
return ids, search['hits']['total']['value']
class SearchableMixin(object):
@classmethod
def search_object(cls, fields, expression, page, per_page, fuzziness=0):
ids, total = query_object(
cls.__tablename__, fields, expression, page, per_page, fuzziness=fuzziness)
if total == 0:
return cls.query.filter_by(id=0), 0
when = []
for i in range(len(ids)):
when.append((ids[i], i))
return cls.query.filter(cls.id.in_(ids)).order_by(
db.case(when, value=cls.id)), total
class User(db.Model, UserMixin, SearchableMixin):
__searchable__ = ['email', 'phone', 'street_address',
'city', 'state', 'zip_code', 'last_reminded']
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(100), unique=True)
phone = db.Column(db.String(20), nullable=True, default=None)
password = db.Column(db.String(200))
street_address = db.Column(db.String(100))
city = db.Column(db.String(100))
state = db.Column(db.String(52))
zip_code = db.Column(db.String(10))
last_reminded = db.Column(db.String(40), default=datetime.utcnow().strftime(
"%A, %B %e{}, %Y".format(suffix(datetime.today().day))))
admin = db.Column(db.Boolean, default=False)
wish_lists = db.relationship('WishList', backref='sponsor', lazy='dynamic')
drives = db.relationship(
'HolidayCheerDrive', secondary=years, lazy='dynamic')
class WishList(db.Model, SearchableMixin):
__searchable__ = ['program_number', 'first_name', 'age', 'gender', 'wants', 'needs', 'wear',
'read', 'shoe_sock_size', 'shoe_size_category', 'shoe_type', 'sheet_size', 'additional_comments', 'time_chosen',
]
id = db.Column(db.Integer, primary_key=True)
program_number = db.Column(db.String(4))
first_name = db.Column(db.String(20))
age = db.Column(db.String(10))
gender = db.Column(db.String(20))
wants = db.Column(db.String(300))
needs = db.Column(db.String(300))
wear = db.Column(db.String(300))
read = db.Column(db.String(300))
pant_dress_size = db.Column(db.String(20), default='unspecified')
shirt_blouse_size = db.Column(db.String(20), default='unspecified')
jacket_sweater_size = db.Column(db.String(20), default='unspecified')
shoe_sock_size = db.Column(db.String(20), default='unspecified')
shoe_size_category = db.Column(db.String(20), default='unspecified')
shoe_type = db.Column(db.String(50), nullable=True, default=None)
sheet_size = db.Column(db.String(20))
additional_comments = db.Column(db.Text(), nullable=True, default=None)
time_chosen = db.Column(db.String(40), nullable=True, default=None)
sponsor_id = db.Column(db.Integer, db.ForeignKey(
'user.id'), nullable=True, default=None)
drive_id = db.Column(db.Integer, db.ForeignKey(
'holiday_cheer_drive.id'), nullable=False, default=None)
time_chosen
可搜索的术语,因为它尚未定义,但这没有帮助。我也尝试仅搜索单个字段,但也没有用。当我搜索用户时,它不会返回任何错误。据我所知,python路由似乎没有任何错误,它们是:
@bp.route('/manage_users', methods=['GET', 'POST'])
#@admin_only
def manage_users():
form = SearchUsersForm()
page = request.args.get('page', 1, type=int)
if not form.validate():
all_users = User.query.all()
total = len(all_users)
start = ((page-1) * current_app.config['RESULTS_PER_PAGE'])
end = (start + current_app.config['RESULTS_PER_PAGE'])
if end >= total:
display_users = all_users[start:]
else:
display_users = all_users[start:end]
else:
if form.fields.data == 'default':
fields = [field for field in User.__searchable__]
elif form.fields.data == 'address':
fields = ['street_address', 'city', 'state', 'zip_code']
else:
fields = [form.fields.data]
fuzzy = True
if fuzzy:
fuzziness = 50
else:
fuzziness = 0
display_users, total = User.search_object(fields, form.q.data, page,
current_app.config['RESULTS_PER_PAGE'], fuzziness=fuzziness)
q = form.q.data if form.q.data else None
next_url = url_for('admin.manage_users', q=q, page=page + 1) \
if total > page * current_app.config['RESULTS_PER_PAGE'] else None
prev_url = url_for('admin.manage_users', q=q, page=page - 1) \
if page > 1 else None
context = {
'next_url': next_url,
'prev_url': prev_url,
'display_users': display_users,
'form': form,
}
return render_template('/admin/manage_users.html', **context)
@bp.route('/manage_wish_lists', methods=['GET', 'POST'])
#@admin_only
def manage_wish_lists():
form = SearchWishListsForm()
page = request.args.get('page', 1, type=int)
if not form.validate():
all_wish_lists = WishList.query.all()
total = len(all_wish_lists)
start = ((page-1) * current_app.config['RESULTS_PER_PAGE'])
end = (start + current_app.config['RESULTS_PER_PAGE'])
if end >= total:
display_lists = all_wish_lists[start:]
else:
display_lists = all_wish_lists[start:end]
else:
if form.fields.data == 'default':
fields = [field for field in WishList.__searchable__]
elif form.fields.data == 'wish_list_items':
fields = ['wants', 'need', 'wear', 'read']
elif form.fields.data == "shoes":
fields = ['shoe_sock_size', 'shoe_size_category', 'shoe_type']
else:
fields = [form.fields.data]
fuzzy = True
if fuzzy:
fuzziness = 50
else:
fuzziness = 0
display_lists, total = WishList.search_object(fields, form.q.data, page,
current_app.config['RESULTS_PER_PAGE'], fuzziness=fuzziness)
q = form.q.data if form.q.data else None
next_url = url_for('admin.manage_wish_lists', q=q, page=page + 1) \
if total > page * current_app.config['RESULTS_PER_PAGE'] else None
prev_url = url_for('admin.manage_wish_lists', q=q, page=page - 1) \
if page > 1 else None
context = {
'next_url': next_url,
'prev_url': prev_url,
'display_lists': display_lists,
'form': form,
}
return render_template('/admin/manage_wish_lists.html', **context)
print(index)
print(query)
print(fields)
import json
body={'query': {'multi_match': {'query': query, 'fields': fields, 'fuzziness': fuzziness}},
'from': (page - 1) * per_page, 'size': per_page}
print(json.dumps(body))
search = current_app.elasticsearch.search(
index=index,
body={'query': {'multi_match': {'query': query, 'fields': fields, 'fuzziness': fuzziness}},
'from': (page - 1) * per_page, 'size': per_page}
)
print(json.dumps(search))
print
语句的编辑输出
#INDEX NAME
user
#QUERY TERMS
bob
#SEARCH FIELDS
['email', 'phone', 'street_address', 'city', 'state', 'zip_code', 'last_reminded']
#BODY OF SEARCH
{"query": {"multi_match": {"query": "bob", "fields": ["email", "phone", "street_address", "city", "state", "zip_code", "last_reminded"], "fuzziness": 50}}, "from": 0, "size": 10}
#SEARCH JSON
{"took": 18, "timed_out": false, "_shards": {"total": 1, "successful": 1, "skipped": 0, "failed": 0}, "hits": {"total": {"value": 0, "relation": "eq"}, "max_score": null, "hits": []}}
#INDEX NAME
wish_list
#QUERY TERMS
bob
#SEARCH FIELDS
['program_number', 'first_name', 'age', 'gender', 'wants', 'needs', 'wear', 'read', 'shoe_sock_size', 'shoe_size_category', 'shoe_type', 'sheet_size', 'additional_comments', 'time_chosen']
#BODY OF SEARCH
{"query": {"multi_match": {"query": "bob", "fields": ["program_number", "first_name", "age", "gender", "wants", "needs", "wear", "read", "shoe_sock_size", "shoe_size_category", "shoe_type", "sheet_size", "additional_comments", "time_chosen"], "fuzziness": 50}}, "from": 0, "size": 10}
#Cannot build query so search object never instantiates
additional_comments
是文本列而不是字符串列;这有可能发挥作用吗?
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-7.6.1.jar:7.6.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[?:?]
at java.lang.Thread.run(Thread.java:830) ~[?:?]
[2020-03-23T09:34:43,437][DEBUG][o.e.a.s.TransportSearchAction] [LAPTOP-G8TTIC4C] All shards failed for phase: [query]
org.elasticsearch.index.query.QueryShardException: failed to create query: {
"multi_match" : {
"query" : "bob",
"fields" : [
"additional_comments^1.0",
"age^1.0",
"first_name^1.0",
"gender^1.0",
"needs^1.0",
"program_number^1.0",
"read^1.0",
"sheet_size^1.0",
"shoe_size_category^1.0",
"shoe_sock_size^1.0",
"shoe_type^1.0",
"time_chosen^1.0",
"wants^1.0",
"wear^1.0"
],
"type" : "best_fields",
"operator" : "OR",
"slop" : 0,
"fuzziness" : "50",
"prefix_length" : 0,
"max_expansions" : 50,
"zero_terms_query" : "NONE",
"auto_generate_synonyms_phrase_query" : true,
"fuzzy_transpositions" : true,
"boost" : 1.0
}
}
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:350) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:333) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:750) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.search.SearchService.createContext(SearchService.java:591) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:550) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:351) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.search.SearchService.lambda$executeQueryPhase$1(SearchService.java:343) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.action.ActionListener.lambda$map$2(ActionListener.java:146) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:63) [elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:58) [elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:73) [elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:44) [elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:692) [elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) [elasticsearch-7.6.1.jar:7.6.1]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
at java.lang.Thread.run(Thread.java:830) [?:?]
Caused by: java.lang.IllegalArgumentException: Can only use fuzzy queries on keyword and text fields - not on [shoe_sock_size] which is of type [long]
at org.elasticsearch.index.mapper.MappedFieldType.fuzzyQuery(MappedFieldType.java:356) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.search.MatchQuery$MatchQueryBuilder.lambda$newTermQuery$0(MatchQuery.java:569) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.search.MatchQuery$MatchQueryBuilder.newTermQuery(MatchQuery.java:579) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.search.MatchQuery.parse(MatchQuery.java:277) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.search.MultiMatchQuery.buildFieldQueries(MultiMatchQuery.java:108) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.search.MultiMatchQuery.parse(MultiMatchQuery.java:76) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.query.MultiMatchQueryBuilder.doToQuery(MultiMatchQueryBuilder.java:833) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:99) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.query.QueryShardContext.lambda$toQuery$1(QueryShardContext.java:334) ~[elasticsearch-7.6.1.jar:7.6.1]
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:346) ~[elasticsearch-7.6.1.jar:7.6.1]
... 17 more
最佳答案
我发现,当我将搜索字段的数据类型显式设置为“文本”时,在所有不搜索类似数字的字段的情况下,查询都可以正常构建。以前,所有字段都无法正确构建。
我已将SearchableMixin
类中的索引类函数设置为以下内容:
def add_to_index(index, model):
payload = {}
properties = {}
for field in model.__searchable__:
payload[field] = getattr(model, field)
properties[field] = {'type': 'text'}
payload['mappings'] = {'properties': properties}
current_app.elasticsearch.index(index=index, id=model.id, body=payload)
program_number
,
shoe_sock_size
和
age
都是整数或强制转换为字符串的浮点数)。我将研究或询问另一个问题来解决该问题。
关于python - Flask应用程序中的ElasticSearch RequestError:查询仅适用于两个模型之一(search_phase_execution_exception,创建查询失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60805908/
我正在通过 labrepl 工作,我看到了一些遵循此模式的代码: ;; Pattern (apply #(apply f %&) coll) ;; Concrete example user=> (a
我从未向应用商店提交过应用,但我会在不久的将来提交。 到目前为止,我对为 iPhone 而非 iPad 进行设计感到很自在。 我了解,通过将通用PAID 应用放到应用商店,客户只需支付一次就可以同时使
我有一个应用程序,它使用不同的 Facebook 应用程序(2 个不同的 AppID)在 Facebook 上发布并显示它是“通过 iPhone”/“通过 iPad”。 当 Facebook 应用程序
我有一个要求,我们必须通过将网站源文件保存在本地 iOS 应用程序中来在 iOS 应用程序 Webview 中运行网站。 Angular 需要服务器来运行应用程序,但由于我们将文件保存在本地,我们无法
所以我有一个单页客户端应用程序。 正常流程: 应用程序 -> OAuth2 服务器 -> 应用程序 我们有自己的 OAuth2 服务器,因此人们可以登录应用程序并获取与用户实体关联的 access_t
假设我有一个安装在用户设备上的 Android 应用程序 A,我的应用程序有一个 AppWidget,我们可以让其他 Android 开发人员在其中以每次安装成本为基础发布他们的应用程序推广广告。因此
Secrets of the JavaScript Ninja中有一个例子它提供了以下代码来绕过 JavaScript 的 Math.min() 函数,该函数需要一个可变长度列表。 Example:
当我分别将数组和对象传递给 function.apply() 时,我得到 NaN 的 o/p,但是当我传递对象和数组时,我得到一个数字。为什么会发生这种情况? 由于数组也被视为对象,为什么我无法使用它
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章ASP转换格林威治时间函数DateDiff()应用由作者收集整理,如果你
我正在将列表传递给 map并且想要返回一个带有合并名称的 data.frame 对象。 例如: library(tidyverse) library(broom) mtcars %>% spl
我有一个非常基本的问题,但我不知道如何实现它:我有一个返回数据框,其中每个工具的返回值是按行排列的: tmp<-as.data.frame(t(data.frame(a=rnorm(250,0,1)
我正在使用我的 FB 应用创建群组并邀请用户加入我的应用群组,第一次一切正常。当我尝试创建另一个组时,出现以下错误: {"(OAuthException - #4009) (#4009) 在有更多用户
我们正在开发一款类似于“会说话的本”应用程序的 child 应用程序。它包含大量用于交互式动画的 JPEG 图像序列。 问题是动画在 iPad Air 上播放正常,但在 iPad 2 上播放缓慢或滞后
我关注 clojure 一段时间了,它的一些功能非常令人兴奋(持久数据结构、函数式方法、不可变状态)。然而,由于我仍在学习,我想了解如何在实际场景中应用,证明其好处,然后演化并应用于更复杂的问题。即,
我开发了一个仅使用挪威语的应用程序。该应用程序不使用本地化,因为它应该仅以一种语言(挪威语)显示。但是,我已在 Info.plist 文件中将“本地化 native 开发区域”设置为“no”。我还使用
读完 Anthony's response 后上a style-related parser question ,我试图说服自己编写单体解析器仍然可以相当紧凑。 所以而不是 reference ::
multicore 库中是否有类似 sapply 的东西?还是我必须 unlist(mclapply(..)) 才能实现这一点? 如果它不存在:推理是什么? 提前致谢,如果这是一个愚蠢的问题,我们深表
我喜欢在窗口中弹出结果,以便更容易查看和查找(例如,它们不会随着控制台继续滚动而丢失)。一种方法是使用 sink() 和 file.show()。例如: y <- rnorm(100); x <- r
我有一个如下所示的 spring mvc Controller @RequestMapping(value="/new", method=RequestMethod.POST) public Stri
我正在阅读 StructureMap关于依赖注入(inject),首先有两部分初始化映射,具体类类型的接口(interface),另一部分只是实例化(请求实例)。 第一部分需要配置和设置,这是在 Bo
我是一名优秀的程序员,十分优秀!