python - elasticsearch + python在字符串数组中搜索子字符串
问题描述
我在弹性搜索中有 10k+ 条记录。其中一个字段(部门)以数组的形式保存数据
例如记录是
{
"username": "tom",
"dept": [
"cust_service",
"sales_rpr",
"store_in",
],
"location": "NY"
}
{
"username": "adam",
"dept": [
"cust_opr",
"floor_in",
"mg_cust_opr",
],
"location": "MA"
}
.
.
.
我想在dept
字段上进行自动完成,如果用户搜索cus
它应该返回
["cust_service", "cust_opr", "mg_cust_opr"]
最佳匹配在顶部
我已查询
query = {
"_source": [],
"size": 0,
"min_score": 0.5,
"query": {
"bool": {
"must": [
{
"wildcard": {
"dept": {
"value": "*cus*"
}
}
}
],
"filter": [],
"should": [],
"must_not": []
}
},
"aggs": {
"auto_complete": {
"terms": {
"field": f"dept.raw",
"size": 20,
"order": {"max_score": 'desc'}
},
"aggs": {
"max_score": {
"avg": {"script": "_score"}
}
}
}
}
}
它没有给出 ["cust_service", "cust_opr", "mg_cust_opr"]
而是给出与搜索键(cus)无关的其他答案。但是当字段只是string
而不是array
它时,它会按预期给出结果。
我该如何解决这个问题?
提前致谢!
解决方案
推荐阅读
- python - request.GET() 无法从给定的输入中获取数据。我该如何解决这个问题?
- javascript - 每个只返回第一个对象字符串的 Javascript JSON
- javascript - 如何在 JavaScript 中将 UUID/GUID 转换为 OID/DICOM UID?
- c++ - 派生类的切片是如何发生的?
- jmespath - 使用 JMESPath,根据数组值属性的长度过滤数组元素
- jar - weblogic wseeclient.jar 在从 weblogic 服务器迁移到 jboss 时,我想要一个用于 JBOSS 的替代 jar
- ruby - 在数组Ruby的第一个元素中创建逗号分隔的字符串
- ios - 键盘卡在 ios 13 模拟器中
- azure-devops - Azure DevOps Api - 获取剩余工作日
- javascript - 编辑表单时来自本地存储的复选框值