python - 弹性搜索 DSL (Python) 动态字段映射
问题描述
我有一些字段存储在字典中,如下所示:
type_specific_attributes = {
"color": "red",
"fabric": "velvet",
"material": "leather",
"measurements": "1x2x3",
"size": "Medium",
"style": "Gucci"
}
我希望生成以下 ES 查询,其中该字典的键是动态映射的:
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"color": "red"
}
},
{
"match_phrase": {
"fabric": "velvet"
}
},
... // and so forth for all the keys in my dictionary
]
}
}
}
我尝试将所有查询添加到列表并将该列表传递给 .query() 函数:
# Append all Queries to a list
should = []
for attribute in type_specific_attributes:
should.append(Q("match_phrase", attribute=type_specific_attributes[attribute]))
# Create an instance of the Search object
s = Search(using=self.client, index=self.index)
# Add Query to Search Object
s = s.query(
Q(
"bool",
should=should
)
)
但生成的查询没有动态字段:
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"attribute": "red" // I'm expecting this to be dynamic, not "attribute"
}
},
... // a bunch of other same objects
]
}
}
}
解决方案
推荐阅读
- c++ - 如何编译运行一个C++开源程序?
- python - 如何使用硒刮 iframe?
- python-3.x - 基于 Pandas 数据框创建表
- python - 读取文件的有效最后一行时出现问题
- javascript - Dojo 中的启动和 postCreate 函数
- python - 如何找到以大写字母开头的字符串中的单词?
- jsx - Tailwind 中带有背景图像的深色变体
- html - 为什么“.html”出现在浏览器中我的网址末尾?
- trains - ClearML(以前的 Trains)可以在本地服务器上工作吗?
- javascript - 第一个 setTimeout 有效,但其他无效