elasticsearch - 按子串匹配
问题描述
我需要获取Description
字段包含 substring的文档28/859
。这是我的查询:
{
"explain": true,
"query": {
"bool":{
"filter":{
"bool":{"should":[{"query_string":{"default_field":"Description","query":"28//859",
"analyzer": "keyword"}}]}},
"must_not":{"exists":{"field":"ParentId"}}}
}
}
我得到的文件是:
28/859
在Description
现场(没关系)28
但我也会在现场获得文件Description
(我不需要它)
如何28/859
仅获取带有子字符串的文档?
更新:解释示例:
{
"_shard": "[tech_places][4]",
"_node": "u7QI_gjjRXy4-xdqnK2KMw",
"_index": "tech_places",
"_type": "entity",
"_id": "8403",
"_score": 0.0,
"_source": {
"Id": 8403,
"Name": "RETE-43424",
"Description": "SRF-10kv №28 VISO",
"ParentId": null,
"OrganizationId": 12,
"OrganizationPath": "12_27",
"LocationId": 27,
"Classification": "",
"Type": "A",
"Status": 0,
"MaintenanceObjectId": null,
"TreePath": "8403"
},
"_explanation": {
"value": 0.0,
"description": "sum of:",
"details": [
{
"value": 0.0,
"description": "match on required clause, product of:",
"details": [
{
"value": 0.0,
"description": "# clause",
"details": []
},
{
"value": 0.0,
"description": "sum of:",
"details": [
{
"value": 0.0,
"description": "weight(Description:28 in 35112) [], result of:",
"details": [
{
"value": 0.0,
"description": "score(doc=35112,freq=1.0), with freq of:",
"details": [
{
"value": 1.0,
"description": "termFreq=1.0",
"details": []
}
]
}
]
}
]
}
]
}
]
}
},
解决方案
您可以whitespace
为此使用分析器。使用如下属性创建/更新您的映射description
:
{
"description": {
"type": "text",
"analyzer": "whitespace"
}
}
这将确保像 28/859 这样的东西被视为单个令牌。您甚至可以使用正则表达式创建自己的自定义标记器/分析器。然后您可以使用下面的查询来获得所需的结果:
{
"query": {
"query_string": {
"default_field": "description",
"query": "28\\/859"
}
}
}
推荐阅读
- javascript - 来自 ArrayBuffer 的普通浮点数组
- nginx - 来自 Kubernetes 中 ConfigMap 的自定义 nginx.conf
- tabulator - 在制表符中排序
- entity-framework-core - 如何获取在 HasComment 中设置的 EF Core 模型配置实体属性注释作为 html 标题属性
- python - Matplotlib 错误:可变参数关键字参数不能有默认值
- javascript - 单击时将js代码应用于多个li标签而不为每个标签编写相同的js代码(li)
- swift - 尝试删除行时,带有绑定视图的 ForEach 崩溃
- c++ - 无法在异步 lambda 中访问 std::vector*
- laravel - 我正在研究如何在 .env 文件中安全地添加数据库连接变量,但通过 laravel 中的刀片
- python - 如何使用 pytest 使发布请求异步?