按子串匹配 [英] Match by substring
本文介绍了按子串匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要获取 Description
字段包含子字符串 28/859
的文档.这是我的查询:
I need to get the documents where Description
field contains substring 28/859
. Here is my query:
{
"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
in theDescription
field (it's fine)- but I alse get documents with
28
in theDescription
field (i don't need it)
如何获取仅包含子字符串 28/859
的文档?
How can I get the documents with substring 28/859
only ?
更新:说明示例:
{
"_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
属性创建/更新您的映射,如下所示:
You can use whitespace
analyzer for this. Create/Update your mapping with description
property as below:
{
"description": {
"type": "text",
"analyzer": "whitespace"
}
}
这将确保将类似28/859的内容视为单个令牌.您甚至可以使用正则表达式创建自己的自定义标记生成器/分析器.然后,您可以使用下面的查询来获取所需的结果:
This will make sure that something like 28/859 is treated as s single token. You can even created your own custom tokenizer/analyzer using regex. Then you can use the query below to get required result:
{
"query": {
"query_string": {
"default_field": "description",
"query": "28\\/859"
}
}
}
这篇关于按子串匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文