Skip to content

Instantly share code, notes, and snippets.

@hhyyg
Last active April 22, 2017 08:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save hhyyg/ad303bb9415fc3d393dbe893d54555cf to your computer and use it in GitHub Desktop.
#define HowToExample
using System;
using System.Configuration;
using System.Linq;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Spatial;
namespace AzureSearch.SDKHowToSynonyms
{
class Program
{
// This sample shows how to delete, create, upload documents and query an index with a synonym map
static void Main(string[] args)
{
string input = Console.ReadLine();
if (input == "setup")
{
CleanupAndSetup();
}
else if (input == "search")
{
Search();
}
else if (input == "update")
{
UploadSynonyms(CreateSearchServiceClient());
}
Console.WriteLine("{0}", "Complete. Press any key to end application...\n");
Console.ReadKey();
}
static void Search()
{
ISearchIndexClient indexClientForQueries = CreateSearchIndexClient();
RunQueriesWithNonExistentTermsInIndex(indexClientForQueries);
}
static void CleanupAndSetup()
{
SearchServiceClient serviceClient = CreateSearchServiceClient();
Console.WriteLine("{0}", "Cleaning up resources...\n");
CleanupResources(serviceClient);
Console.WriteLine("{0}", "Creating index...\n");
CreateHotelsIndex(serviceClient);
ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("hotels");
Console.WriteLine("{0}", "Uploading documents...\n");
UploadDocuments(indexClient);
ISearchIndexClient indexClientForQueries = CreateSearchIndexClient();
RunQueriesWithNonExistentTermsInIndex(indexClientForQueries);
Console.WriteLine("{0}", "Adding synonyms...\n");
UploadSynonyms(serviceClient);
EnableSynonymsInHotelsIndex(serviceClient);
}
private static SearchServiceClient CreateSearchServiceClient()
{
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
string adminApiKey = ConfigurationManager.AppSettings["SearchServiceAdminApiKey"];
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
return serviceClient;
}
private static SearchIndexClient CreateSearchIndexClient()
{
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
string queryApiKey = ConfigurationManager.AppSettings["SearchServiceQueryApiKey"];
SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, "hotels", new SearchCredentials(queryApiKey));
return indexClient;
}
private static void CleanupResources(SearchServiceClient serviceClient)
{
if (serviceClient.Indexes.Exists("hotels"))
{
serviceClient.Indexes.Delete("hotels");
}
if (serviceClient.SynonymMaps.Exists("desc-synonymmap"))
{
serviceClient.SynonymMaps.Delete("desc-synonymmap");
}
}
private static void CreateHotelsIndex(SearchServiceClient serviceClient)
{
var definition = new Index()
{
Name = "hotels",
Fields = FieldBuilder.BuildForType<Hotel>()
};
serviceClient.Indexes.Create(definition);
}
private static void EnableSynonymsInHotelsIndex(SearchServiceClient serviceClient)
{
Index index = serviceClient.Indexes.Get("hotels");
index.Fields.First(f => f.Name == "category").SynonymMaps = new[] { "desc-synonymmap" };
index.Fields.First(f => f.Name == "tags").SynonymMaps = new[] { "desc-synonymmap" };
serviceClient.Indexes.CreateOrUpdate(index);
}
private static void UploadSynonyms(SearchServiceClient serviceClient)
{
var synonymMap = new SynonymMap()
{
Name = "desc-synonymmap",
Format = "solr",
Synonyms = "インターネット,internet,ワイファイ,wifi\nリッチ,five star=>高級\n旅亭,旅荘=>旅館"
};
serviceClient.SynonymMaps.CreateOrUpdate(synonymMap);
Console.WriteLine("UploadSynonyms");
}
private static void UploadDocuments(ISearchIndexClient indexClient)
{
var hotels = new Hotel[]
{
new Hotel()
{
HotelName = "東京ホテル",
Category = "ホテル",
Tags = new[] { "ジャグジー", "夜景", "wifi", "送迎", "高級"},
HotelId = "1",
BaseRate = 199.0,
Description = "Best hotel in town",
DescriptionFr = "Meilleur hôtel en ville",
ParkingIncluded = false,
SmokingAllowed = false,
LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.Zero),
Rating = 5,
Location = GeographyPoint.Create(47.678581, -122.131577)
},
new Hotel()
{
HotelName = "神奈川旅館",
Category = "旅館",
Tags = new[] { "温泉", "内湯", "大浴場", "露天風呂"},
HotelId = "2",
BaseRate = 79.99,
Description = "Cheapest hotel in town",
DescriptionFr = "Hôtel le moins cher en ville",
ParkingIncluded = true,
SmokingAllowed = true,
LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero),
Rating = 1,
Location = GeographyPoint.Create(49.678581, -122.131577)
},
new Hotel()
{
HotelId = "3",
BaseRate = 129.99,
Description = "Close to town hall and the river"
}
};
var batch = IndexBatch.Upload(hotels);
try
{
indexClient.Documents.Index(batch);
}
catch (IndexBatchException e)
{
// Sometimes when your Search service is under load, indexing will fail for some of the documents in
// the batch. Depending on your application, you can take compensating actions like delaying and
// retrying. For this simple demo, we just log the failed document keys and continue.
Console.WriteLine(
"Failed to index some of the documents: {0}",
String.Join(", ", e.IndexingResults.Where(r => !r.Succeeded).Select(r => r.Key)));
}
Console.WriteLine("Waiting for documents to be indexed...\n");
Thread.Sleep(2000);
}
private static void RunQueriesWithNonExistentTermsInIndex(ISearchIndexClient indexClient)
{
SearchParameters parameters;
DocumentSearchResult<Hotel> results;
Console.WriteLine("Search with terms nonexistent in the index:\n");
parameters =
new SearchParameters()
{
SearchFields = new[] { "category", "tags" },
Select = new[] { "hotelName", "category", "tags" },
};
Console.WriteLine("1. Search the entire index for the phrase \"five star\":\n");
results = indexClient.Documents.Search<Hotel>("\"five star\"", parameters);
WriteDocuments(results);
Console.WriteLine("2. Search the entire index for the term 'インターネット':\n");
results = indexClient.Documents.Search<Hotel>("インターネット", parameters);
WriteDocuments(results);
Console.WriteLine("3. Search the entire index for the term '旅亭':\n");
results = indexClient.Documents.Search<Hotel>("旅亭", parameters);
WriteDocuments(results);
Console.WriteLine("4. Search the entire index for the terms 'インターネット' AND 'five star':\n");
results = indexClient.Documents.Search<Hotel>("インターネット AND five star", parameters);
WriteDocuments(results);
Console.WriteLine("5. Search the entire index for the term 'リッチ':\n");
results = indexClient.Documents.Search<Hotel>("リッチ", parameters);
WriteDocuments(results);
}
private static void WriteDocuments(DocumentSearchResult<Hotel> searchResults)
{
if (searchResults.Results.Count != 0)
{
foreach (SearchResult<Hotel> result in searchResults.Results)
{
Console.WriteLine(result.Document);
}
}
else
{
Console.WriteLine("no document matched");
}
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment