【问题标题】:How to allow wildcards for custom analyzer in Azure Search如何在 Azure 搜索中允许自定义分析器使用通配符
【发布时间】:2020-03-08 09:16:30
【问题描述】:

提前感谢您的帮助。

我正在使用 Azure Search .Net SDK 来构建索引器。我目前也在使用自定义分析器

在使用自定义分析器之前,我使用的是 EnLucene 分析器,它允许我使用通配符搜索 *. 比如我是用来让用户搜索后缀搜索的。如果用户搜索“app”,它会返回“apple, application, approach”等结果。请不要建议自动完成或建议,因为建议器不能与自定义分析器一起使用。我不想创建 仅因为建议者而增加了 20 个搜索字段。 (一个用于建议者,一个用于搜索)。

以下是我的自定义分析器示例。它不允许我使用 * 进行部分匹配。我不是在寻找任何前缀或后缀部分匹配的 NGram 解决方案。我实际上想使用通配符 *.我该怎么做才能允许通配符搜索?

var definition = new Index()
{
    Name = indexName,
    Fields = mapFields,
    Analyzers = new[]
    {
        new CustomAnalyzer
        {
            Name = "custom_analyzer",
            Tokenizer = TokenizerName.Whitespace,
            TokenFilters = new[]
            {
                TokenFilterName.AsciiFolding,
                TokenFilterName.Lowercase,
                TokenFilterName.Phonetic
            }
        }
    }
};

【问题讨论】:

    标签: c# azure azure-cognitive-search analyzer azure-search-.net-sdk


    【解决方案1】:

    您可以这样做:

    • 添加您的自定义分析器,如下所示:

    {
      "name":"names",
      "fields":[
        { "name":"id", "type":"Edm.String", "key":true, "searchable":false },
        { "name":"name", "type":"Edm.String", "analyzer":"my_standard" }
      ],
      "analyzers":[
        {
          "name":"my_standard",
          "@odata.type":"#Microsoft.Azure.Search.CustomAnalyzer",
          "tokenizer":"standard",
          "tokenFilters":[ "lowercase", "asciifolding" ]
        }
      ]
    }
    
    // Below snippet is for creating definition using c#
    new CustomAnalyzer
                    {
                        Name = "custom_analyzer",
                        Tokenizer = TokenizerName.Standard,
                        TokenFilters = new[]
                        {
                            TokenFilterName.Lowercase,
                            TokenFilterName.AsciiFolding,
                            TokenFilterName.Phonetic
                        }
                    }
    • 然后在创建文档定义时引用自定义分析器,如下所示:

        [IsSearchable, IsFilterable, IsSortable, Analyzer("custom_analyzer")]
        public string Property { get; set; }

    查看此博客以获取更多参考:

    https://azure.microsoft.com/en-in/blog/custom-analyzers-in-azure-search/

    这是自定义分析仪的示例测试方法:

    [Fact]
            public void CanSearchWithCustomAnalyzer()
            {
                Run(() =>
                {
                    const string CustomAnalyzerName = "my_email_analyzer";
                    const string CustomCharFilterName = "my_email_filter";
    
                    Index index = new Index()
                    {
                        Name = SearchTestUtilities.GenerateName(),
                        Fields = new[]
                        {
                            new Field("id", DataType.String) { IsKey = true },
                            new Field("message", (AnalyzerName)CustomAnalyzerName) { IsSearchable = true }
                        },
                        Analyzers = new[]
                        {
                            new CustomAnalyzer()
                            {
                                Name = CustomAnalyzerName,
                                Tokenizer = TokenizerName.Standard,
                                CharFilters = new[] { (CharFilterName)CustomCharFilterName }
                            }
                        },
                        CharFilters = new[] { new PatternReplaceCharFilter(CustomCharFilterName, "@", "_") }
                    };
    
                    Data.GetSearchServiceClient().Indexes.Create(index);
    
                    SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);
    
                    var documents = new[]
                    {
                        new Document() { { "id", "1" }, { "message", "My email is someone@somewhere.something." } },
                        new Document() { { "id", "2" }, { "message", "His email is someone@nowhere.nothing." } },
                    };
    
                    indexClient.Documents.Index(IndexBatch.Upload(documents));
                    SearchTestUtilities.WaitForIndexing();
    
                    DocumentSearchResult<Document> result = indexClient.Documents.Search("someone@somewhere.something");
    
                    Assert.Equal("1", result.Results.Single().Document["id"]);
                });
            }
    

    欢迎在对话中标记我,希望对您有所帮助。

    【讨论】:

    • 嗨莫希特。非常感谢您的回答。您似乎正在使用标准标记器。除了标准 Lucene 分析器之外,我是否可以在任何其他标记器中使用正则表达式搜索?如果是这样,我应该如何指定允许在除 Standard 之外的任何其他标记器中进行正则表达式搜索?
    • Mohit,看起来在您的单元测试中,您指定了一个完整的令牌来搜索“someone@somewhere.something”。相反,如果您搜索值“some”,您实际上会返回两条记录吗?在尝试遵循您的示例时,当使用原始帖子中提到的 Kyle 自定义分析器时,我没有得到部分匹配返回。您不能再将星号与自定义分析器一起使用,因此无法识别部分匹配。在博客中,他们使用 edgeNGram 方法通过一个单独的字段来解决它。你认为这是解决部分比赛挑战的唯一方法吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2021-10-04
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多