csharp Azure搜索服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Azure搜索服务相关的知识,希望对你有一定的参考价值。

string searchServiceName = "rich-01"

// If possible, share a single instance of SearchServiceClient in your application to avoid opening too many connections.
// Class methods are thread-safe to enable such sharing.
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials("111B0E42F3ExxxxxxxxxA4884DF58"));

// Model class
public partial class Hotel
{
\!h     [System.ComponentModel.DataAnnotations.Key]
    [IsFilterable]
    public string HotelId { get; set; }

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

    [IsSearchable]
\!h     [Analyzer(AnalyzerName.AsString.EnMicrosoft)]
    public string Description { get; set; }

\!h     [IsSearchable, IsFilterable, IsSortable, IsFacetable]
    public string Category { get; set; }
    
    // ... etc.
}

string indexName = "SearchIndexName";

// Create index
var definition = new Index()
{
    Name = indexName,
    Fields = FieldBuilder.BuildForType<Hotel>()
};
serviceClient.Indexes.Create(definition);

// Delete index
if (serviceClient.Indexes.Exists(indexName))
{
    serviceClient.Indexes.Delete(indexName);
}
ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);

var actions = new IndexAction<Hotel>[]
{
    IndexAction.Upload(
        new Hotel()
        {
            HotelId = "1",
            HotelName = "Secret Point Motel"
            // More fields
        };
    ),
    // More hotels to upload
};

var batch = IndexBatch.New(actions);
indexClient.Documents.Index(batch);
ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);

// Search on term 'boutique'
// Sort by rating in descending order, taking the top two results
// Returning only these fields: HotelId, HotelName, Category, Rating
SearchParameters parameters =
    new SearchParameters()
    {
        OrderBy = new[] { "Rating desc" },
        Select = new[] { "HotelId", "HotelName", "Category", "Rating" },
        Top = 2
    };
DocumentSearchResult<Hotel> results = indexClient.Documents.Search<Hotel>("boutique", parameters);

以上是关于csharp Azure搜索服务的主要内容,如果未能解决你的问题,请参考以下文章

csharp Azure搜索同义词:https://github.com/Azure-Samples/search-dotnet-getting-started/tree/master/DotNetH

csharp 在Migraged Azure移动服务应用程序上启用应用程序洞察

最新的Azure SQL数据库实例中的Azure搜索服务与全文搜索?

当搜索包含特殊字符时,Azure 搜索服务筛选器 search.ismatch() 未返回正确结果

csharp Azure事件中心

csharp Azure Batch