【问题标题】:Azure Search .NET SDK Custom AnalyzerAzure 搜索 .NET SDK 自定义分析器
【发布时间】:2018-01-23 17:55:42
【问题描述】:

没有太多背景,这是我的问题:

要使用 C# 中的 .NET SDK 创建新的 Azure 搜索索引(使用文档中提供的酒店示例),我的代码如下所示:

public class Hotel
{
    [System.ComponentModel.DataAnnotations.Key]
    [IsFilterable]
    public string HotelId { get; set; }

    [IsFilterable, IsSortable, IsFacetable]
    public double? BaseRate { get; set; }

    [IsSearchable]
    public string Description { get; set; }

    [IsSearchable]
    [Analyzer(AnalyzerName.AsString.FrLucene)]
    [JsonProperty("description_fr")]
    public string DescriptionFr { get; set; }

    [IsSearchable, IsFilterable, IsSortable]
    public string HotelName { get; set; }

    [IsSearchable, IsFilterable, IsSortable, IsFacetable]
    public string Category { get; set; }

    [IsSearchable, IsFilterable, IsFacetable]
    public string[] Tags { get; set; }

    [IsFilterable, IsFacetable]
    public bool? ParkingIncluded { get; set; }

    [IsFilterable, IsFacetable]
    public bool? SmokingAllowed { get; set; }

    [IsFilterable, IsSortable, IsFacetable]
    public DateTimeOffset? LastRenovationDate { get; set; }

    [IsFilterable, IsSortable, IsFacetable]
    public int? Rating { get; set; }

    [IsFilterable, IsSortable]
    public GeographyPoint Location { get; set; }
}

private static void CreateHotelsIndex(ISearchServiceClient serviceClient)
    {
        var definition = new Index
        {
            Name = "hotels",
            Fields = FieldBuilder.BuildForType<Hotel>()
        };

        serviceClient.Indexes.Create(definition);
    }

这很好用。

使用 .NET SDK 进行搜索时会出现问题。前缀搜索工作正常

var results = indexClient.Documents.Search<Hotel>("cheap*");

将返回所有带有以“便宜”开头的字符串的文档,但我需要一个 string.Contains() 类型的功能,或者至少是后缀搜索。我正在尝试做类似的事情

var results = indexClient.Documents.Search<Hotel>("*heap*");

在任意位置获取包含字符串“heap”的所有结果。

我知道可以使用自定义分析器执行此操作,但这些分析器只能通过 Azure 搜索 REST API 创建和应用,并且只能在创建索引时进行。这使得我上面提供的几乎所有内容都无法使用,因为我必须通过 Postman 在 JSON 中定义我的“Hotels”索引、字段和分析器,而 SDK 实际上只对查询有用。这也意味着我需要在我创建的每个索引中重复定义相同的自定义分析器,因为 Azure 搜索似乎不支持全局分析器定义。

所以这里的问题是:有没有办法在 C# 中定义一个自定义分析器,我可以在创建时引用并应用于我的索引?或者,真的,有没有一种简单的方法可以仅使用 .NET SDK 获得完整的通配符支持?

【问题讨论】:

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


    【解决方案1】:

    你可以这样做:

    private static void CreateHotelsIndex(ISearchServiceClient serviceClient)
    {
        var definition = new Index
        {
            Name = "hotels",
            Fields = FieldBuilder.BuildForType<Hotel>(),
            Analyzers = new[]
            {
                new CustomAnalyzer
                {
                    Name = "my_analyzer",
                    Tokenizer = TokenizerName.Standard,
                    TokenFilters = new[]
                    {
                        TokenFilterName.Lowercase,
                        TokenFilterName.AsciiFolding,
                        TokenFilterName.Phonetic
                    }
                }
            }
        };
    
        serviceClient.Indexes.Create(definition);
    }
    

    ...然后在文档定义中引用自定义分析器:

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

    有关更多信息,请参阅 Custom analyzers in Azure Search 博客文章和来自 API 单元测试的示例 CustomAnalyzerTests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-04
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 2020-12-26
      • 2020-03-08
      相关资源
      最近更新 更多