问题描述
我的数据如下:
{
"name": "name1",
"name_gr": ["gr1","gr2"]
},
{
"name": "name2",
"name_gr": ["gr1","gr2"]
},
{
"name": "name3",
"name_gr": ["gr2","gr3"]
},
{
"name": "name4",
"name_gr": ["gr1","gr2","gr3","gr4"]
},
{
"name": "name4",
"name_gr": ["gr4","gr5"]
}
现在,我正在尝试通过 elasticsearch_dsl
python模块通过从其中导入 Q
来编写弹性搜索查询.
Now, i am trying to write elastic search query using elasticsearch_dsl
python module by importing Q
from it.
我的输入查询将是"name_gr"
上的一些 regex
.例如:"name_gr":"gr [12]"
.因此,在这种情况下,我希望所有在"name_gr"列表中同时具有"gr1"和"gr2"的名称.
My input query will be some regex
on "name_gr"
. For eg: "name_gr": "gr[12]"
.So, in this case I want all the names which has both "gr1" and "gr2" in their list of "name_gr".
因此,->
示例1:"name_gr":"gr [12]"
将是:
{
"name": "name1",
"name_gr": ["gr1","gr2"]
},
{
"name": "name2",
"name_gr": ["gr1","gr2"]
},
{
"name": "name4",
"name_gr": ["gr1","gr2","gr3","gr4"]
}
因为gr1,gr2都出现在name1,name2和name4中.
because, gr1 and gr2 both are present in name1, name2 and name4.
示例2:"name_gr":"gr.*"
为:
{
"name": "name4",
"name_gr": ["gr1","gr2","gr3","gr4"]
}
因为所有的gr(gr1,gr2,gr3,gr4)都只出现在name4中.
because, all the gr's (gr1,gr2,gr3,gr4) are present in name4 only.
我尝试如下编写查询:
must = [{'match':{'regexp': {'name_gr': 'gr[12]'}}}]
result = Q('bool', must = must)
但是它也给出了name3,这是无效的,因为gr1和gr2都不存在.请帮忙.从现在开始,我一直陷入困境.
But it gives name3 also in result, which is invalid as both gr1 and gr2 are not present in it. Please help. I am stuck in this from few days now.
谢谢.
推荐答案
在您的示例中,您想使用 regexp
来使用 match
查询.为此,只需使用:
You are using a match
query in your example where you want to be using regexp
instead. To do this just use:
s = Search().query('regexp', name_gr='gr[12]')
s.execute()
希望这会有所帮助!
这篇关于弹性搜索查询从regexp输入参数中查找列表中值的完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!