我有一个情况,我需要做基于多匹配的ElasticSearch。
情况1:关键字字段的短语搜索。应该只返回完整短语匹配的结果(不应该拆分短语)。情况2:对于剩余字段,1个关键字匹配必须返回结果。
我使用should组合了多匹配和术语查询。如下。
例如:如果我用“短语搜索多个字段”
首先,它应该在关键字字段中搜索完整的短语,然后它可以在其他字段的多匹配中搜索。注意:如果我用单个词搜索,如“多个”或“字段”,它不应该从关键字返回任何结果。
"search": {
"method": "POST",
"param": {
"query": {
"bool": {
"should": [
{
"terms": {
"keywords": [
"Phrase search in multiple fields",
" Phrase search in multiple fields"
]
}
},
{
"multi_match": {
"query": "Phrase search in multiple fields",
"fields": {
"title": "title",
"metaTitle": "title",
"href": "Path",
},
"fuzziness": "AUTO"
}
}
]
}
}
}
1条答案
按热度按时间q0qdq0h21#
案例一:短语搜索关键字字段。应仅返回完整短语匹配的结果(不应拆分短语)
使用
keyword
字段类型。不应该拆分短语=>如果你使用
keyword
字段类型,这意味着没有文本分析器,它不会拆分短语。案例二:对于其余字段,必须返回1个关键字匹配的结果。
标准分析仪将
使用
text
字段类型。1关键字匹配必须返回结果=>标准(默认)分析器使用空格(和一些特殊字符)标记短语并将其拆分为单词。
我分享了一些例子来深入解释,希望有帮助。