public IEnumerable<string> SearchKeywordLocation(string keyword, double latitude, double longitude)
        {
            var results = new List<string>();

            var searchRequest = new SearchRequest();

            // Set the credentials using a valid Bing Maps key
            searchRequest.Credentials = new SearchService.Credentials();
            searchRequest.Credentials.ApplicationId = GetBingMapsApplicationKey();

            //Create the search query
            var ssQuery = new StructuredSearchQuery();
            ssQuery.Keyword = keyword;
            ssQuery.Location = string.Format("{0}, {1}", latitude, longitude);
            searchRequest.StructuredQuery = ssQuery;

            //Make the search request 
            SearchResponse searchResponse;
            using (var searchService = new SearchServiceClient("BasicHttpBinding_ISearchService"))
            {
                searchResponse = searchService.Search(searchRequest);
            }

            foreach (var searchResult in searchResponse.ResultSets[0].Results)
            {
                results.Add(string.Format("{0} ({1})", searchResult.Name, searchResult.Distance));
            }
            return results;
        }
Example #2
0
 private void button2_Click(object sender, EventArgs e)
 {
     using (var serarcClient = new SearchServiceClient())
     {
         var result = serarcClient.Search(GetSearch());
         MessageBox.Show(result.Metadata.Status.ToString());
     }
 }
        public static SearchServiceClient GetSearchServiceClient(this SearchServiceFixture fixture)
        {
            var factory = new CSMTestEnvironmentFactory();
            TestEnvironment currentEnvironment = factory.GetTestEnvironment();
            Uri baseUri = currentEnvironment.GetBaseSearchUri(ExecutionMode.CSM, fixture.SearchServiceName);

            SearchServiceClient client =
                new SearchServiceClient(new SearchCredentials(fixture.PrimaryApiKey), baseUri);

            return TestBaseCopy.AddMockHandler<SearchServiceClient>(ref client);
        }
Example #4
0
		public static void Open(string host) {
			CustomBinding binding = new CustomBinding(
				new BinaryMessageEncodingBindingElement(),
				new TcpTransportBindingElement());

			SearchService = new SearchServiceClient(binding, new EndpointAddress(host + "Search"));
			PlaylistService = new PlaylistServiceClient(binding, new EndpointAddress(host + "Playlist"));
			PlayerService = new PlayerServiceClient(binding, new EndpointAddress(host + "Player"));

            pOpenedServices = 0;

			SearchService.OpenCompleted += OnServiceOpenCompleted;
			PlaylistService.OpenCompleted += OnServiceOpenCompleted;
			PlayerService.OpenCompleted += OnServiceOpenCompleted;

			SearchService.OpenAsync();
			PlaylistService.OpenAsync();
			PlayerService.OpenAsync();
		}
Example #5
0
        static async Task Main(string[] args)
        {
            SearchServiceClient searchService = CreateSearchServiceClient(configuration);

            string indexName      = "idx-" + configuration["IndexNameforHospital"];
            string indexerName    = "idxr-" + configuration["IndexNameforHospital"];
            string dataSourceName = "dsn-" + configuration["IndexNameforHospital"];
            string tableName      = configuration["TableNameforHospital"];

            Console.WriteLine("Deleting index...\n");
            await DeleteIndexIfExists(indexName, searchService);

            Console.WriteLine("Creating index....\n");
            await CreateIndexForHospital(indexName, searchService);

            Console.WriteLine("Indexing Azure Sql Db hospital data...\n");
            await CreateAndRunSqlDbIndexer(indexName, indexerName, dataSourceName, tableName, searchService);

            indexName      = "idx-" + configuration["IndexNameforDoctor"];
            indexerName    = "idxr-" + configuration["IndexNameforDoctor"];
            dataSourceName = "dsn-" + configuration["IndexNameforDoctor"];
            tableName      = configuration["TableNameforDoctor"];


            Console.WriteLine("Deleting index...\n");
            await DeleteIndexIfExists(indexName, searchService);

            Console.WriteLine("Creating index....\n");
            await CreateIndexForDoctor(indexName, searchService);

            Console.WriteLine("Indexing Azure Sql Db hospital data...\n");
            await CreateAndRunSqlDbIndexer(indexName, indexerName, dataSourceName, tableName, searchService);

            Console.WriteLine("Complete. Press any key to end applications...\n");
            Console.ReadKey();
        }
Example #6
0
        protected void TestCanSearchWithSynonyms()
        {
            SearchServiceClient searchClient = Data.GetSearchServiceClient();

            const string synonymMapName = "names";
            SynonymMap   synonymMap     = new SynonymMap(name: synonymMapName, format: SynonymMapFormat.Solr, synonyms: "luxury,fancy");

            searchClient.SynonymMaps.Create(synonymMap);

            SearchIndexClient client = GetClientForQuery();
            Index             index  = searchClient.Indexes.Get(client.IndexName);

            index.Fields.First(f => f.Name == "hotelName").SynonymMaps = new[] { synonymMapName };

            searchClient.Indexes.CreateOrUpdate(index);

            var searchParameters =
                new SearchParameters()
            {
                QueryType    = QueryType.Full,
                SearchFields = new[] { "hotelName" },
                Select       = new[] { "hotelName", "baseRate" }
            };

            DocumentSearchResult <Hotel> response =
                client.Documents.Search <Hotel>("luxury", searchParameters);

            var expectedDoc = new Hotel()
            {
                HotelName = "Fancy Stay", BaseRate = 199
            };

            Assert.NotNull(response.Results);
            Assert.Equal(1, response.Results.Count);
            Assert.Equal(expectedDoc, response.Results.First().Document);
        }
Example #7
0
        public async Task CreateClient()
        {
            await using SearchResources resources = await SearchResources.GetSharedHotelsIndexAsync(this);

            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Samples_CreateClient
            // Get the service endpoint and API key from the environment
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            AzureKeyCredential credential = new AzureKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));

            // Create a new SearchServiceClient
            SearchServiceClient search = new SearchServiceClient(endpoint, credential);
            /*@@*/ search = InstrumentClient(new SearchServiceClient(endpoint, credential, GetSearchClientOptions()));

            // Perform an operation
            Response <SearchServiceStatistics> stats = search.GetServiceStatistics();
            Console.WriteLine($"You are using {stats.Value.Counters.IndexCounter.Usage} indexes.");
            #endregion Snippet:Azure_Search_Tests_Samples_CreateClient

            Assert.AreEqual(1, stats.Value.Counters.IndexCounter.Usage);
        }
Example #8
0
        private static void CreateIndex(SearchServiceClient client)
        {
            var IndexDefination = new Index()
            {
                Name   = IndexName,
                Fields = new[]
                {
                    new Field("id", DataType.String)
                    {
                        IsKey = true
                    },
                    new Field("class", DataType.String)
                    {
                        IsSearchable = true, IsFacetable = true, IsRetrievable = true, IsSortable = true, IsFilterable = true
                    },
                    new Field("defaultOption", DataType.Boolean)
                    {
                        IsFacetable = true, IsRetrievable = true, IsSortable = true, IsFilterable = true
                    },
                    new Field("description", DataType.String)
                    {
                        IsSearchable = true, IsFacetable = true, IsRetrievable = true, IsSortable = true, IsFilterable = true
                    },
                    new Field("name", DataType.String)
                    {
                        IsSearchable = true, IsFacetable = true, IsRetrievable = true, IsSortable = true, IsFilterable = true
                    },
                    new Field("nameKey", DataType.String)
                    {
                        IsFacetable = true, IsRetrievable = true, IsSortable = true, IsFilterable = true
                    },
                }
            };

            client.Indexes.Create(IndexDefination);
        }
Example #9
0
        private static void AddKnowledgeStore(SearchServiceClient searchClient, AppConfig appConfig, ref Index index, ref Indexer indexer, ref Skillset skillset)
        {
            // Add the skills from previous steps
            AddSentimentAnalysisSkill(ref index, ref indexer, ref skillset);
            AddCustomTranslateSkill(ref index, ref indexer, ref skillset, appConfig.FunctionApp);
            //AddCustomSummarizerSkill(ref index, ref indexer, ref skillset, appConfig.FunctionApp);

            // Convert the Skillset into a JSON string.

            var skillsetJson = CognitiveSearchHelper.GetSkillsetJson(skillset).GetAwaiter().GetResult();

            //  Insert ShaperSkill into the Skillset as a JSON string
            Console.WriteLine("Adding Shaper Skill to the pipeline to set up the table projections.");
            var shaperSkillJson             = GetJsonFromFile("shaper-skill").GetAwaiter().GetResult();
            var skillsetJsonWithShaperSkill = CognitiveSearchHelper.InsertSkillAsJson(skillsetJson, shaperSkillJson);

            // Insert knowledge store JSON string into the Skillset JSON
            Console.WriteLine("Inserting the knowledge store with table projections.");
            var updatedSkillset = InsertKnowledgeStoreJson(skillsetJsonWithShaperSkill, appConfig.BlobStorage);

            // Create the search pipeline using the updated skillset JSON. No SDK exists yet for doing this using the SDK objects, so must use the REST API to accomplish adding a knowledge store via code.
            Console.WriteLine("Rebuilding cognitive search pipeline...");
            CognitiveSearchHelper.CreateCognitiveSearchPipeline(searchClient, appConfig.Search, index, indexer, skillset.Name, updatedSkillset).GetAwaiter().GetResult();
        }
Example #10
0
        // Demonstrates index delete, create, load, and query
        // Commented-out code is uncommented in later steps
        static void Main(string[] args)
        {
            Task p = new AzSearchDemoMDS.Program().Main();

            p.Wait();


            Console.ReadLine();
            IConfigurationBuilder builder       = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            IConfigurationRoot    configuration = builder.Build();



            SearchServiceClient serviceClient = CreateSearchServiceClient(configuration);

            string indexName = configuration["SearchIndexName"];

            Console.WriteLine("{0}", "Deleting index...\n");
            //DeleteIndexIfExists (indexName, serviceClient);

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

            // Uncomment next 3 lines in "2 - Load documents"
            ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);

            Console.WriteLine("{0}", "Uploading documents...\n");
            //UploadDocuments(indexClient);

            // Uncomment next 2 lines in "3 - Search an index"
            Console.WriteLine("{0}", "Searching index...\n");
            RunQueries(indexClient);

            Console.WriteLine("{0}", "Complete.  Press any key to end application...\n");
            Console.ReadKey();
        }
Example #11
0
        public void CanIndexAndRetrieveModelWithExtraProperties()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index = Book.DefineIndex();
                serviceClient.Indexes.Create(index);

                SearchIndexClient client = Data.GetSearchIndexClient(index.Name);
                var resolver             = new MyCustomContractResolver();
                client.SerializationSettings.ContractResolver   = resolver;
                client.DeserializationSettings.ContractResolver = resolver;

                string bookJson =
                    @"{ ""ISBN"": ""123"", ""Title"": ""The Hobbit"", ""Author"": ""J.R.R.Tolkien"", ""Rating"": 5 }";

                // Real customers would just use JsonConvert, but that would break the test.
                var expectedBook = SafeJsonConvert.DeserializeObject <ReviewedBook>(bookJson);

                DocumentIndexResult result = client.Documents.Index(IndexBatch.Upload(new[] { expectedBook }));

                Assert.Equal(1, result.Results.Count);
                AssertIndexActionSucceeded("123", result.Results[0], 201);

                SearchTestUtilities.WaitForIndexing();

                Assert.Equal(1, client.Documents.Count());

                ReviewedBook actualBook = client.Documents.Get <ReviewedBook>(expectedBook.ISBN);

                Assert.Equal(0, actualBook.Rating);
                actualBook.Rating = 5;
                Assert.Equal(expectedBook, actualBook);
            });
        }
Example #12
0
        private static async Task <List <ImageInformation> > SearchImagesAsync(string searchQuery)
        {
            List <ImageInformation> images = new List <ImageInformation>();

            SearchServiceClient serviceClient = new SearchServiceClient("[YOUR AZURE SEARCH SERVICE NAME]", new SearchCredentials("[YOUR AZURE SEARCH API KEY]"));

            var sp = new SearchParameters()
            {
                Top = 10
            };

            ISearchIndexClient   indexClient = serviceClient.Indexes.GetClient("tags");
            DocumentSearchResult response    = await indexClient.Documents.SearchAsync(searchQuery.Trim() + "*", sp);

            images = (from result in response.Results
                      select new ImageInformation()
            {
                Id = new Guid((string)result.Document["id"]),
                Caption = (result.Document["caption"] + "").ToFirstCharUpper(),
                Tags = ((string[])result.Document["tags"]).ToList()
            }).ToList();

            return(images);
        }
Example #13
0
        static void DoAzureSeach()
        {
            //var endpoint = "https://thnetazsearch.search.windows.net";

            var client = new SearchServiceClient("thnetazsearch", new SearchCredentials("6E74D968A34AD1E6A47C9EEAD75ABD09"));

            var index    = client.Indexes.GetClient("mbafiap");
            var document = new CustomDocument()
            {
                Id       = "rm39611",
                Cidade   = "Mauá",
                Email    = "*****@*****.**",
                Endereco = "rua alcindo dian, 50",
                Idade    = 28,
                Nome     = "Edvaldo Farias"
            };

            var batch = IndexBatch.MergeOrUpload(new List <CustomDocument>()
            {
                document
            });

            index.Documents.Index(batch);
        }
Example #14
0
        static void Main(string[] args)
        {
            string searchServiceName = "daniel";

            string apiKey = "38E9EC1BBC7F012747BE72FDBECDFB3F";

            SearchServiceClient serviceClientApi = Helper.Initialize(searchServiceName, apiKey);
            ISearchIndexClient  indexClientApi   = serviceClientApi.Indexes.GetClient(Helper.IndexName);

            Uploader uploader = new Uploader();

            uploader.Upload(indexClientApi);

            Searcher searcher = new Searcher();

            Console.WriteLine("Begin searching Perodua....");
            searcher.SearchDocuments(indexClientApi, "Perodua");
            Console.WriteLine(string.Empty);

            // https://docs.microsoft.com/en-us/rest/api/searchservice/odata-expression-syntax-for-azure-search
            Console.WriteLine("Begin searching Hatchback category....");
            searcher.SearchDocuments(indexClientApi, "*", "Category eq 'Hatchback'");
            Console.WriteLine(string.Empty);

            Console.WriteLine("Begin searching Price more than 100,000....");
            searcher.SearchDocuments(indexClientApi, "*", "Price gt 100000 and Category eq 'Sedan'");
            Console.WriteLine(string.Empty);

            Console.WriteLine("Begin searching by Category facet");
            List <string> facets = new List <string>();

            facets.Add("Category");
            searcher.SearchDocuments(indexClientApi, "*", null, facets);

            Console.ReadLine();
        }
        private void TestCanSearchWithCustomConverter <TBook, TAuthor>(Action <SearchIndexClient> customizeSettings = null)
            where TBook : CustomBookBase <TAuthor>, new()
            where TAuthor : CustomAuthor, new()
        {
            customizeSettings = customizeSettings ?? (client => { });

            SearchServiceClient serviceClient = Data.GetSearchServiceClient();

            Index index = Book.DefineIndex();

            serviceClient.Indexes.Create(index);
            SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

            customizeSettings(indexClient);

            var doc = new TBook()
            {
                InternationalStandardBookNumber = "123",
                Name       = "Lord of the Rings",
                AuthorName = new TAuthor()
                {
                    FullName = "J.R.R. Tolkien"
                },
                PublishDateTime = new DateTime(1954, 7, 29)
            };

            var batch = IndexBatch.Upload(new[] { doc });

            indexClient.Documents.Index(batch);
            SearchTestUtilities.WaitForIndexing();

            DocumentSearchResult <TBook> response = indexClient.Documents.Search <TBook>("*");

            Assert.Equal(1, response.Results.Count);
            Assert.Equal(doc, response.Results[0].Document);
        }
Example #16
0
        private DocumentSuggestResult <Document> GetSuggestorResults(SearchServiceClient serviceClient)
        {
            try
            {
                //var index = serviceClient.Indexes.Get(indexName);
                //#region Add Suggestors
                //var suggestor = new Suggester(autoCompleteSuggestor);
                //suggestor.SourceFields.Add("firstName");
                //suggestor.SourceFields.Add("lastName");

                //index.Suggesters = new List<Suggester>();
                //index.Suggesters.Add(suggestor);
                //#endregion

                //serviceClient.Indexes.CreateOrUpdate(index);

                SuggestParameters suggestParameters = new SuggestParameters
                {
                    Select           = new[] { "firstName", "lastName" },
                    UseFuzzyMatching = true,
                    Top = 20
                };

                //Since we are updating the main index in the upper code, we need to get the searchIndexClient here again
                SearchIndexClient searchIndexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(queryKey));

                var result = searchIndexClient.Documents.Suggest("Hug", autoCompleteSuggestor, suggestParameters);
                _logger.Information("GetSuggestorResults {@result}", result);
                return(result);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
                throw ex;
            }
        }
Example #17
0
        static void Main(string[] args)
        {
            IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");

            _config = builder.Build();

            string indexName = _config["SearchIndexName"];

            searchServiceClient = CreateSearchServiceClient();

            Console.WriteLine($"Deleting the existing index.. : {indexName}");
            DeleteIndexIfExists(indexName, searchServiceClient);

            Console.WriteLine($"Creating the index : {indexName}");
            CreateIndexs(indexName, searchServiceClient);

            Console.WriteLine($"Uploading the documents ...");
            searchIndexClient = searchServiceClient.Indexes.GetClient(indexName);
            UploadDocuments(searchIndexClient);

            RunQueries(searchIndexClient);

            Console.ReadLine();
        }
Example #18
0
        static void Main(string[] args)
        {
            IConfigurationBuilder builder       = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            IConfigurationRoot    configuration = builder.Build();
            SearchServiceClient   serviceClient = CreateSearchServiceClient(configuration);

            string indexName = "catgang";

            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndexIfExists(serviceClient, indexName);

            Console.WriteLine("Creating index...");
            CreateIndex <Cat>(serviceClient, indexName);

            var indexClient = GetSearchIndexClient(serviceClient, indexName);

            UploadDocuments <Cat>(indexClient, Samples.GenerateSampleCats());

            ISearchIndexClient indexClientForQueries = CreateSearchIndexClient(configuration, indexName);

            RunQueries <Cat>(indexClientForQueries);

            SeeMe();
        }
Example #19
0
        public async static Task <List <SearchResultInformation> > SearchAsync(string searchQuery)
        {
            List <SearchResultInformation> searchResults = new List <SearchResultInformation>();

            SearchServiceClient serviceClient = new SearchServiceClient("tagur", new SearchCredentials(SearchConstants.SearchApiKey));

            var sp = new SearchParameters();

            ISearchIndexClient   indexClient = serviceClient.Indexes.GetClient("tags");
            DocumentSearchResult response    = await indexClient.Documents.SearchAsync(searchQuery, sp);

            var results = response.Results;
            var next    = results.Select(s => s.Document).Where(w => w.ContainsKey("id"));

            searchResults = (from result in next
                             select new SearchResultInformation()
            {
                Id = new Guid((string)result["id"]),
                Caption = ((string)result["caption"] + "").ToFirstCharUpper(),
                Tags = ((string[])result["tags"]).ToList(),
            }).ToList();

            return(searchResults);
        }
Example #20
0
        public void CanUpdateDataSource()
        {
            Run(() =>
            {
                SearchServiceClient searchClient = Data.GetSearchServiceClient();

                DataSource initial = CreateTestDataSource();

                DataSourceDefinitionResponse createResponse = searchClient.DataSources.Create(initial);
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                DataSource updated  = CreateTestDataSource();
                updated.Name        = initial.Name;
                updated.Container   = new DataContainer("somethingdifferent");
                updated.Description = "somethingdifferent";
                updated.DataChangeDetectionPolicy   = new HighWaterMarkChangeDetectionPolicy("rowversion");
                updated.DataDeletionDetectionPolicy = new SoftDeleteColumnDeletionDetectionPolicy("isDeleted", "1");

                DataSourceDefinitionResponse updateResponse = searchClient.DataSources.CreateOrUpdate(updated);
                Assert.Equal(HttpStatusCode.OK, updateResponse.StatusCode);

                AssertDataSourcesEqual(updated, updateResponse.DataSource);
            });
        }
        //Async method to get the data from web service.
        async void btnFindButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string searchString = (findTextBox.Text).ToString();

                busyIndicator.IsBusy = true;

                client = new SearchServiceClient();

                //everything after this line is made to wait until the execution is finished.
                //while it waits, the application control is handed over to the calling method.
                //That way, the UI is kept responsive.
                var result = await client.SearchItemsAsync(searchString);

                client.Close();

                grdData.ItemsSource = result;

                busyIndicator.IsBusy = false;

            }
            catch (Exception ex)
            {
                busyIndicator.IsBusy = false;

                //do not show error for deliberately aborted requests.
                if (!ex.Message.Contains("The request was aborted: The request was canceled."))
                {
                    MessageBox.Show("Unexpected error: " + ex.Message,
                        "Async Await Demo", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

            this.UpdateLayout();
        }
Example #22
0
        public DocumentSearchClient(IConfiguration configuration)
        {
            try
            {
                searchServiceName = configuration.GetSection("SearchServiceName")?.Value;
                apiKey            = configuration.GetSection("SearchApiKey")?.Value;
                IndexName         = configuration.GetSection("SearchIndexName")?.Value;
                idField           = configuration.GetSection("KeyField")?.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)
            {
                // If you get an exceptio here, most likely you have not set your
                // credentials correctly in appsettings.json
                throw new ArgumentException(e.Message.ToString());
            }
        }
Example #23
0
        public void CanRoundtripStaticallyTypedPrimitiveCollections()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index = ModelWithPrimitiveCollections.CreateIndex();
                serviceClient.Indexes.Create(index);

                SearchIndexClient indexClient = Data.GetSearchIndexClient(index.Name);

                var expectedDoc =
                    new ModelWithPrimitiveCollections()
                {
                    Key   = "1",
                    Bools = new[] { true, false },
                    Dates = new[]
                    {
                        new DateTimeOffset(2019, 4, 14, 14, 24, 0, TimeSpan.FromHours(-7)),
                        new DateTimeOffset(1999, 12, 31, 23, 59, 59, TimeSpan.Zero)
                    },
                    Doubles = new[] { double.NegativeInfinity, 0.0, 2.78, double.NaN, 3.14, double.PositiveInfinity },
                    Ints    = new[] { 1, 2, 3, 4, -13, 5, 0 },
                    Longs   = new[] { -9_999_999_999_999_999L, 832_372_345_832_523L },
Example #24
0
        private static void Main(string[] args)
        {
            const string searchServiceName = "YourServiceName";
            const string accesskey         = "YourAccessKey";
            var          serviceClient     = new SearchServiceClient(searchServiceName, new SearchCredentials(accesskey));

            DeleteIndexIfExists(serviceClient);

            CreateIndex(serviceClient);

            var cars = CreateCars();

            var indexClient = serviceClient.Indexes.GetClient("cars");
            var batch       = IndexBatch.Upload(cars);

            indexClient.Documents.Index(batch);

            // necessary, otherwise no search result are found
            Thread.Sleep(1000);

            var searchResults = Search(indexClient);

            PrintSearchResult(searchResults);
        }
        public override void Initialize(MockContext context)
        {
            base.Initialize(context);

            SearchServiceClient searchClient = this.GetSearchServiceClient();

            TargetIndexName = SearchTestUtilities.GenerateName();

            DataSourceName = SearchTestUtilities.GenerateName();

            var index = new Index(
                TargetIndexName,
                new[]
            {
                new Field("feature_id", DataType.String)
                {
                    IsKey = true
                },
                new Field("feature_name", DataType.String)
                {
                    IsFilterable = true, IsSearchable = true
                },
                new Field("feature_class", DataType.String),
                new Field("state", DataType.String)
                {
                    IsFilterable = true, IsSearchable = true
                },
                new Field("county_name", DataType.String)
                {
                    IsFilterable = true, IsSearchable = true
                },
                new Field("elevation", DataType.Int32)
                {
                    IsFilterable = true
                },
                new Field("map_name", DataType.String)
                {
                    IsFilterable = true, IsSearchable = true
                },
                new Field("history", DataType.Collection(DataType.String))
                {
                    IsSearchable = true
                },
                new Field("myEntities", DataType.Collection(DataType.String))
                {
                    IsSearchable = true
                },
                new Field("myText", DataType.String)
                {
                    IsSearchable = true
                }
            });

            searchClient.Indexes.Create(index);

            var dataSource =
                DataSource.AzureSql(
                    name: DataSourceName,
                    sqlConnectionString: AzureSqlReadOnlyConnectionString,
                    tableOrViewName: AzureSqlTestTableName);

            searchClient.DataSources.Create(dataSource);
        }
Example #26
0
        private SearchServiceClient CreateSearchServiceClient()
        {
            SearchServiceClient serviceClient = new SearchServiceClient(_azureSearchOptions.SearchServiceName, new SearchCredentials(_azureSearchOptions.SearchServiceQueryApiKey));

            return(serviceClient);
        }
Example #27
0
        public void MockData()
        {
            SearchServiceClient serviceClient = CreateSearchServiceClient();
            var definition = new Index()
            {
                Name   = "news",
                Fields = new[]
                {
                    new Field("Id", DataType.String)
                    {
                        IsKey = true, IsSearchable = false, IsFilterable = false, IsSortable = false, IsRetrievable = true
                    },
                    new Field("Title", DataType.String)
                    {
                        IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsRetrievable = true
                    },
                    new Field("Category", DataType.String)
                    {
                        IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsRetrievable = true
                    },
                    new Field("Spot", DataType.String)
                    {
                        IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsRetrievable = true
                    },
                    new Field("Content", DataType.String)
                    {
                        IsKey = false, IsSearchable = true, IsFilterable = true, IsSortable = true, IsRetrievable = true
                    },
                }
            };

            //serviceClient.Indexes.Delete("news");

            serviceClient.Indexes.CreateOrUpdate(definition);

            var indexClient = serviceClient.Indexes.GetClient("news");

            for (int i = 0; i < 1500; i++)
            {
                var data = GetData(i);

                foreach (var news in data)
                {
                    try
                    {
                        Console.WriteLine(news.Id);


                        var actions =
                            new[]
                        {
                            IndexAction.Upload(
                                new
                            {
                                Id       = news.Id.ToString(),
                                Content  = news.Content,
                                Title    = news.Title,
                                Spot     = news.Spot,
                                Category = news.Category
                            })
                        };

                        var batch = IndexBatch.New(actions);

                        indexClient.Documents.Index(batch);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }
Example #28
0
        protected override async Task <IEnumerable <IDocument> > ExecuteConfigAsync(IDocument input, IExecutionContext context, IMetadata values)
        {
            string        searchServiceName = values.GetString(SearchServiceName) ?? throw new ExecutionException("Invalid search service name");
            string        indexName         = values.GetString(IndexName) ?? throw new ExecutionException("Invalid search index name");
            string        apiKey            = values.GetString(ApiKey) ?? throw new ExecutionException("Invalid search API key");
            IList <Field> fields            = values.GetList <Field>(Fields)?.ToList() ?? throw new ExecutionException("Invalid search fields");

            SearchServiceClient client = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));

            // Delete the index if it currently exists (recreating is the easiest way to update it)
            CorsOptions corsOptions = null;

            if (await client.Indexes.ExistsAsync(indexName, null, context.CancellationToken))
            {
                // Get the CORS options because we'll need to recreate those
                Microsoft.Azure.Search.Models.Index existingIndex = await client.Indexes.GetAsync(indexName, null, context.CancellationToken);

                corsOptions = existingIndex.CorsOptions;

                // Delete the existing index
                context.LogDebug($"Deleting existing search index {indexName}");
                await client.Indexes.DeleteAsync(indexName, null, null, context.CancellationToken);
            }

            // Create the index
            Microsoft.Azure.Search.Models.Index index = new Microsoft.Azure.Search.Models.Index
            {
                Name        = indexName,
                Fields      = fields,
                CorsOptions = corsOptions
            };
            context.LogDebug($"Creating search index {indexName}");
            await client.Indexes.CreateAsync(index, null, context.CancellationToken);

            // Upload the documents to the search index in batches
            context.LogDebug($"Uploading {context.Inputs.Length} documents to search index {indexName}...");
            ISearchIndexClient indexClient = client.Indexes.GetClient(indexName);
            int start = 0;

            do
            {
                // Create the dynamic search documents and batch
                IndexAction <Microsoft.Azure.Search.Models.Document>[] indexActions = context.Inputs
                                                                                      .Skip(start)
                                                                                      .Take(BatchSize)
                                                                                      .Select(doc =>
                {
                    Microsoft.Azure.Search.Models.Document searchDocument = new Microsoft.Azure.Search.Models.Document();
                    foreach (Field field in fields)
                    {
                        if (doc.ContainsKey(field.Name))
                        {
                            searchDocument[field.Name] = doc.Get(field.Name);
                        }
                    }
                    return(IndexAction.Upload(searchDocument));
                })
                                                                                      .ToArray();
                IndexBatch <Microsoft.Azure.Search.Models.Document> indexBatch = IndexBatch.New(indexActions);

                // Upload the batch with exponential retry for failures
                await Policy
                .Handle <IndexBatchException>()
                .WaitAndRetryAsync(
                    5,
                    attempt =>
                {
                    context.LogWarning($"Failure while uploading batch {(start / BatchSize) + 1}, retry number {attempt}");
                    return(TimeSpan.FromSeconds(Math.Pow(2, attempt)));
                },
                    (ex, _) => indexBatch = ((IndexBatchException)ex).FindFailedActionsToRetry(indexBatch, fields.Single(x => x.IsKey == true).Name))
                .ExecuteAsync(async ct => await indexClient.Documents.IndexAsync(indexBatch, null, ct), context.CancellationToken);

                context.LogDebug($"Uploaded {start + indexActions.Length} documents to search index {indexName}");
                start += 1000;
            }while (start < context.Inputs.Length);

            return(context.Inputs);
        }
Example #29
0
        public override void Initialize(MockContext context)
        {
            base.Initialize(context);

            SearchServiceClient searchClient = this.GetSearchServiceClient();

            IndexName = SearchTestUtilities.GenerateName();

            // This is intentionally a different index definition than the one returned by IndexTests.CreateTestIndex().
            // That index is meant to exercise serialization of the index definition itself, while this one is tuned
            // more for exercising document serialization, indexing, and querying operations.
            var index =
                new Index()
            {
                Name   = IndexName,
                Fields = new[]
                {
                    new Field("hotelId", DataType.String)
                    {
                        IsKey = true, IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("baseRate", DataType.Double)
                    {
                        IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("description", DataType.String)
                    {
                        IsSearchable = true
                    },
                    new Field("descriptionFr", AnalyzerName.FrLucene),
                    new Field("hotelName", DataType.String)
                    {
                        IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("category", DataType.String)
                    {
                        IsSearchable = true, IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("tags", DataType.Collection(DataType.String))
                    {
                        IsSearchable = true, IsFilterable = true, IsFacetable = true
                    },
                    new Field("parkingIncluded", DataType.Boolean)
                    {
                        IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("smokingAllowed", DataType.Boolean)
                    {
                        IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("lastRenovationDate", DataType.DateTimeOffset)
                    {
                        IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("rating", DataType.Int32)
                    {
                        IsFilterable = true, IsSortable = true, IsFacetable = true
                    },
                    new Field("location", DataType.GeographyPoint)
                    {
                        IsFilterable = true, IsSortable = true
                    }
                },
                Suggesters = new[]
                {
                    new Suggester(
                        name: "sg",
                        searchMode: SuggesterSearchMode.AnalyzingInfixMatching,
                        sourceFields: new[] { "description", "hotelName" })
                },
                ScoringProfiles = new[]
                {
                    new ScoringProfile("nearest")
                    {
                        FunctionAggregation = ScoringFunctionAggregation.Sum,
                        Functions           = new[]
                        {
                            new DistanceScoringFunction("location", 2, new DistanceScoringParameters("myloc", 100))
                        }
                    }
                }
            };

            searchClient.Indexes.Create(index);

            // Give the index time to stabilize before running tests.
            // TODO: Remove this workaround once the retry hang bug is fixed.
            TestUtilities.Wait(TimeSpan.FromSeconds(20));
        }
        private static void InternalCreateDataSource(IConfigurationRoot configuration, SearchServiceClient serviceClient)
        {
            DataSource ds = new DataSource()
            {
                Name        = "travelblobs",
                Type        = DataSourceType.AzureBlob,
                Container   = new DataContainer("documents"),
                Credentials = new DataSourceCredentials(configuration["storageconnection"])
            };

            serviceClient.DataSources.CreateOrUpdate(ds);
        }
 public AzureSearchEngine(SearchServiceClient searchClient)
 {
     this.searchClient = searchClient;
 }
 public AzureSearchServiceHelper(string searchServiceName, string searchServiceAdminKey)
 {
     client = new SearchServiceClient(searchServiceName, new SearchCredentials(searchServiceAdminKey));
     client.HttpClient.DefaultRequestHeaders.Add("api-key", searchServiceAdminKey);
 }
 public SearchController(IConfiguration configuration)
 {
     serviceClient = new SearchServiceClient(configuration["AzureSearch:AzureSearchServiceName"], new SearchCredentials(configuration["AzureSearch:AzureSearchApiKey"]));
 }
Example #34
0
        private static SearchServiceClient CreateSearchServiceClient(SearchOptions searchOptions)
        {
            SearchServiceClient serviceClient = new SearchServiceClient(searchOptions.Name, new SearchCredentials(searchOptions.ApiKey));

            return(serviceClient);
        }
Example #35
0
        static void InitializeServices()
        {
            // create the storage containers if needed
            CloudBlobClient blobClient = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={EnrichFunction.IMAGE_AZURE_STORAGE_ACCOUNT_NAME};AccountKey={EnrichFunction.IMAGE_BLOB_STORAGE_ACCOUNT_KEY};EndpointSuffix=core.windows.net").CreateCloudBlobClient();

            blobClient.GetContainerReference(EnrichFunction.IMAGE_BLOB_STORAGE_CONTAINER).CreateIfNotExists(BlobContainerPublicAccessType.Blob);
            blobClient.GetContainerReference(EnrichFunction.LIBRARY_BLOB_STORAGE_CONTAINER).CreateIfNotExists(BlobContainerPublicAccessType.Off);

            // create the index if needed
            var serviceClient = new SearchServiceClient(EnrichFunction.AZURE_SEARCH_SERVICE_NAME, new SearchCredentials(EnrichFunction.AZURE_SEARCH_ADMIN_KEY));

            if (!serviceClient.Indexes.List().Indexes.Any(i => i.Name == EnrichFunction.AZURE_SEARCH_INDEX_NAME))
            {
                var definition = new Index()
                {
                    Name        = EnrichFunction.AZURE_SEARCH_INDEX_NAME,
                    Fields      = FieldBuilder.BuildForType <HOCRDocument>(),
                    CorsOptions = new CorsOptions()
                    {
                        AllowedOrigins = new[] { "*" }
                    }
                };

                serviceClient.Indexes.CreateOrUpdate(definition);
            }

            // test the pipeline and index
            Console.WriteLine("Sending a test image through the pipeline");
            using (var file = File.OpenRead(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test-image.jpg")))
            {
                EnrichFunction.Run(file, "TEST_IMAGE", log).Wait();
            }

            Console.WriteLine("Querying the test image");
            var indexClient = serviceClient.Indexes.GetClient(EnrichFunction.AZURE_SEARCH_INDEX_NAME);
            var results     = indexClient.Documents.Search("ABC12345XYZ", new SearchParameters()
            {
                Facets          = new[] { "tags", "people", "places", "adult", "racy" },
                HighlightFields = new[] { "text" },
            });

            // TODO: Add some additional validations for fields
            if (results.Results.Count > 0)
            {
                Console.WriteLine("Item found in index");
            }
            else
            {
                Console.WriteLine("Item missing from index");
            }

            Console.WriteLine("Delete the test item");
            var deleteResult = indexClient.Documents.Index(IndexBatch.Delete("id", new[] { "TEST_IMAGE" }));

            if (deleteResult.Results.Count > 0)
            {
                Console.WriteLine("Item deleted from the index");
            }
            else
            {
                Console.WriteLine("could not delete the item");
            }
        }
Example #36
0
        public string SearchKeywordLocation(string keywordLocation)
        {
            String results = "";

            SearchRequest searchRequest = new SearchRequest();

            // Set the credentials using a valid Bing Maps key
            searchRequest.Credentials = new SearchService.Credentials();
            searchRequest.Credentials.ApplicationId = key;

            //Create the search query
            StructuredSearchQuery ssQuery = new StructuredSearchQuery();
            string[] parts = keywordLocation.Split(';');
            ssQuery.Keyword = parts[0];
            ssQuery.Location = parts[1];
            searchRequest.StructuredQuery = ssQuery;

            //Define options on the search
            searchRequest.SearchOptions = new SearchOptions();
            searchRequest.SearchOptions.Filters =
                new FilterExpression()
                {
                    PropertyId = 3,
                    CompareOperator = CompareOperator.GreaterThanOrEquals,
                    FilterValue = 8.16
                };

            //Make the search request
            SearchServiceClient searchService = new SearchServiceClient("BasicHttpBinding_ISearchService");
            SearchResponse searchResponse = searchService.Search(searchRequest);

            //Parse and format results
            if (searchResponse.ResultSets[0].Results.Length > 0)
            {
                StringBuilder resultList = new StringBuilder("");
                for (int i = 0; i < searchResponse.ResultSets[0].Results.Length; i++)
                {
                    resultList.Append(String.Format("{0}. {1}\n", i + 1,
                        searchResponse.ResultSets[0].Results[i].Name));
                }

                results = resultList.ToString();
            }
            else
                results = "No results found";

            return results;
        }