我们使用 Azure 认知搜索作为搜索图像的搜索引擎。该分析器是 Lucene 标准,当用户搜索“苏格兰风景”时,我们的一些用户声称他们的图像丢失了。然后,他们必须在图像中添加关键字“风景”,以便搜索引擎可以找到它们。
将分析器更改为“en-lucene”或“en-microsoft”似乎只会获得更小的搜索结果,我们的用户不喜欢这样。
Azure 认知搜索似乎不区分单数和复数单词。为了解决这个问题,我在数据库中创建了一个字典,使用了词形变化并尝试操作搜索词:
foreach (var term in terms)
{
if (ps.IsSingular(term))
{
// check with db
var singular = noun.GetSingularWord(term);
if (!string.IsNullOrEmpty(singular))
{
var plural = ps.Pluralize(term);
keywords = keywords + " " + plural;
}
}
else
{
// check with db
var plural = noun.GetPluralWord(term);
if (!string.IsNullOrEmpty(plural))
{
var singular = ps.Singularize(term);
keywords = keywords + " " + singular;
}
}
}
我的解决方案并不是 100% 理想,但如果 Azure 认知搜索能够区分单数和复数单词就更好了。
更新: 自定义分析器可能是我问题的答案,我只需要找到正确的 token 过滤器。
更新: 下面是我的自定义分析器。它删除了 html 结构、撇号、停用词并将它们转换为小写。分词器是 MicrosoftLanguageStemmingTokenizer,它将单词减少为其根词,因此它适合复数到单数的场景(搜索“landscapes”返回“landscapes”和“landscape”)
"analyzers": [
{
"name": "p4m_custom_analyzer",
"@odata.type": "#Microsoft.Azure.Search.CustomAnalyzer",
"charFilters": [
"html_strip",
"remove_apostrophe"
],
"tokenizer": "custom_tokenizer",
"tokenFilters": [
"lowercase",
"remove_stopwords"
]
}
],
"charFilters": [
{
"name": "remove_apostrophe",
"@odata.type":"#Microsoft.Azure.Search.MappingCharFilter",
"mappings": ["'=>"]
}
],
"tokenizers": [
{
"name": "custom_tokenizer",
"@odata.type":"#Microsoft.Azure.Search.MicrosoftLanguageStemmingTokenizer",
"isSearchTokenizer": "false"
}
],
"tokenFilters": [
{
"name": "remove_stopwords",
"@odata.type": "#Microsoft.Azure.Search.StopwordsTokenFilter"
}
]
我还没有想出相反的办法。如果用户搜索“apple”,它应该返回“apple”和“apples”。
最佳答案
en.lucene 和 en.microsoft 都应该对此有所帮助,您不需要手动扩展您这边的变化。我很惊讶地听到你对他们的内存减少了。一般来说,我希望这些分析仪比标准分析仪有更高的召回率。您是否有使用不同分析器的多个可搜索字段?那可能会干扰。否则,最好看到一个特定的案例(查询/文档对以及索引定义)来进一步调查。
作为快速测试,我使用了这个小索引定义:
{
"name": "inflections",
"fields": [
{
"name": "id",
"type": "Edm.String",
"searchable": false,
"filterable": true,
"retrievable": true,
"sortable": false,
"facetable": false,
"key": true
},
{
"name": "en_ms",
"type": "Edm.String",
"searchable": true,
"filterable": false,
"retrievable": true,
"sortable": false,
"facetable": false,
"key": false,
"analyzer": "en.microsoft"
}
]
}
这些文档:
{
"id": "1",
"en_ms": "example with scottish landscape as part of the sentence"
},
{
"id": "2",
"en_ms": "this doc has one apple word"
},
{
"id": "3",
"en_ms": "this doc has two apples in it"
}
对于此搜索search=landscapes
,我看到以下结果:
{
"value": [
{
"@search.score": 0.9631388,
"id": "1",
"en_ms": "example with scottish landscape as part of the sentence"
}
]
}
对于 search=apple
我看到:
{
"value": [
{
"@search.score": 0.51188517,
"id": "3",
"en_ms": "this doc has two apples in it"
},
{
"@search.score": 0.46152657,
"id": "2",
"en_ms": "this doc has one apple word"
}
]
}
关于c# - 处理 Azure 认知搜索中的单数和复数搜索词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66531968/