예제 #1
0
        public async Task Index()
        {
            await using SearchResources resources = await SearchResources.CreateWithEmptyHotelsIndexAsync(this);

            SearchClient client = resources.GetQueryClient();

            try
            {
                #region Snippet:Azure_Search_Tests_Samples_Readme_Index
                IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                    IndexDocumentsAction.Upload(new Hotel {
                    Id = "783", Name = "Upload Inn"
                }),
                    IndexDocumentsAction.Merge(new Hotel {
                    Id = "12", Name = "Renovated Ranch"
                }));

                IndexDocumentsOptions options = new IndexDocumentsOptions {
                    ThrowOnAnyError = true
                };
                client.IndexDocuments(batch, options);
                #endregion Snippet:Azure_Search_Tests_Samples_Readme_Index
            }
            catch (RequestFailedException)
            {
                // Ignore the non-existent merge failure
            }
        }
예제 #2
0
        public void DeleteCustomerData(CustomerIndex customerToDelete)
        {
            IndexDocumentsBatch <CustomerIndex> batch = IndexDocumentsBatch.Create(IndexDocumentsAction.Delete(customerToDelete));
            IndexDocumentsOptions idxoptions          = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            _qryClient.IndexDocuments(batch, idxoptions);
        }
        static void ParallelBatchApplication(List <IndexDocumentsBatch <SemanticScholar> > batchJobs, SearchClient srchclient)
        {
            // Apply a set of batches in parallel
            var idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            Parallel.ForEach(batchJobs,
                             new ParallelOptions {
                MaxDegreeOfParallelism = MaxParallelUploads
            },
                             (b) =>
            {
                Interlocked.Increment(ref BatchUploadCounter);
                Console.WriteLine("Uploading Batch {0} with doc count {1}", BatchUploadCounter.ToString(), (BatchUploadCounter * MaxBatchSize).ToString());
                srchclient.IndexDocuments(b, idxoptions);
            });
        }
예제 #4
0
        static void Main(string[] args)
        {
            string serviceName = "lynx-searchengine-jsancho";
            string indexName   = "hotels-quickstart-v11";
            string apiKey      = "6C88A8613367F73135756A4CCFE1C24C";

            // Create a SearchIndexClient to send create/delete index commands
            Uri serviceEndpoint           = new Uri($"https://{serviceName}.search.windows.net/");
            AzureKeyCredential credential = new AzureKeyCredential(apiKey);
            SearchIndexClient  idxclient  = new SearchIndexClient(serviceEndpoint, credential);

            // Create a SearchClient to load and query documents
            SearchClient srchclient = new SearchClient(serviceEndpoint, indexName, credential);

            // Delete index if it exists
            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndexIfExists(indexName, idxclient);

            var searchClientOptions = new SearchClientOptions
            {
                Serializer = new JsonObjectSerializer(
                    new JsonSerializerOptions
                {
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                })
            };

            var builder = new FieldBuilder
            {
                Serializer = searchClientOptions.Serializer
            };

            var index = new SearchIndex(indexName)
            {
                Fields = builder.Build(typeof(Hotel))
            };

            Console.WriteLine("{0}", "Creating index...\n");
            idxclient.CreateIndex(index);

            // Load documents (using a subset of fields for brevity)
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(new Hotel {
                Id = "78", Name = "Upload Inn", Category = "hotel", Rate = 279, Updated = new DateTime(2018, 3, 1, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "54", Name = "Breakpoint by the Sea", Category = "motel", Rate = 162, Updated = new DateTime(2015, 9, 12, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "39", Name = "Debug Motel", Category = "motel", Rate = 159, Updated = new DateTime(2016, 11, 11, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "48", Name = "NuGet Hotel", Category = "hotel", Rate = 238, Updated = new DateTime(2016, 5, 30, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "12", Name = "Renovated Ranch", Category = "motel", Rate = 149, Updated = new DateTime(2020, 1, 24, 7, 0, 0)
            }));

            IndexDocumentsOptions idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            Console.WriteLine("{0}", "Loading index...\n");
            srchclient.IndexDocuments(batch, idxoptions);

            // Wait 2 secondsfor indexing to complete before starting queries (for demo and console-app purposes only)
            Console.WriteLine("Waiting for indexing...\n");
            System.Threading.Thread.Sleep(2000);

            // Call the RunQueries method to invoke a series of queries
            Console.WriteLine("Starting queries...\n");
            RunQueries(srchclient);

            // End the program
            Console.WriteLine("{0}", "Complete. Press any key to end this program...\n");
            Console.ReadKey();
        }
예제 #5
0
        static void Main(string[] args)
        {
            //Setting up db context
            var optionsBuilder = new DbContextOptionsBuilder <BankAppDataContext>();
            var context        = new BankAppDataContext(optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")).Options);

            string serviceName = Environment.GetEnvironmentVariable("SearchService");
            string indexName   = "customersearch";
            string apiKey      = Environment.GetEnvironmentVariable("SearchKey");

            // Create a SearchIndexClient to send create/delete index commands
            Uri serviceEndpoint           = new Uri($"https://{serviceName}.search.windows.net/");
            AzureKeyCredential credential = new AzureKeyCredential(apiKey);
            SearchIndexClient  idxclient  = new SearchIndexClient(serviceEndpoint, credential);

            // Create a SearchClient to load and query documents
            SearchClient qryclient = new SearchClient(serviceEndpoint, indexName, credential);


            // Define an index schema using SearchIndex
            // Create the index using SearchIndexClient
            SearchIndex index = new SearchIndex(indexName)
            {
                Fields =
                {
                    new SimpleField("customerId",    SearchFieldDataType.String)
                    {
                        IsKey = true,                IsSortable= true
                    },
                    new SearchableField("givenName")
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SearchableField("surname")
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SearchableField("city")
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SimpleField("streetAddress", SearchFieldDataType.String)
                    {
                        IsSortable = true,           IsHidden = true
                    },
                    new SimpleField("nationalId",    SearchFieldDataType.String)
                    {
                        IsSortable = true,           IsHidden = true
                    }
                }
            };

            idxclient.CreateIndex(index);

            //Fyller index med data
            var actions = new List <IndexDocumentsAction <CustomerIndex> >();

            foreach (var customers in context.Customers)
            {
                actions.Add(IndexDocumentsAction.Upload(new CustomerIndex
                {
                    CustomerId    = customers.CustomerId.ToString(),
                    City          = customers.City,
                    Givenname     = customers.Givenname,
                    NationalId    = customers.NationalId,
                    Streetaddress = customers.Streetaddress,
                    Surname       = customers.Surname
                }));
            }
            ;

            IndexDocumentsBatch <CustomerIndex> batch = IndexDocumentsBatch.Create(actions.ToArray());

            IndexDocumentsOptions idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            qryclient.IndexDocuments(batch, idxoptions);
        }
예제 #6
0
 public override async Task <Response <IndexDocumentsResult> > IndexDocumentsAsync <T>(IndexDocumentsBatch <T> batch, IndexDocumentsOptions options = null, CancellationToken cancellationToken = default)
 {
     try
     {
         SplitWhenRequested();
         return(ProcessResponse(await base.IndexDocumentsAsync(batch, options, cancellationToken).ConfigureAwait(false)));
     }
     finally
     {
         RaiseNotification();
     }
 }
예제 #7
0
 public override Response <IndexDocumentsResult> IndexDocuments <T>(IndexDocumentsBatch <T> batch, IndexDocumentsOptions options = null, CancellationToken cancellationToken = default)
 {
     try
     {
         SplitWhenRequested();
         return(ProcessResponse(base.IndexDocuments(batch, options, cancellationToken)));
     }
     finally
     {
         RaiseNotification();
     }
 }
        static void Main(string[] args)
        {
            string serviceName = "<YOUR-SEARCH-SERVICE-NAME>";
            string indexName   = "hotels-quickstart-v11";
            string apiKey      = "<YOUR-ADMIN-API-KEY>";

            // Create a SearchIndexClient to send create/delete index commands
            Uri serviceEndpoint           = new Uri($"https://{serviceName}.search.windows.net/");
            AzureKeyCredential credential = new AzureKeyCredential(apiKey);
            SearchIndexClient  idxclient  = new SearchIndexClient(serviceEndpoint, credential);

            // Create a SearchClient to load and query documents
            SearchClient qryclient = new SearchClient(serviceEndpoint, indexName, credential);

            // Delete index if it exists
            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndexIfExists(indexName, idxclient);

            // Define an index schema and create the index
            SearchIndex index = new SearchIndex(indexName)
            {
                Fields =
                {
                    new SimpleField("hotelId",            SearchFieldDataType.String)
                    {
                        IsKey = true,                     IsFilterable= true, IsSortable = true
                    },
                    new SearchableField("hotelName")
                    {
                        IsFilterable = true,              IsSortable = true
                    },
                    new SearchableField("hotelCategory")
                    {
                        IsFilterable = true,              IsSortable = true
                    },
                    new SimpleField("baseRate",           SearchFieldDataType.Int32)
                    {
                        IsFilterable = true,              IsSortable = true
                    },
                    new SimpleField("lastRenovationDate", SearchFieldDataType.DateTimeOffset)
                    {
                        IsFilterable = true,              IsSortable = true
                    }
                }
            };

            Console.WriteLine("{0}", "Creating index...\n");
            idxclient.CreateIndex(index);

            // Load documents (using a subset of fields for brevity)
            IndexDocumentsBatch <Hotel> batch = IndexDocumentsBatch.Create(
                IndexDocumentsAction.Upload(new Hotel {
                Id = "78", Name = "Upload Inn", Category = "hotel", Rate = 279, Updated = new DateTime(2018, 3, 1, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "54", Name = "Breakpoint by the Sea", Category = "motel", Rate = 162, Updated = new DateTime(2015, 9, 12, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "39", Name = "Debug Motel", Category = "motel", Rate = 159, Updated = new DateTime(2016, 11, 11, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "48", Name = "NuGet Hotel", Category = "hotel", Rate = 238, Updated = new DateTime(2016, 5, 30, 7, 0, 0)
            }),
                IndexDocumentsAction.Upload(new Hotel {
                Id = "12", Name = "Renovated Ranch", Category = "motel", Rate = 149, Updated = new DateTime(2020, 1, 24, 7, 0, 0)
            }));

            IndexDocumentsOptions idxoptions = new IndexDocumentsOptions {
                ThrowOnAnyError = true
            };

            Console.WriteLine("{0}", "Loading index...\n");
            qryclient.IndexDocuments(batch, idxoptions);

            // Wait 2 secondsfor indexing to complete before starting queries (for demo and console-app purposes only)
            Console.WriteLine("Waiting for indexing...\n");
            System.Threading.Thread.Sleep(2000);

            // Call the RunQueries method to invoke a series of queries
            Console.WriteLine("Starting queries...\n");
            RunQueries(qryclient);

            // End the program
            Console.WriteLine("{0}", "Complete. Press any key to end this program...\n");
            Console.ReadKey();
        }