【问题标题】:Handle singular and plural search terms in Azure Cognitive Search在 Azure 认知搜索中处理单复数搜索词
【发布时间】:2021-06-06 11:04:05
【问题描述】:

我们使用 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 认知搜索能够区分单复数单词会更好。

更新: 自定义分析器可能是我的问题的答案,我只需要找到正确的令牌过滤器。

更新: 下面是我的自定义分析器。它删除了 html 结构、撇号、停用词并将它们转换为小写。分词器是 MicrosoftLanguageStemmingTokenizer,它将单词简化为根词,因此它适合复数到单数的场景(搜索“风景”返回“风景”和“风景”)

"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”。

【问题讨论】:

    标签: c# azure search azure-cognitive-search


    【解决方案1】:

    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"
            }
        ]
    }
    

    【讨论】:

    • 嗨,我正在试用语言分析器。我会让你知道结果。
    • 使用 en.microsoft 分析器似乎可以解决问题。感谢您的帮助!
    猜你喜欢
    • 2022-01-24
    • 2020-03-27
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2020-09-01
    • 2021-09-21
    • 1970-01-01
    相关资源
    最近更新 更多