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);