public static void UploadDocuments(SearchIndexClient indexClient, string fileId, string fileName, string ocrText, KeyPhraseResult keyPhraseResult) { List<IndexAction> indexOperations = new List<IndexAction>(); var doc = new Document(); doc.Add("fileId", fileId); doc.Add("fileName", fileName); doc.Add("ocrText", ocrText); doc.Add("keyPhrases", keyPhraseResult.KeyPhrases.ToList()); indexOperations.Add(IndexAction.Upload(doc)); try { indexClient.Documents.Index(new IndexBatch(indexOperations)); } 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))); } }
public SearchService(string searchServiceName, string apiKey, string index) { this.searchServiceName = searchServiceName; this.apiKey = apiKey; serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); indexClient = serviceClient.Indexes.GetClient(index); }
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; }
public AzureSearchService(IConfigurationRoot configurationRoot) { var searchServiceName = configurationRoot["AppSettings:SearchServiceName"]; var searchApiKey = configurationRoot["AppSettings:SearchServiceApiKey"]; var tweetIndexName = configurationRoot["AppSettings:TweetIndexName"]; var serviceClient = new SearchServiceClient( searchServiceName, new SearchCredentials(searchApiKey)); _indexClient = serviceClient.Indexes.GetClient(tweetIndexName); }
private static async Task<IEnumerable<ItemViewModel>> DoSearchAsync(string searchString, SearchParameters parameters) { List<ItemViewModel> searchResults = new List<ItemViewModel>(); using (var indexClient = new SearchIndexClient(ServiceName, IndexName, new SearchCredentials(QueryKey))) { var response = await indexClient.Documents.SearchAsync<ItemViewModel>(searchString, parameters); searchResults.AddRange(response.Results.Select(result => result.Document)); } return searchResults; }
public SearchController() { //var appSettings = ConfigurationManager.AppSettings; //var searchServiceName = appSettings["SearchServiceName"]; //var searchServiceApiKey = appSettings["SearchServiceApiKey"]; //var indexName = appSettings["IndexName"]; //var searchServiceName = CloudConfigurationManager.GetSetting("SearchServiceName"); //var searchServiceApiKey = CloudConfigurationManager.GetSetting("SearchServiceApiKey"); //var indexName = CloudConfigurationManager.GetSetting("IndexName"); _searchServiceName = CloudConfigurationManager.GetSetting("SearchServiceName"); _searchServiceApiKey = CloudConfigurationManager.GetSetting("SearchServiceApiKey"); _indexName = CloudConfigurationManager.GetSetting("IndexName"); _searchServiceClient = new SearchServiceClient(_searchServiceName, new SearchCredentials(_searchServiceApiKey)); _searchIndexClient = _searchServiceClient.Indexes.GetClient(_indexName); }
public StorageController() { var azureStorageConnectionString = CloudConfigurationManager.GetSetting("AzureStorage.ConnectionString"); var azureStorageContainerReference = CloudConfigurationManager.GetSetting("AzureStorage.ContainerReference"); _cloudStorageAccount = CloudStorageAccount.Parse(azureStorageConnectionString); _cloudBlobClient = _cloudStorageAccount.CreateCloudBlobClient(); _cloudBlobContainer = _cloudBlobClient.GetContainerReference(azureStorageContainerReference); _searchServiceName = CloudConfigurationManager.GetSetting("SearchServiceName"); _searchServiceApiKey = CloudConfigurationManager.GetSetting("SearchServiceApiKey"); _indexName = CloudConfigurationManager.GetSetting("IndexName"); _searchServiceClient = new SearchServiceClient(_searchServiceName, new SearchCredentials(_searchServiceApiKey)); _searchIndexClient = _searchServiceClient.Indexes.GetClient(_indexName); }
static FeaturesSearch() { try { string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"]; // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient("geonames"); } catch (Exception e) { errorMessage = e.Message.ToString(); } }
static AzureSearchHelper() { try { string searchServiceName = ValidationHelper.GetString(ConfigurationManager.AppSettings["AzureSearchName"], ""); string apiKey = ValidationHelper.GetString(ConfigurationManager.AppSettings["AzureSearchAPIKey"], ""); _indexName = ValidationHelper.GetString(ConfigurationManager.AppSettings["AzureSearchIndexName"], ""); // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(_indexName); } catch (Exception e) { errorMessage = e.Message.ToString(); } }
public async Task ImportDataAsync() { var indexclient = new SearchIndexClient(_serviceName, _indexname, new SearchCredentials(_adminkey)); var data = await ReadStringFromLocalFile(_fileName); var presidentlist = JsonConvert.DeserializeObject<List<President>>(data); try { // Add data to the index // Ajoute les données à l'index var response = indexclient.Documents.Index(IndexBatch.Create(presidentlist.Select(IndexAction.Create))); } catch (Exception e) { var dialog = new MessageDialog("Error importing data"); await dialog.ShowAsync(); } }
// This Sample shows how to delete, create, upload documents and query an index static void Main(string[] args) { string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"]; // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(GeoNamesIndex); Console.WriteLine("{0}", "Deleting index, data source, and indexer...\n"); if (DeleteIndexingResources()) { Console.WriteLine("{0}", "Creating index...\n"); CreateIndex(); Console.WriteLine("{0}", "Sync documents from Azure SQL...\n"); SyncDataFromAzureSQL(); } Console.WriteLine("{0}", "Complete. Press any key to end application...\n"); Console.ReadKey(); }
public static void SearchDocuments(SearchIndexClient indexClient, string searchText) { // Search using the supplied searchText and output documents that match try { var sp = new SearchParameters(); DocumentSearchResult<OCRTextIndex> response = indexClient.Documents.Search<OCRTextIndex>(searchText, sp); foreach (SearchResult<OCRTextIndex> result in response.Results) { Console.WriteLine("File ID: {0}", result.Document.fileId); Console.WriteLine("File Name: {0}", result.Document.fileName); Console.WriteLine("Extracted Text: {0}", result.Document.ocrText); } } catch (Exception e) { Console.WriteLine("Failed search: {0}", e.Message.ToString()); } }
private static void SearchDocuments(SearchIndexClient indexClient, string searchText, string filter = null) { // Execute search based on search text and optional filter var sp = new SearchParameters(); if (!String.IsNullOrEmpty(filter)) { sp.Filter = filter; } DocumentSearchResponse<Hotel> response = indexClient.Documents.Search<Hotel>(searchText, sp); foreach (SearchResult<Hotel> result in response) { Console.WriteLine(result.Document); } }
private static void LoadPackagesIntoAzureSearch(IEnumerable<Package> packagesToLoad) { _searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; _apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"]; _SearchClient = new SearchServiceClient(_searchServiceName, new SearchCredentials(_apiKey)); _IndexClient = new SearchIndexClient(_searchServiceName, "packages", new SearchCredentials(_apiKey)); Suggester sg = new Suggester(); sg.Name = "sg"; sg.SearchMode = SuggesterSearchMode.AnalyzingInfixMatching; sg.SourceFields = new List<string> { "NuGetIdRaw", "NuGetIdCollection" }; var indexDefinition = new Index() { Name = "packages", Suggesters = new List<Suggester> { sg }, Fields = new[] { new Field("NuGetIdRaw", DataType.String) { IsKey=false, IsSearchable=true, IsSortable=true, IsRetrievable=true }, new Field("NuGetIdCollection", DataType.Collection(DataType.String)) { IsKey=false, IsSearchable=true, IsSortable=false, IsRetrievable=true, IsFacetable=true, IsFilterable=true }, new Field("NuGetId", DataType.String) { IsKey=true, IsSearchable=false, IsSortable=false, IsRetrievable=true } } }; // NOTE: ALready exists _SearchClient.Indexes.Delete("packages"); _SearchClient.Indexes.Create(indexDefinition); // Populate try { for (var i = 0; i < packagesToLoad.Count(); i += 1000) { _IndexClient.Documents.Index( IndexBatch.Create( packagesToLoad.Skip(i).Take(1000).Select( doc => IndexAction.Create(new { NuGetId = Guid.NewGuid(), NuGetIdRaw = doc.NuGetId, NuGetIdCollection = doc.NuGetId.Split('.') }) ) ) ); } } 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.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))); } Console.Out.WriteLine("Completed"); }
public override void Initialize() { //this.Crawlers = new List<IProviderCrawler>(); var indexConfiguration = this.Configuration as AzureIndexConfiguration; if (indexConfiguration == null) throw new ConfigurationErrorsException("Index has no configuration."); InitializeSearchIndexInitializables(this.Configuration, this.Crawlers); string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"]; // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(Name); }
static SearchService() { client = new SearchServiceClient(AzureEndpoints.SearchAccount, new SearchCredentials(AzureEndpoints.SearchKey)); indexClient = client.Indexes.GetClient("entries"); }
private void SetupAzureSearchClient() { var searchName = CloudConfigurationManager.GetSetting("AzureSearchServiceName") ?? AzureConfiguration.AzureSearchServiceName; var searchKey = CloudConfigurationManager.GetSetting("AzureSearchServiceApiKey") ?? AzureConfiguration.AzureSearchServiceApiKey; //create client/index for indexing AzureServiceClient = new SearchServiceClient(searchName, new SearchCredentials(searchKey)); AzureIndexClient = AzureServiceClient.Indexes.GetClient(Name); var retryStrategy = new IncrementalRetryStrategy(AzureConfiguration.AzureSearchRetryCount, TimeSpan.FromSeconds(AzureConfiguration.AzureSearchRetryInitialInterval), TimeSpan.FromSeconds(AzureConfiguration.AzureSearchRetryIncrement)); var retryPolicy = new RetryPolicy<AzureErrorIndexDetectionStrategy>(retryStrategy); AzureIndexClient.SetRetryPolicy(retryPolicy); //create client/index for searching AzureSearchClient = AzureServiceClient.Indexes.GetClient(Name); //AzureSearchClient.UseHttpGetForQueries = true; }
static void Main(string[] args) { string searchServiceName = args[0]; var credentials = new SearchCredentials(args[1]); var searchClient = new SearchServiceClient(searchServiceName, credentials); try { IndexDefinitionResponse getResponse = searchClient.Indexes.Get(IndexName); if (getResponse?.Index != null) { Console.WriteLine("Deleting and recreating index " + IndexName); searchClient.Indexes.Delete(IndexName); } } catch (CloudException) { // We expect this if the index does not yet exist. } IndexDefinitionResponse createIndexResponse = searchClient.Indexes.Create(new Index( IndexName, new[] { new Field("ItemId", DataType.String) { IsKey = true }, new Field("Title", DataType.String) { IsSearchable = true }, new Field("Content", DataType.String) { IsSearchable = true }, new Field("CommentThreadId", DataType.Int32), new Field("TimelineEntryId", DataType.Int32), new Field("MediaAlbumId", DataType.Int32), new Field("UserMediaId", DataType.Int32) })); Index index = createIndexResponse.Index; var indexClient = new SearchIndexClient(searchServiceName, IndexName, credentials); using (var dbContext = new ApplicationDbContext(args[2])) { IEnumerable<TimelineEntry> timelineEntries = dbContext.TimelineEntries .Include(te => te.Message) .Include(te => te.CommentThread.Comments.Select(c => c.Text)); foreach (TimelineEntry entry in timelineEntries) { var batchActions = new List<IndexAction<MessageIndexEntry>>(); batchActions.Add(new IndexAction<MessageIndexEntry>( IndexActionType.Upload, new MessageIndexEntry { ItemId = "timeline-" + entry.TimelineEntryId, Content = entry.Message.Content, TimelineEntryId = entry.TimelineEntryId })); if (entry.CommentThread != null) { foreach (Comment comment in entry.CommentThread.Comments) { batchActions.Add(new IndexAction<MessageIndexEntry>( IndexActionType.Upload, new MessageIndexEntry { ItemId = "comment-" + comment.CommentId, Content = comment.Text.Content, TimelineEntryId = entry.TimelineEntryId, CommentThreadId = comment.CommentThreadId })); } } var batch = new IndexBatch<MessageIndexEntry>(batchActions); DocumentIndexResponse indexDocumentsResponse = indexClient.Documents.Index(batch); } IEnumerable<MediaAlbum> albums = dbContext.MediaAlbums .Include(a => a.CommentThread.Comments.Select(c => c.Text)); foreach (MediaAlbum album in albums) { var batchActions = new List<IndexAction<MessageIndexEntry>>(); batchActions.Add(new IndexAction<MessageIndexEntry>( IndexActionType.Upload, new MessageIndexEntry { ItemId = "album-" + album.MediaAlbumId, Title = album.Title, Content = album.Description, MediaAlbumId = album.MediaAlbumId })); if (album.CommentThread != null) { foreach (Comment comment in album.CommentThread.Comments) { batchActions.Add(new IndexAction<MessageIndexEntry>( IndexActionType.Upload, new MessageIndexEntry { ItemId = "comment-" + comment.CommentId, Content = comment.Text.Content, MediaAlbumId = album.MediaAlbumId, CommentThreadId = comment.CommentThreadId })); } } var batch = new IndexBatch<MessageIndexEntry>(batchActions); DocumentIndexResponse indexDocumentsResponse = indexClient.Documents.Index(batch); } IEnumerable<UserMedia> medias = dbContext.UserMedias .Include(m => m.Description) .Include(m => m.CommentThread.Comments.Select(c => c.Text)); foreach (UserMedia media in medias) { var batchActions = new List<IndexAction<MessageIndexEntry>>(); batchActions.Add(new IndexAction<MessageIndexEntry>( IndexActionType.Upload, new MessageIndexEntry { ItemId = "media-" + media.UserMediaId, Title = media.Title, Content = media.Description?.Content, UserMediaId = media.UserMediaId, MediaAlbumId = media.MediaAlbumId })); if (media.CommentThread != null) { foreach (Comment comment in media.CommentThread.Comments) { batchActions.Add(new IndexAction<MessageIndexEntry>( IndexActionType.Upload, new MessageIndexEntry { ItemId = "comment-" + comment.CommentId, Content = comment.Text.Content, UserMediaId = media.UserMediaId, MediaAlbumId = media.MediaAlbumId, CommentThreadId = comment.CommentThreadId })); } } var batch = new IndexBatch<MessageIndexEntry>(batchActions); DocumentIndexResponse indexDocumentsResponse = indexClient.Documents.Index(batch); } } }
public async Task<List<Beer>> Get(string searchTerm) { try { //Setup tracking how long the HTTP request takes. telemtryClient.Context.Operation.Id = Guid.NewGuid().ToString(); telemtryClient.Context.Operation.Name = "Search"; var stopwatch = System.Diagnostics.Stopwatch.StartNew(); //Log the fact we've search some beers telemtryClient.TrackEvent("SearchedBeers"); indexClient = serviceClient.Indexes.GetClient("beers"); //Setup suggestionParmeters for AzureSearch var suggestParameters = new SuggestParameters(); suggestParameters.UseFuzzyMatching = true; suggestParameters.Top = 25; suggestParameters.HighlightPreTag = "["; suggestParameters.HighlightPostTag = "]"; suggestParameters.MinimumCoverage = 100; var suggestions = await indexClient.Documents.SuggestAsync<AzureSearchBeerResponse>(searchTerm, "nameSuggester", suggestParameters); //Convert to Beer Drinkin Beer Type & save to our DB. var results = new List<Beer>(); foreach (var result in suggestions.Results) { var indexedBeer = result.Document; var beer = new Beer { Id = indexedBeer.Id, Abv = indexedBeer.Abv, Description = indexedBeer.Description, BreweryDbId = indexedBeer.Id, Name = indexedBeer.Name }; beer.Image = new Image(); //Fetch Brewery information if(indexedBeer.Images.Count() > 0) { beer.HasImages = true; beer.Image.SmallUrl = indexedBeer?.Images[0]; beer.Image.MediumUrl = indexedBeer?.Images[1]; beer.Image.LargeUrl = indexedBeer?.Images[2]; } results.Add(beer); } stopwatch.Stop(); telemtryClient.TrackRequest("BeerSearch", DateTime.Now, stopwatch.Elapsed, "200", true); return results; } catch(Exception ex) { telemtryClient.TrackException(ex); return new List<DataObjects.Beer>(); } }
public static void CreateIndex() { SearchClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchApiKey)); IndexClient = SearchClient.Indexes.GetClient(IndexName); try { if (DeleteIndex(SearchClient, IndexName)) { // Create index schema for this dataset Console.WriteLine("{0}", "Creating index...\n"); var definition = new Index() { Name = IndexName, Fields = new[] { new Field("id", DataType.String) { IsKey = true, IsSearchable = false, IsFilterable = true, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("title", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("imdbID", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("spanishTitle", DataType.String) { IsKey = false, IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("imdbPictureURL", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("year", DataType.Int32) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("rtID", DataType.String) { IsKey = false, IsSearchable = false, IsFilterable = false, IsSortable = false, IsFacetable = false, IsRetrievable = true }, new Field("rtAllCriticsRating",DataType.Double) { IsKey = false, IsSearchable = false, IsFilterable = true, IsSortable = true, IsFacetable = true, IsRetrievable = true }, new Field("recommendations",DataType.Collection(DataType.String)) { IsSearchable = true, IsFilterable = true, IsFacetable = true } }, Suggesters = new[] { new Suggester("sg", SuggesterSearchMode.AnalyzingInfixMatching, new string[] { "title" }) }, CorsOptions = new CorsOptions(new string[] { "*" }) // This * option should only be enabled for demo purposes or when you fully trust your users }; SearchClient.Indexes.Create(definition); // first apply the changes and if we succeed then record the new version that // we'll use as starting point next time Console.WriteLine("{0}", "Uploading Content...\n"); ApplyData(@"data\movies.dat"); } } catch (Exception ex) { Console.WriteLine("Error: {0}:\r\n", ex.Message.ToString()); } }
private void SetupUI() { Title = BeerDrinkin.Core.Helpers.Strings.Search_Title; searchBar.Layer.BorderWidth = 1; searchBar.Layer.BorderColor = "15A9FE".ToUIColor().CGColor; serviceClient = new SearchServiceClient("beerdrinkin", new SearchCredentials(Core.Helpers.Keys.AzureSearchKey)); indexClient = serviceClient.Indexes.GetClient("beers"); View.AddSubview(suggestionsTableView); var dataSource = new SearchPlaceholderDataSource(this); placeHolderTableView.Source = dataSource; placeHolderTableView.ReloadData(); placeHolderTableView.BackgroundColor = "F7F7F7".ToUIColor(); placeHolderTableView.ContentInset = new UIEdgeInsets(10, 0, 0, 0); placeHolderTableView.SeparatorStyle = UITableViewCellSeparatorStyle.None; placeHolderTableView.ScrollsToTop = true; searchResultsTableView.ScrollsToTop = true; View.BringSubviewToFront(placeHolderTableView); searchBar.TextChanged += async delegate { var connected = await Plugin.Connectivity.CrossConnectivity.Current.IsReachable("google.com", 1000); if(!connected) return; if(searchBar.Text != "") { View.BringSubviewToFront(suggestionsTableView); var suggestParameters = new SuggestParameters(); suggestParameters.UseFuzzyMatching = true; suggestParameters.Top = 25; suggestParameters.HighlightPreTag = "["; suggestParameters.HighlightPostTag = "]"; suggestParameters.MinimumCoverage = 100; var response = await indexClient.Documents.SuggestAsync<BeerDrinkin.Models.IndexedBeer>(searchBar.Text, "nameSuggester", suggestParameters); var results = new List<string>(); foreach(var r in response) { results.Add(r.Text); } var suggestionSource = new SearchSuggestionDataSource(results); suggestionSource.SelectedRow += (int index) => { searchBar.Text = response.Results[index].Document.Name; SearchForBeers(this, null); }; suggestionsTableView.Source = suggestionSource; suggestionsTableView.ReloadData(); } else { View.BringSubviewToFront(placeHolderTableView); } }; }
public static void InitializeSearchService() { var searchServiceClient = new SearchServiceClient(Config.SearchServiceName, new SearchCredentials(Config.SearchServiceKey)); SearchIndexClient = searchServiceClient.Indexes.GetClient("concerts"); }
public AzureSearchClient(string searchServiceName, string apiKey, string indexName) { _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(indexName); }
public static void InitializeSearchService() { var searchServiceClient = new SearchServiceClient(Config.SearchServiceName, new SearchCredentials(Config.SearchServiceKey)); if (!Config.RunningInDev) { CreateIndex(searchServiceClient); CreateIndexer(searchServiceClient); searchServiceClient.Indexers.Run("fromsql"); } SearchIndexClient = searchServiceClient.Indexes.GetClient("concerts"); }
private static void UploadDocuments(SearchIndexClient indexClient) { var documents = new Hotel[] { new Hotel() { HotelId = "1058-441", HotelName = "Fancy Stay", BaseRate = 199.0, Category = "Luxury", Tags = new[] { "pool", "view", "concierge" }, ParkingIncluded = false, LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.Zero), Rating = 5, Location = GeographyPoint.Create(47.678581, -122.131577) }, new Hotel() { HotelId = "666-437", HotelName = "Roach Motel", BaseRate = 79.99, Category = "Budget", Tags = new[] { "motel", "budget" }, ParkingIncluded = true, LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero), Rating = 1, Location = GeographyPoint.Create(49.678581, -122.131577) }, new Hotel() { HotelId = "970-501", HotelName = "Econo-Stay", BaseRate = 129.99, Category = "Budget", Tags = new[] { "pool", "budget" }, ParkingIncluded = true, LastRenovationDate = new DateTimeOffset(1995, 7, 1, 0, 0, 0, TimeSpan.Zero), Rating = 4, Location = GeographyPoint.Create(46.678581, -122.131577) }, new Hotel() { HotelId = "956-532", HotelName = "Express Rooms", BaseRate = 129.99, Category = "Budget", Tags = new[] { "wifi", "budget" }, ParkingIncluded = true, LastRenovationDate = new DateTimeOffset(1995, 7, 1, 0, 0, 0, TimeSpan.Zero), Rating = 4, Location = GeographyPoint.Create(48.678581, -122.131577) }, new Hotel() { HotelId = "566-518", HotelName = "Surprisingly Expensive Suites", BaseRate = 279.99, Category = "Luxury", ParkingIncluded = false } }; try { indexClient.Documents.Index(IndexBatch.Create(documents.Select(doc => IndexAction.Create(doc)))); } 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.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))); } // Wait a while for indexing to complete. Thread.Sleep(2000); }
private void UploadTweetsToSearchIndex(List<Tweet> tweets) { if (_searchIndexClient == null) { _searchIndexClient = _searchServiceClient.Indexes.GetClient("asp5tweets"); } var enrichedTweets = _tweetsEnricher.FlattenAndEnrichTweets(tweets); try { _searchIndexClient.Documents.Index(IndexBatch.Create(enrichedTweets.Select(IndexAction.Create))); Trace.TraceInformation($"Uploaded indexes for tweets: {string.Join(", ", enrichedTweets.Select(t => t.TweetId))}."); } catch (IndexBatchException e) { Trace.TraceError("Failed to index some tweets: {0}", string.Join(", ", e.IndexResponse.Where(r => !r.Succeeded).Select(r => r.Key))); } //Make it slow Thread.Sleep(200); }
static void Main(string[] args) { // This sample will Create an Azure Search Index and import data from a set of JSON files SearchClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchApiKey)); IndexClient = SearchClient.Indexes.GetClient(IndexName); CreateIndex(); SearchDocuments(IndexClient, "star wars"); Console.WriteLine("Press any key to continue..."); Console.ReadLine(); }
static void Main(string[] args) { // Ingest data from Mahout outout and merge into Azure Search string line; int currentID = -1; int docCounter = 0; List<Recommendation> recList = new List<Recommendation>(); List<SearchIndexSchema> sisList = new List<SearchIndexSchema>(); List<string> recs = new List<string>(); // Configure cloud storage and search connections StorageCredentials credentials = new StorageCredentials(StorageAccountName, StorageApiKey); CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference(StorageContainer); CloudBlockBlob blob = container.GetBlockBlobReference("output\\part-r-00000"); // In large implementations there may be multiple part files serviceClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchApiKey)); indexClient = serviceClient.Indexes.GetClient(IndexName); // Open and parse mahout output file using (var stream = blob.OpenRead()) { using (StreamReader file = new StreamReader(stream)) { while ((line = file.ReadLine()) != null) { Recommendation rec = new Recommendation(); char[] delimiters = new char[] { '\t' }; string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); rec.itemID = Convert.ToInt32(parts[0]); rec.recItemID = Convert.ToInt32(parts[1]); rec.percentSimilar = Convert.ToDouble(parts[2]); if (currentID != rec.itemID) { docCounter++; if (recList.Count > 0) { recList = recList.OrderByDescending(w => w.percentSimilar).Take(5).ToList(); // Take the 5 most similar items foreach (var item in recList) recs.Add(item.recItemID.ToString()); recList.Clear(); sisList.Add(new SearchIndexSchema { id = currentID.ToString(), recommendations = recs.ToArray() }); recs.Clear(); if (sisList.Count == 500) { MergeDocument(sisList); sisList.Clear(); Console.WriteLine("{0} Docs written to Index...", docCounter - 1); } } currentID = rec.itemID; } recList.Add(rec); } file.Close(); if (sisList.Count > 0) { MergeDocument(sisList); sisList.Clear(); Console.WriteLine("{0} Docs written to Index...", docCounter - 1); } } } Console.WriteLine("Press any key to continue..."); Console.ReadLine(); }
static void Main(string[] args) { //Download Mahout distribution and store in Azure Blob DownloadAndUnzipMahoutDistribution(); UploadMahoutDistributiontoBlobStorage(); UploadHiveQueryToBlobStorage(); //Setup the Azure SQL Database CreateAzureSQLTables(); BCPDataIntoAzureSQLTable(); //Setup the Azure Search Index // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(IndexName); Console.WriteLine("{0}", "Deleting index...\n"); DeleteIndex(); Console.WriteLine("{0}", "Creating index...\n"); CreateIndex(); Console.WriteLine("{0}", "Uploading documents...\n"); UploadDocuments("data1.json"); UploadDocuments("data2.json"); Console.WriteLine("{0}", "Creating the Azure Search Indexer...\n"); CreateIndexer(); Console.WriteLine("{0}", "Complete. Press any key to end application...\n"); Console.ReadKey(); }
public SearchController() { _SearchClient = new SearchServiceClient(_searchServiceName, new SearchCredentials(_apiKey)); _IndexClient = new SearchIndexClient(_searchServiceName, "packages", new SearchCredentials(_apiKey)); }