private ISearchResult Results(SearchParameters sp) { var client = GetClient(); var config = GetConfiguration(); ISearchIndexClient indexClient = client.Indexes.GetClient(config.IndexName); var startTime = DateTime.UtcNow; var response = indexClient.Documents.Search(_searchTerm, sp); var processStartTime = DateTime.UtcNow; var results = new Models.SearchResult(); foreach (var result in response.Results) { results.Content.Add(FromDocument(result.Document, result.Score)); } if (response.Facets != null) { foreach (var facet in response.Facets) { var searchFacet = new SearchFacet() { Name = facet.Key, Items = facet.Value.Select(x => new KeyValuePair <string, long>(x.Value.ToString(), x.Count.HasValue ? x.Count.Value : 0)) }; results.Facets.Add(searchFacet); } } if (response.Count != null) { results.Count = (int)response.Count; } if (config.LogSearchPerformance) { string lb = Environment.NewLine; Log.Info($"AzureSearch Log (cached client){lb} - Response Duration: {(int)(processStartTime - startTime).TotalMilliseconds}ms{lb} - Process Duration: {(int)(DateTime.UtcNow - processStartTime).TotalMilliseconds}ms{lb} - Results Count: {results.Count}{lb} - Origin: {HttpContext.Current?.Request?.Url}{lb} - Index name: {config.IndexName}{lb} - Base uri: {indexClient.BaseUri}{lb} - Search term: {_searchTerm}{lb} - Uri query string: {HttpUtility.UrlDecode(sp.ToString())}{lb}"); } return(results); }
static void Main(string[] args) { IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); IConfigurationRoot configuration = builder.Build(); SearchServiceClient serviceClient = CreateSearchServiceClient(configuration); string indexName = configuration["SearchIndexName"]; RecreateIndex(indexName, serviceClient); // TODO: Populate index and do some queries ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName); Console.WriteLine("Uploading documents..."); UploadDocuments(indexClient); // Some search queries to do on the portal: // 1) * (returns all up to 50 matches) // 2) Hotel in new york or san antonio // 3) Hotel with pool&$count=true // 4) pool&$select=HotelName,Tags&$count=true // 5) *&$top=2&$count=true then *&$top=2&$skip=2&$count=true // 6) *&$filter=Rating gt 4.5 // 7) *&$orderby=Rating desc SearchParameters parameters; DocumentSearchResult <Hotel> results; // Query 1 parameters = new SearchParameters(); results = indexClient.Documents.Search <Hotel>("*", parameters); // Query 2 parameters = new SearchParameters() { Filter = "Rating gt 4.5" }; results = indexClient.Documents.Search <Hotel>("*", parameters); Console.WriteLine("Complete. Press any key to end application..."); }
// TODO: make a demo for 'prefix' matching, pulling back items with XXX in the name static void Main(string[] args) { Console.WriteLine("Starting Azure Search Examples"); Console.WriteLine("Connecting to Storage Search"); // note: this can be either an admin key or a query key, in this case we'll use admin // but we'd prefer to use query if all we're doing is reading data. // Wherever possible, reuse HTTP connections. If you are using the Azure Search .NET SDK, this means you should reuse // an instance or SearchIndexClient instance, and if you are using the REST API, you should reuse a single HttpClient. serviceClient = new SearchServiceClient(SEARCH_SERVICE_NAME, new SearchCredentials(API_KEY)); Console.WriteLine($"Creating an Index Client"); indexClient = new SearchIndexClient( SEARCH_SERVICE_NAME, SEARCH_INDEX_NAME, new SearchCredentials(API_KEY)); // note: you can also create a client from an already existing index // indexClient = serviceClient.Indexes.GetClient(SEARCH_INDEX_NAME); // run tests Console.WriteLine("Starting example snippets\r\n"); DeleteIndexIfExists(); CreateIndex(); CreateIndexEntries(); SearchEntireIndexByPhrase(); SearchIndexByRate(); SearchIndexByName(); SearchIndexByNameWithFacets(); // paging // upload csv data from blob // IndexBatch.Merge // IndexBatch.MergeOrUpload // IndexBatch.Delete // IndexBatch.New Console.WriteLine($"Examples completed, press enter to quit."); Console.ReadLine(); }
static void Main(string[] args) { IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json"); IConfigurationRoot configuration = builder.Build(); string indexName = configuration[indexFieldName]; string pageViewDataCsvPath = configuration["FactsheetWebAnalyticsPath"]; var data = File.ReadAllLines(pageViewDataCsvPath) .ToList() .Select(l => l.Split(',').ToList()) .Select(i => new { RelativeUrl = i.ElementAt(0), Uniques = Convert.ToDouble(i.ElementAt(1)) }) .ToDictionary(i => i.RelativeUrl, i => i.Uniques); Func <string, double> getUniquesForRelativeUrl = relativeUrl => { if (data.ContainsKey(relativeUrl)) { return(data[relativeUrl]); } else { return(0); } }; SearchServiceClient serviceClient = CreateSearchServiceClient(configuration); if (serviceClient.Indexes.Exists(indexName)) { serviceClient.Indexes.Delete(indexName); } CreateIndex(serviceClient, indexName); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName); String jsonString = File.ReadAllText(configuration["FactSheetJsonPath"]); IEnumerable <FactSheet> factsheets = DeserializeFactsheets(jsonString, getUniquesForRelativeUrl); UploadDocuments(indexClient, factsheets); }
private static void RunQueries(ISearchIndexClient indexClient) { SearchParameters parameters; DocumentSearchResult <Location> results; Console.WriteLine("Search the entire index for the term and return only the name field:\n"); parameters = new SearchParameters() { Select = new[] { "name", "geo" }, QueryType = Microsoft.Azure.Search.Models.QueryType.Full, SearchFields = new [] { "name", "asciiName", "alternatifName" }, SearchMode = SearchMode.All }; results = indexClient.Documents.Search <Location>("qu~", parameters); WriteDocuments(results); }
public async static Task <IEnumerable <string> > GetSuggestionsAsync(string searchQuery) { List <string> suggestions = new List <string>(); SearchServiceClient serviceClient = new SearchServiceClient(SearchConstants.SearchServiceName, new SearchCredentials(SearchConstants.SearchApiKey)); var sp = new SearchParameters(); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(SearchConstants.SearchIndexName); DocumentSearchResult response = await indexClient.Documents.SearchAsync(searchQuery.Trim() + "*", sp); var results = response.Results.Select(s => s.Document).ToList(); foreach (var result in results) { suggestions.AddRange((string[])result["tags"]); } return(suggestions.Where(w => w.StartsWith(searchQuery, StringComparison.OrdinalIgnoreCase)).Distinct()); }
// 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(); }
private static void SearchIndex() { SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey)); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("customer"); SearchParameters parameters; DocumentSearchResult <Customer> results; parameters = new SearchParameters() { Select = new[] { "customername", "customeremail" } }; results = indexClient.Documents.Search <Customer>("Mark", parameters); foreach (SearchResult <Customer> result in results.Results) { Console.WriteLine($"{result.Document.customername},{result.Document.customeremail}"); } }
public static void PopulateSearchIndexEntServices(int?EntSvcIDX) { try { //connect to Azure Search SearchServiceClient serviceClient = CreateSearchServiceClient(); //get all ent services needing to sync List <EECIP_Index> _ps = db_EECIP.GetT_OE_ORGANIZATION_ENT_SVCS_ReadyToSync(EntSvcIDX); if (_ps != null && _ps.Count > 0) { var batch = IndexBatch.Upload(_ps); try { //send to Azure ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("eecip"); indexClient.Documents.Index(batch); //then update local rec sync ind foreach (EECIP_Index p in _ps) { db_EECIP.InsertUpdatetT_OE_ORGANIZATION_ENT_SVCS(p.KeyID.ConvertOrDefault <int>() - 100000, null, null, null, null, null, null, null, null, true, null, false); } } 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))); } } } catch (Exception ex) { db_Ref.InsertT_OE_SYS_LOG("AzureSearch", ex.InnerException.ToString().SubStringPlus(0, 2000)); } }
public static async Task RunAsync( [BlobTrigger("datum/{name}", Connection = "Search")] CloudBlockBlob myBlob, [CosmosDB("taskDatabase", "TaskCollection", ConnectionStringSetting = "CosmosDB")] IAsyncCollector <object> todos, string name, ILogger log) { SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName: "jfk-search-service-qymjpzort5hho", credentials: new SearchCredentials("023DB82430FFA416AD39EEF8A6FDFF2A")); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("jfkindex"); await serviceClient.Indexers.RunAsync("jfkindexer"); try { IndexerExecutionInfo execInfo = serviceClient.Indexers.GetStatus("jfkindexer"); Console.WriteLine("{0} veces ejecutadas.", execInfo.ExecutionHistory.Count); Console.WriteLine("Indexer Status: " + execInfo.Status.ToString()); IndexerExecutionResult result = execInfo.LastResult; Console.WriteLine("Latest run"); Console.WriteLine(" Run Status: {0}", result.Status.ToString()); Console.WriteLine(" Total Documents: {0}, Failed: {1}", result.ItemCount, result.FailedItemCount); TimeSpan elapsed = result.EndTime.Value - result.StartTime.Value; Console.WriteLine(" StartTime: {0:T}, EndTime: {1:T}, Elapsed: {2:t}", result.StartTime.Value, result.EndTime.Value, elapsed); string errorMsg = (result.ErrorMessage == null) ? "none" : result.ErrorMessage; Console.WriteLine(" ErrorMessage: {0}", errorMsg); Console.WriteLine(" Document Errors: {0}, Warnings: {1}\n", result.Errors.Count, result.Warnings.Count); } catch (Exception e) { Console.WriteLine("Error: ", e); } var param = new SearchParameters { Select = new[] { "*" }, //IncludeTotalResultCount = true }; System.Threading.Thread.Sleep(19000); log.LogInformation($"espera terminada{name}"); var results = indexClient.Documents.Search <Book>($"{name}", param); log.LogInformation($"enviando a cosmos.... {myBlob.Name}"); await todos.AddAsync(results); }
/// <summary> /// Initializes a new instance of the <see cref="EventSearchService"/> class. /// </summary> /// <param name="optionsAccessor">A set of key/value application configuration properties.</param> /// <param name="eventRepository">Event repository dependency injection.</param> /// <param name="logger">Instance to send logs to the Application Insights service.</param> /// <param name="searchServiceClient">Search service client dependency injection.</param> /// <param name="searchIndexClient">Search index client dependency injection.</param> public EventSearchService( IOptions<SearchServiceSettings> optionsAccessor, IEventRepository eventRepository, ILogger<EventSearchService> logger, ISearchServiceClient searchServiceClient, ISearchIndexClient searchIndexClient) { optionsAccessor = optionsAccessor ?? throw new ArgumentNullException(nameof(optionsAccessor)); this.options = optionsAccessor.Value; var searchServiceValue = this.options.SearchServiceName; this.initializeTask = new Lazy<Task>(() => this.InitializeAsync()); this.eventRepository = eventRepository; this.logger = logger; this.searchServiceClient = searchServiceClient; this.searchIndexClient = searchIndexClient; this.retryPolicy = Policy.Handle<CloudException>( ex => (int)ex.Response.StatusCode == StatusCodes.Status409Conflict || (int)ex.Response.StatusCode == StatusCodes.Status429TooManyRequests) .WaitAndRetryAsync(Backoff.LinearBackoff(TimeSpan.FromMilliseconds(2000), 2)); }
static BillionSearch() { try { //PROD string searchServiceName = SearchServiceNameProd; string apiKey = SearchServiceApiKeyProd; //TEST //string searchServiceName = SearchServiceName; //string apiKey = SearchServiceApiKey; // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient("mowido-index"); } catch (Exception e) { errorMessage = e.Message.ToString(); } }
public DocumentSearchClient(IConfiguration configuration) { try { searchServiceName = configuration.GetSection("SearchServiceName")?.Value; apiKey = configuration.GetSection("SearchApiKey")?.Value; IndexName = configuration.GetSection("SearchIndexName")?.Value; telemetryClient.InstrumentationKey = configuration.GetSection("InstrumentationKey")?.Value; // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(IndexName); Schema = new SearchSchema().AddFields(_searchClient.Indexes.Get(IndexName).Fields); Model = new SearchModel(Schema); } catch (Exception e) { throw new ArgumentException(e.Message.ToString()); } }
static int GetCurrentDocCount(ISearchIndexClient IndexClient) { // Get the current doc count of the specified index try { SearchParameters sp = new SearchParameters() { SearchMode = SearchMode.All, IncludeTotalResultCount = true }; DocumentSearchResult response = IndexClient.Documents.Search("*", sp); return(Convert.ToInt32(response.Count)); } catch (Exception ex) { Console.WriteLine("Error: {0}", ex.Message.ToString()); } return(-1); }
static Search() { try { // We will use REST since I am using autocomplete which is currently private preview and not yet in .NET SDK _serviceUri = new Uri("https://" + ConfigurationManager.AppSettings["SearchServiceName"] + ".search.windows.net"); _httpClient = new HttpClient(); _httpClient.DefaultRequestHeaders.Add("api-key", ConfigurationManager.AppSettings["SearchServiceApiKey"]); 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(indexName); } catch (Exception e) { errorMessage = e.Message.ToString(); } }
public async Task <IActionResult> DeleteTodoById( [HttpTrigger(AuthorizationLevel.Anonymous, "delete", Route = "todos/{id}")] HttpRequest req, string id, [AzureSearch(Key = "{id}")] Ticket toDeleteTicket, [AzureSearch(ApiKey = "AzureSearch:WriteApiKey")] ISearchIndexClient writeTodos, ILogger logger) { if (toDeleteTicket == null) { logger.LogWarning($"Ticket not found (id: {id})"); return(new NotFoundObjectResult(new { error = $"Ticket not found (id: {id})", id })); } logger.LogDebug($"Deleting ticket (id: {id})"); await writeTodos.Documents .IndexAsync(IndexBatch.Delete(new[] { toDeleteTicket })) .ConfigureAwait(false); logger.LogDebug($"Successfully deleted ticket (id: {id})"); return(new NoContentResult()); }
public DocumentSearchResult <Contest> RunQuery(SearchIndexParameters parameters) { try { SearchParameters searchParameters = new SearchParameters() { Filter = parameters.Filter, Select = parameters.Select }; ISearchIndexClient indexClient = _searchClient.Indexes.GetClient("scoresindex"); var results = indexClient.Documents.Search <Contest>("*", searchParameters); return(results); } catch (Exception ex) { Console.WriteLine(ex); return(null); } }
public async Task <ISearchIndexClient> GetOrCreateIndex() { try { if (_searchIndexClient == null) { if (!await _searchServiceClient.Indexes.ExistsAsync(_indexName)) { await Initialize(); } _searchIndexClient = _searchServiceClient.Indexes.GetClient(_indexName); } return(_searchIndexClient); } catch (Exception e) { throw new Exception($"Index failure calling search service client: {e.Message}", e); } }
private bool disposedValue = false; // 重複する呼び出しを検出するには protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { if (_searchClient != null) { _searchClient.Dispose(); } if (_indexClient != null) { _indexClient.Dispose(); } _searchClient = null; _indexClient = null; } disposedValue = true; } }
private static void ImportDocuments(ISearchIndexClient indexClient) { var actions = new List <IndexAction <Account> >(); string line; //JObject o1 = JObject.Parse(File.ReadAllText("accounts.json")); //// read JSON directly from a file //using (StreamReader file = File.OpenText("accounts.json")) //using (JsonTextReader reader = new JsonTextReader(file)) //{ // JObject o2 = (JObject)JToken.ReadFrom(reader); // Account account = o2.ToObject<Account>(); // actions.Add(IndexAction.Upload(account)); //} using (System.IO.StreamReader file = new System.IO.StreamReader("accounts.json")) { while ((line = file.ReadLine()) != null) { JObject json = JObject.Parse(line); Account account = json.ToObject <Account>(); actions.Add(IndexAction.Upload(account)); } file.Close(); } var batch = IndexBatch.New(actions); Console.WriteLine("Indexed......"); try { indexClient.Documents.Index(batch); } catch (Exception ex) { throw ex; } }
// Upload documents as a batch private static void UploadDocuments(ISearchIndexClient indexClient) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(@"D:\Projects\AZSD\SuperHeroes.xml"); XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/root/SuperHeros/SuperHero"); List <SuperHero> SuperHeroCollections = new List <SuperHero>(); var actions = new IndexAction <SuperHero> [nodeList.Count]; int i = 0; foreach (XmlNode node in nodeList) { SuperHero SuperHero = new SuperHero(); SuperHero.ID = node.SelectSingleNode("ID").InnerText; SuperHero.NAME = node.SelectSingleNode("NAME").InnerText; SuperHero.ScreenName = node.SelectSingleNode("ScreenName").InnerText; SuperHero.Power = node.SelectSingleNode("Power").InnerText; SuperHeroCollections.Add(SuperHero); actions[i] = IndexAction.Upload(SuperHero); i++; } IndexBatch <SuperHero> batch = IndexBatch.New(actions); try { indexClient.Documents.Index(batch); } catch (Exception e) { // When a service is under load, indexing might fail for some documents in the batch. // Depending on your application, you can compensate by 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.Message)); } // Wait 2 seconds before starting queries Console.WriteLine("Waiting for indexing...\n"); Thread.Sleep(2000); }
private void CreateAzureIndex(SearchServiceClient serviceClient) { #region Delete and Create an Index DeleteIndexIfExists(serviceClient); var accountIndexDefinition = new Index() { Name = indexName, Fields = FieldBuilder.BuildForType <Account>() }; #region Add Suggestors var suggestor = new Suggester(autoCompleteSuggestor); suggestor.SourceFields.Add("firstName"); suggestor.SourceFields.Add("lastName"); accountIndexDefinition.Suggesters = new List <Suggester>(); accountIndexDefinition.Suggesters.Add(suggestor); #endregion serviceClient.Indexes.CreateOrUpdate(accountIndexDefinition); #endregion //if (!serviceClient.Indexes.Exists(indexName)) //{ // var accountIndexDefinition = new Index() // { // Name = indexName, // Fields = FieldBuilder.BuildForType<Account>() // }; // serviceClient.Indexes.Create(accountIndexDefinition); //} ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName); if (indexClient != null) { ImportData(indexClient); } }
static void Main(string[] args) { string searchServiceName = "search-suzano-bot"; string apiKey = "380FF6176DF4212E336643BA3F23CE4A"; bool DeleteToCreate = false; // Create an HTTP reference to the catalog index _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey)); _indexClient = _searchClient.Indexes.GetClient(indexName); // To process a file I'm deleting the given index_name, and recreating the index based on the Class definition. // If the file doesn't have the full data, the data existent over the Azure Side will be lost. if (DeleteToCreate) { DeleteHotelsIndexIfExists(_searchClient, indexName); CreateHotelsIndex(_searchClient, indexName); } UploadDocuments(_indexClient, "C:\\Users\\visouza\\Repos\\suzano-bot\\suzanobot\\AzureSearchImporter\\data\\fiscal.txt", true); }
//static string searchServiceQueryKey = ConfigurationManager.AppSettings["StephenSearchServiceQueryKey"]; static void Main(string[] args) { SearchServiceClient serviceClient = new SearchServiceClient (searchServiceName, new SearchCredentials(searchServiceAdminKey)); if (serviceClient.Indexes.Exists("accounts")) { serviceClient.Indexes.Delete("accounts"); } var accountIndexDefinition = new Index() { Name = "accounts", Fields = FieldBuilder.BuildForType <Account>() }; serviceClient.Indexes.Create(accountIndexDefinition); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("accounts"); ImportDocuments(indexClient); }
public Onspd RunQuery(ISearchIndexClient indexClient, string postcode) { SearchParameters parameters; DocumentSearchResult <Onspd> results; parameters = new SearchParameters { Select = new[] { "pcd", "pcd2", "pcds", "Parish", "LocalAuthority", "Region", "County", "Country", "lat", "long" }, SearchMode = SearchMode.All, Top = 1, QueryType = QueryType.Full }; results = indexClient.Documents.Search <Onspd>(postcode, parameters); if (results.Results != null && results.Results.Any()) { return(results.Results.First().Document); } return(null); }
private void ButtonIndex_Click(object sender, EventArgs e) { Article article = new Article { Title = InputTitle.Text, Category = InputCategory.Text, Text = InputText.Text }; using (var serviceClient = new SearchServiceClient(SearchService, new SearchCredentials(SearchServiceKey))) { var actions = new IndexAction <Article>[] { IndexAction.MergeOrUpload(article) }; var batch = IndexBatch.New(actions); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(SearchServiceIndex); indexClient.Documents.Index(batch); } }
// This sample shows how to delete, create, upload documents and query an index static void Main(string[] args) { SearchServiceClient serviceClient = CreateSearchServiceClient(); Console.WriteLine("{0}", "Deleting index...\n"); DeleteHotelsIndexIfExists(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(); RunQueries(indexClientForQueries); Console.WriteLine("{0}", "Complete. Press any key to end application...\n"); Console.ReadKey(); }
public async Task Index() { using (var client = CreateServiceClient()) { if (await client.Indexes.ExistsAsync(index) == false) { RaiseEvent("Creating Index"); CreateIndex(client); } else { RaiseEvent("Index Exists"); } var data = GetData(); var privateData = GetPrivateData(); var actions = new List <IndexAction <SearchIndex> >(); foreach (var item in data.Concat(privateData)) { actions.Add(IndexAction.Upload(item)); RaiseEvent($"Adding {item.Name} to Azure Search"); } var batch = IndexBatch.New(actions); try { RaiseEvent($"Uploading Indexes to Azure Search"); ISearchIndexClient indexClient = client.Indexes.GetClient(index); indexClient.Documents.Index(batch); RaiseEvent($"Indexing Complete"); } catch (Exception ex) { RaiseEvent("Indexing Failed" + ex.ToString()); } } }
public async Task <T> SearchById(string id) { ISearchIndexClient client = await GetOrCreateIndex(); try { DocumentSearchResult <T> azureSearchResult = await client.Documents.SearchAsync <T>($"\"{id}\"", new SearchParameters { SearchFields = new List <string> { "id" } }); return(azureSearchResult?.Results?.SingleOrDefault()?.Document); } catch (Exception ex) { throw new FailedToQuerySearchException("Failed to query search", ex); } }
public async static void Run(Stream myBlob, string blobName, TraceWriter log) { log.Info($"C# Blob trigger function Processed blob\n Name:{blobName} \n Size: {myBlob.Length} Bytes"); log.Info($"Extracting images from the PDF"); List <Image> images = PdfImageExtractor.ExtractImages(myBlob, blobName, log); StringBuilder extractedText = new StringBuilder(); try { log.Info($"Extracting text from images"); VisionServiceClient visionServiceClient = new VisionServiceClient(VisionServiceSubscriptionKey); foreach (Image image in images) { using (var stream = new MemoryStream()) { image.Save(stream, System.Drawing.Imaging.ImageFormat.Png); stream.Seek(0, SeekOrigin.Begin); await ExtractTextAsync(visionServiceClient, stream, extractedText, log); } } if (extractedText.Length != 0) { SearchServiceClient serviceClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchServiceAPIKey)); ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(IndexName); string documentId = HttpServerUtility.UrlTokenEncode(Encoding.UTF8.GetBytes(blobName)); log.Info($"Uploading document to Azure Search using ID: {documentId}"); UploadToAzureSeearch(indexClient, documentId, log, extractedText.ToString()); } } catch (Exception ex) { log.Info($"Error: {ex.Message}"); } }
private static void RunQueries(ISearchIndexClient indexClient) { SearchParameters parameters; DocumentSearchResult<Hotel> results; Console.WriteLine("Search the entire index for the term 'budget' and return only the hotelName field:\n"); parameters = new SearchParameters() { Select = new[] { "hotelName" } }; results = indexClient.Documents.Search<Hotel>("budget", parameters); WriteDocuments(results); Console.Write("Apply a filter to the index to find hotels cheaper than $150 per night, "); Console.WriteLine("and return the hotelId and description:\n"); parameters = new SearchParameters() { Filter = "baseRate lt 150", Select = new[] { "hotelId", "description" } }; results = indexClient.Documents.Search<Hotel>("*", parameters); WriteDocuments(results); Console.Write("Search the entire index, order by a specific field (lastRenovationDate) "); Console.Write("in descending order, take the top two results, and show only hotelName and "); Console.WriteLine("lastRenovationDate:\n"); parameters = new SearchParameters() { OrderBy = new[] { "lastRenovationDate desc" }, Select = new[] { "hotelName", "lastRenovationDate" }, Top = 2 }; results = indexClient.Documents.Search<Hotel>("*", parameters); WriteDocuments(results); Console.WriteLine("Search the entire index for the term 'motel':\n"); parameters = new SearchParameters(); results = indexClient.Documents.Search<Hotel>("motel", parameters); WriteDocuments(results); }
public DocumentService(ISearchIndexClient indexClient) { this.indexClient = indexClient; }
public DocumentOperations(ISearchIndexClient searchIndexClient) : this(searchIndexClient.Documents) { }
void TransferDocuments() { // set up the indexes _legacyIndexClient = new SearchIndexClient(Settings.LegacySearchServiceName, Settings.LegacySearchIndex, _legacySearchClient.Credentials); _targetIndexClient = new SearchIndexClient(Settings.TargetSearchServiceName, Settings.TargetSearchIndex, _targetSearchClient.Credentials); long totaldocs = _legacyIndexClient.Documents.Count().DocumentCount; Console.WriteLine(String.Format("Found {0} documents to transfer", totaldocs.ToString())); Console.WriteLine("Indexing"); // create an indexer to put the new docs in for us var indexer = new List<Microsoft.Azure.Search.Models.IndexAction>(); // now get all the docs, enumerate and push into new index // Liam tells us that things could go wrong if docs are added during this => pause indexing. // We get FULL docs back in groups of 50, by default, but i guess this could change - be good to have a queryable "safe" number // store anything that screws up List<string> failures = new List<string>(); // get groups of PAGE_SIZE and push int maxpages = (int)(totaldocs / PAGE_SIZE) + 1; for (int i = 0; i < maxpages; i++) { Console.Write("*"); // search config as it brings back 50 at a time Microsoft.Azure.Search.Models.SearchParameters sparams = new Microsoft.Azure.Search.Models.SearchParameters(); sparams.Top = PAGE_SIZE; sparams.Skip = i * PAGE_SIZE; // get all in that page var documents = _legacyIndexClient.Documents.Search("*", sparams).Results; // get the old docs transposed foreach (var olddoc in documents) { var document = new Microsoft.Azure.Search.Models.Document(); foreach (var key in olddoc.Document.Keys) { object value; if (olddoc.Document.TryGetValue(key, out value)) { document.Add(key, value); } else { failures.Add(key); } } if (FULL_TRACE) Console.WriteLine(String.Format("Indexed {0} ({1})", document[TITLE_FIELD], document[KEY_FIELD])); // now add to the indexer as a new item indexer.Add(new Microsoft.Azure.Search.Models.IndexAction( Microsoft.Azure.Search.Models.IndexActionType.Upload, document)); } if (!DO_NOT_COMMIT_DOCUMENTS) { //now get the indexer to batch import _targetIndexClient.Documents.Index(new Microsoft.Azure.Search.Models.IndexBatch(indexer)); } // reset and go again indexer.Clear(); } Console.WriteLine(String.Empty); Console.WriteLine(String.Format("Done. Short delay to let the indexing complete <queue music>")); // set a timeout so the indexing can complete before we check the count System.Threading.Thread.Sleep(FINAL_CHECK_INDEXING_TIMEOUT_SECONDS * 1000); // were all documents indexed? if (totaldocs == _targetIndexClient.Documents.Count().DocumentCount) { Console.WriteLine(String.Format("ALL DOCUMENTS INDEXED! Found {0} documents in the new index.", _targetIndexClient.Documents.Count().DocumentCount.ToString())); } else { Console.WriteLine(String.Format("Found {0} documents in the new index", _targetIndexClient.Documents.Count().DocumentCount.ToString())); if (failures.Count > 0) { Console.WriteLine("The following were not indexed:"); foreach (var item in failures) { Console.WriteLine(item); } } } }
public SearchService(IAzureEndpoints AzureEndpoints) { client = new SearchServiceClient(AzureEndpoints.GetSearchAccount(), new SearchCredentials(AzureEndpoints.GetSearchKey())); indexClient = client.Indexes.GetClient("entries"); }
private static void UploadDocuments(ISearchIndexClient indexClient) { var hotels = new Hotel[] { new Hotel() { HotelId = "1", BaseRate = 199.0, Description = "Best hotel in town", DescriptionFr = "Meilleur hôtel en ville", HotelName = "Fancy Stay", Category = "Luxury", Tags = new[] { "pool", "view", "wifi", "concierge" }, 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() { HotelId = "2", BaseRate = 79.99, Description = "Cheapest hotel in town", DescriptionFr = "Hôtel le moins cher en ville", HotelName = "Roach Motel", Category = "Budget", Tags = new[] { "motel", "budget" }, 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); }
internal Index(string name, IndexDefinition definition, ISearchIndexClient indexClient) : this(name, definition, new DocumentOperations(indexClient)) { }
// 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 SearchService(ISearchIndexClient indexClient) { this.indexClient = indexClient; }
public static Index ToIndex(this AzureIndex azureIdx, ISearchIndexClient idxClient) { return new Index(azureIdx.Name, FieldsToIndexDefinition(azureIdx.Fields), idxClient); }
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(); } }