如何在Azure搜索中允许通配符用于自定义分析器

问题描述 投票:0回答:1

谢谢您的帮助。

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

[使用自定义分析器之前,我使用的是EnLucene分析器,它使我可以使用通配符搜索*。例如,我曾经用来允许用户搜索后缀搜索。如果用户搜索“应用程序”,它将返回诸如“苹果,应用程序,方法”之类的结果。请不要建议自动完成或建议,因为建议者不能与自定义分析器一起使用。我不想创建仅由于建议者而增加了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个回答
0
投票

这是您可以执行的操作:

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

{
  "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 [email protected]." } },
                    new Document() { { "id", "2" }, { "message", "His email is [email protected]." } },
                };

                indexClient.Documents.Index(IndexBatch.Upload(documents));
                SearchTestUtilities.WaitForIndexing();

                DocumentSearchResult<Document> result = indexClient.Documents.Search("[email protected]");

                Assert.Equal("1", result.Results.Single().Document["id"]);
            });
        }

随意在您的对话中标记我,希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.