Python:Elasticsearch中的通配符查询 [英] Python : wildcard query in Elasticsearch
问题描述
我想在Elasticsearch中使用通配符查询字段,但是问题是搜索字符串存储在变量中,并且不能静态使用.预期的查询是:
I want to query a field using wildcard in Elasticsearch but the problem is that the search string is stored in a variable and not available statically. The intended query is :
body={"query": {"wildcard": {"Name": { "value" : "Vi?????" }}}}
其中搜索字符串"Vi"在编译时不可用.它将由用户给出.假设它存储在某个变量str(="Vi")中.我应该如何使用str和?"来制定查询?
where the search string "Vi" is not available at compile time. It will be given by user. Say it is stored in some variable str (= "Vi"). How should I formulate a query using str and "?"s ?
推荐答案
您必须在 python
中使用 +
串联.
并且您需要转义双引号.有很多方法可以做到这一点.我更喜欢将双引号转义为\"
And you need to escape double quotes. There are many ways to do this. I prefer escaping double quotes as \"
searchWord ="Vi";
query = "{\"query\": {\"wildcard\": {\"Name\": { \"value\" : \"" + searchWord + "?????\" }}}}";
print (query);
searchWord
是您从用户那里收到的东西.我对其进行了硬编码.
searchWord
is something that you receive from the user. I hardcoded it.
query
是您需要形成的查询.这就是我形成它的方式+ searchWord +" ????? \"
query
is the one you need to form. This is how I formed it \"" + searchWord + "?????\"
请检查并提供我需要考虑的任何信息.
Please check and provide any information which I need to consider.
searchWord ="Vi";
x = 2;
query = "{\"query\": {\"wildcard\": {\"Name\": { \"value\" : \"" + "[a-z]{" + str(x) +"}" + searchWord + "?????\" }}}}";
print (query);
这篇关于Python:Elasticsearch中的通配符查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!