public async Task RunAsync(CancellationToken cancellationToken = default)
        {
            if (await _searchClient.Indexes.ExistsAsync(_options.Value.Search.IndexName))
            {
                if (!_commandOptions.Value.Replace)
                {
                    _logger.LogError(
                        "Azure Search index {IndexName} already exists",
                        _options.Value.Search.IndexName);
                    return;
                }

                _logger.LogInformation(
                    "Deleting Azure Search index {IndexName}...",
                    _options.Value.Search.IndexName);

                await _searchClient.Indexes.DeleteAsync(_options.Value.Search.IndexName);
            }

            await _searchClient.Indexes.CreateAsync(new Index
            {
                Name      = _options.Value.Search.IndexName,
                Fields    = FieldBuilder.BuildForType <PackageDocument>(),
                Analyzers = new List <Analyzer>
                {
                    ExactMatchCustomAnalyzer.Instance
                }
            });

            _logger.LogInformation(
                "Created Azure Search index {IndexName}",
                _options.Value.Search.IndexName);
        }
Exemple #2
0
        public static bool IndexImageMetadata([ActivityTrigger] ImageInfo imageInfo, ILogger log)
        {
            string searchServiceName = Environment.GetEnvironmentVariable("SearchServiceName");
            string adminApiKey       = Environment.GetEnvironmentVariable("SearchServiceApiKey");

            log.LogInformation($"Indexing {imageInfo.Url}.");

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));

            var index = serviceClient.Indexes.CreateOrUpdate(new Index
            {
                Name   = "photos",
                Fields = FieldBuilder.BuildForType <ImageInfo>()
            });

            SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, "photos", new SearchCredentials(adminApiKey));

            imageInfo.Id       = Guid.NewGuid().ToString();
            imageInfo.Uploaded = DateTimeOffset.Now;
            var batch = IndexBatch.Upload(new[] { imageInfo });

            try
            {
                indexClient.Documents.Index(batch);
            }
            catch (IndexBatchException ibEx)
            {
                log.LogError(ibEx, $"Error indexing {imageInfo.Url}");
                throw;
            }

            return(true);
        }
Exemple #3
0
        private static void IndexSoccers()
        {
            var definition = new Index
            {
                Name   = "soccers",
                Fields = FieldBuilder.BuildForType <Player>()
            };

            if (!_searchClient.Indexes.Exists(definition.Name))
            {
                _searchClient.Indexes.Create(definition);
            }

            if (!_searchClient.DataSources.Exists("soccersdatasource"))
            {
                DataSource ds = new DataSource
                {
                    Name        = "soccersdatasource",
                    Type        = DataSourceType.AzureSql,
                    Credentials = new DataSourceCredentials(ConfigurationManager.ConnectionStrings["SoccerContex"].ToString()),
                    Container   = new DataContainer("Players")
                };
                _searchClient.DataSources.CreateOrUpdate(ds);
            }

            if (!_searchClient.Indexers.Exists("soccersindexer"))
            {
                var fm = new FieldMapping("Id", "IndexId");
                _searchClient.Indexers.Create(new Indexer("soccersindexer", "soccersdatasource", definition.Name, fieldMappings: new[] { fm }));
            }

            SoccerIndexClient = _searchClient.Indexes.GetClient(definition.Name);
        }
        private void CreateIndex()
        {
            Console.WriteLine("Creating index...");
            var indexExists = searchClient.Indexes.Exists(indexName);

            if (indexExists)
            {
                searchClient.Indexes.Delete(indexName);
            }

            var index = new Index(
                name: indexName,
                fields: FieldBuilder.BuildForType <ArticleIndex>(),
                scoringProfiles: CreateScoringProfile(),
                suggesters: CreateSuggester()
                );

            try
            {
                searchClient.Indexes.Create(index);
                Console.WriteLine("Done");
                serviceWorks = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("Something went wrong");
                serviceWorks = false;
            }
        }
Exemple #5
0
        static void Main(string[] args)
        {
            string indexName = "index";

            SearchServiceClient searchClient = new SearchServiceClient("jjsearch", new SearchCredentials("160B616814BC19916B887A75E4D47A44"));

            if (!searchClient.Indexes.Exists(indexName))
            {
                var newIndex = new Index()
                {
                    Name      = indexName,
                    Fields    = FieldBuilder.BuildForType <TestDocument>(),
                    Analyzers = new[]
                    {
                        new CustomAnalyzer()
                        {
                            Name         = "jj_custom_analyzer",
                            Tokenizer    = "jj_czech_tokenizer",
                            TokenFilters = new[] { TokenFilterName.AsciiFolding }
                        }
                    },
                    Tokenizers = new[]
                    {
                        new MicrosoftLanguageStemmingTokenizer()
                        {
                            Name     = "jj_czech_tokenizer",
                            Language = MicrosoftStemmingTokenizerLanguage.Czech
                        }
                    }
                };

                searchClient.Indexes.Create(newIndex);
            }

            ISearchIndexClient indexClient = searchClient.Indexes.GetClient(indexName);

            string[] dataToLoad = new string[]
            {
                "Jak se máš",
                "Jak se máte",
                "Máme se dobře",
                "Dobrých časů ubývá"
            };

            int i         = 0;
            var documents = new List <TestDocument>();

            foreach (string str in dataToLoad)
            {
                TestDocument document = new TestDocument()
                {
                    Id      = i.ToString(),
                    Content = str
                };
                documents.Add(document);
                i++;
            }
            var batch  = IndexBatch.Upload(documents);
            var result = indexClient.Documents.Index(batch);
        }
Exemple #6
0
        public async Task <IActionResult> RemoveCustomIndex(string key)
        {
            string searchServiceName          = _configuration["SearchServiceName"];
            string adminApiKey                = _configuration["SearchServiceAdminApiKey"];
            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));

            // Create custom index
            var definition = new Microsoft.Azure.Search.Models.Index()
            {
                Name   = "tomcustomindex",
                Fields = FieldBuilder.BuildForType <TomTestModel>()
            };

            //create Index
            if (!serviceClient.Indexes.Exists(definition.Name))
            {
                serviceClient.Indexes.Create(definition);
            }
            //var index = serviceClient.Indexes.Create(definition);

            if (!string.IsNullOrEmpty(key))
            {
                var keys = new List <string>();
                keys.Add(key);
                var batch = IndexBatch.Delete("fileId", keys);
                ISearchIndexClient indexClient = serviceClient.Indexes.GetClient("tomcustomindex");
                indexClient.Documents.Index(batch);

                return(Ok("remove succeed"));
            }

            return(StatusCode(StatusCodes.Status500InternalServerError));
        }
 private void CreateOrUpdateIndex <T>(string indexName)
 {
     string[] allowedOrigins = new string[] { "https://aresa.trifenix.io", "https://dev-aresa.trifenix.io", "https://agro.trifenix.io", "https://agro-dev.trifenix.io", "http://localhost:3000", "http://localhost:9009", "https://portal.azure.com" };
     _search.Indexes.CreateOrUpdate(new Index {
         Name = indexName, Fields = FieldBuilder.BuildForType <T>(), CorsOptions = new CorsOptions(allowedOrigins)
     });
 }
        private static Index CreateDemoIndex(SearchServiceClient serviceClient)
        {
            var index = new Index()
            {
                Name   = "demoindex",
                Fields = FieldBuilder.BuildForType <DemoIndex>()
            };

            try
            {
                bool exists = serviceClient.Indexes.Exists(index.Name);

                if (exists)
                {
                    serviceClient.Indexes.Delete(index.Name);
                }

                serviceClient.Indexes.Create(index);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create the index\n Exception message: {0}\n", e.Message);
                ExitProgram("Cannot continue without an index");
            }

            return(index);
        }
Exemple #9
0
        private Index InitializeIndex <TDocument>(string name, bool addScoringProfile)
        {
            var index = new Index
            {
                Name      = name,
                Fields    = FieldBuilder.BuildForType <TDocument>(),
                Analyzers = new List <Analyzer>
                {
                    DescriptionAnalyzer.Instance,
                    ExactMatchCustomAnalyzer.Instance,
                    PackageIdCustomAnalyzer.Instance,
                },
                Tokenizers = new List <Tokenizer>
                {
                    PackageIdCustomTokenizer.Instance,
                },
                TokenFilters = new List <TokenFilter>
                {
                    IdentifierCustomTokenFilter.Instance,
                    TruncateCustomTokenFilter.Instance,
                },
            };

            if (addScoringProfile)
            {
                var scoringProfile = DefaultScoringProfile.Create(_options.Value.Scoring);

                index.ScoringProfiles = new List <ScoringProfile> {
                    scoringProfile
                };
                index.DefaultScoringProfile = DefaultScoringProfile.Name;
            }

            return(index);
        }
Exemple #10
0
        public static async Task ForceCreateIndexAsync(ISearchServiceClient serviceClient, string indexName)
        {
            if (await serviceClient.Indexes.ExistsAsync(indexName))
            {
                Console.WriteLine($"Index '{indexName}' exists. Deleting the existing index...");

                await serviceClient.Indexes.DeleteAsync(indexName);

                Console.WriteLine($"Index '{indexName}' was deleted.");
            }

            Console.WriteLine($"Creating index '{indexName}'...");

            var definition = new Index()
            {
                Name            = indexName,
                Fields          = FieldBuilder.BuildForType <Document>(),
                ScoringProfiles = new List <ScoringProfile>()
                {
                    Document.CreatePrimaryFieldFavoredScoringProfile()
                },
            };

            await serviceClient.Indexes.CreateAsync(definition);

            Console.WriteLine($"Index '{indexName}' was created.");
        }
Exemple #11
0
        public Task EnsureIndexExists()
        {
            var index = new Index
            {
                Name   = IndexName,
                Fields = FieldBuilder.BuildForType <ReleaseInfo>(),
                //Suggesters =
                //{
                //    // TODO: Suggester names as constants for easy reference
                //    new Suggester("issue number", SuggesterSearchMode.AnalyzingInfixMatching,
                //        nameof(ReleaseInfo.IssueNumbers).ToCamelCase()),
                //    new Suggester("release notes", SuggesterSearchMode.AnalyzingInfixMatching,
                //        nameof(ReleaseInfo.ReleaseNotes).ToCamelCase())
                //},
                //Analyzers =
                //{
                //    // TODO: Analyzer name as constant
                //    new CustomAnalyzer(
                //        "lowercase_keyword",
                //        TokenizerName.Keyword,
                //        new List<TokenFilterName> {TokenFilterName.Lowercase})
                //}
            };

            return(SearchService.Indexes.CreateOrUpdateAsync(index));
        }
Exemple #12
0
        private static void IndexBooks()
        {
            var definition = new Index
            {
                Name   = "books",
                Fields = FieldBuilder.BuildForType <Book>()
            };

            if (!_searchClient.Indexes.Exists(definition.Name))
            {
                _searchClient.Indexes.Create(definition);
            }

            if (!_searchClient.DataSources.Exists("booksdatasource"))
            {
                DataSource ds = new DataSource
                {
                    Name        = "booksdatasource",
                    Type        = DataSourceType.AzureSql,
                    Credentials = new DataSourceCredentials(ConfigurationManager.ConnectionStrings["BookContext"].ToString()),
                    Container   = new DataContainer("Books")
                };
                _searchClient.DataSources.CreateOrUpdate(ds);
            }

            if (!_searchClient.Indexers.Exists("booksindexer"))
            {
                var nameFm = new FieldMapping("Name", FieldMappingFunction.Base64Decode());
                _searchClient.Indexers.Create(new Indexer("booksindexer", "booksdatasource", definition.Name, fieldMappings: new[] { nameFm }));
            }

            _indexClient = _searchClient.Indexes.GetClient(definition.Name);
        }
 private Index GetIndexDefenition(string indexName)
 {
     return(new Index
     {
         Name = indexName,
         Fields = FieldBuilder.BuildForType <T>(),
         Suggesters = new List <Suggester>
         {
             new Suggester
             {
                 Name = Constants.DefaultSuggesterName,
                 SourceFields = suggesterBuilder.BuildForType <T>(),
             }
         },
         ScoringProfiles = new List <ScoringProfile>
         {
             new ScoringProfile
             {
                 Name = Constants.SearchScoringProfileName,
                 TextWeights = new TextWeights
                 {
                     Weights = weightingBuilder.BuildForType <T>()
                 }
             }
         }
     });
 }
Exemple #14
0
        private static Index CreateIndexDefinition(string indexName)
        {
            var result = new Index()
            {
                Name            = indexName,
                Fields          = FieldBuilder.BuildForType <ApartmentDataIndexDocument>(),
                ScoringProfiles = new[]
                {
                    new ScoringProfile
                    {
                        Name        = "names",
                        TextWeights = new TextWeights
                        {
                            Weights = new Dictionary <string, double>
                            {
                                { nameof(ApartmentDataIndexDocument.Name), 3.0 },
                                { nameof(ApartmentDataIndexDocument.FormerName), 3.0 }
                            }
                        }
                    }
                },
                Suggesters = new[]
                {
                    new Suggester("sg", nameof(ApartmentDataIndexDocument.Name)
                                  , nameof(ApartmentDataIndexDocument.FormerName)
                                  , nameof(ApartmentDataIndexDocument.StreetAddress)
                                  , nameof(ApartmentDataIndexDocument.City))
                },
                DefaultScoringProfile = "names"
            };

            return(result);
        }
Exemple #15
0
        private static void CreateIndex(string apiKey)
        {
            var buildForType = FieldBuilder.BuildForType <IndexablePub>();

            var definition = new Index()
            {
                Name        = IndexName,
                Fields      = buildForType,
                CorsOptions = new CorsOptions(new List <string>()
                {
                    "*"
                }),
                Suggesters = new List <Suggester>()
                {
                    new Suggester("sg", "name")
                }
            };

            using (var searchServiceClient = CreateSearchServiceClient(apiKey))
            {
                Console.Out.WriteLine($"Deleting Index");
                searchServiceClient.Indexes.Delete(IndexName);
                Thread.Sleep(5000);
                Console.Out.WriteLine($"Creating Index");
                searchServiceClient.Indexes.Create(definition);
                Thread.Sleep(5000);
            }
        }
Exemple #16
0
        private static void CreateIndex(SearchServiceClient searchClient)
        {
            Console.WriteLine("\nCreating index...");

            // Define the index with a name and fields based on the MargiesIndex class
            var index = new Index()
            {
                Name   = IndexName,
                Fields = FieldBuilder.BuildForType <MargiesIndex>()
            };

            // Create the index
            try
            {
                bool exists = searchClient.Indexes.Exists(index.Name);

                // Delete and recreate the index if it already exists
                if (exists)
                {
                    searchClient.Indexes.Delete(index.Name);
                }

                searchClient.Indexes.Create(index);
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to create the index\n Exception message: {0}\n", e.Message);
            }
        }
Exemple #17
0
        public static void CreateSearchIndex()
        {
            try
            {
                //connect to Azure Search
                SearchServiceClient serviceClient = CreateSearchServiceClient();

                //defining the suggester
                Suggester sg = new Suggester
                {
                    Name         = "eecip_suggest",
                    SourceFields = new List <string>()
                    {
                        "Name"
                    }
                };

                //defining the scoring profile //boosts items updated in the last 180 days
                ScoringProfile sp = new ScoringProfile {
                    Name = "date_scoring"
                };

                var freshnessFunction = new FreshnessScoringFunction()
                {
                    FieldName     = "LastUpdated",
                    Boost         = 20,
                    Parameters    = new FreshnessScoringParameters(new TimeSpan(180, 0, 0, 0)),
                    Interpolation = ScoringFunctionInterpolation.Linear
                };
                // Assigns the freshness function to the scoring profile
                sp.Functions = new List <ScoringFunction>()
                {
                    freshnessFunction
                };


                //define the index (the fields, and link in the suggester and scoring)
                var definition = new Index()
                {
                    Name       = "eecip",
                    Fields     = FieldBuilder.BuildForType <EECIP_Index>(),
                    Suggesters = new List <Suggester> {
                        sg
                    },
                    ScoringProfiles = new List <ScoringProfile>()
                    {
                        sp
                    }
                };



                serviceClient.Indexes.Create(definition);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected override Index GetIndexStructure(string indexName)
        {
            var index = new Index
            {
                Name            = indexName,
                Fields          = FieldBuilder.BuildForType <Entities.Document>(new SearchIndexEnumToIntContractResolver()),
                ScoringProfiles = new List <ScoringProfile>
                {
                    new ScoringProfile("ScoringProfile")
                    {
                        TextWeights = new TextWeights(new Dictionary <string, double>
                        {
                            [nameof(Entities.Document.Name)]    = 4,
                            [nameof(Entities.Document.Tags)]    = 3.5,
                            [nameof(Entities.Document.Content)] = 3,
                        }),
                        FunctionAggregation = ScoringFunctionAggregation.Sum,
                        Functions           = new List <ScoringFunction>
                        {
                            new TagScoringFunction(Entities.Document.CourseNameField, 3.2, new TagScoringParameters(TagsCourseParameter)),
                            new TagScoringFunction(Entities.Document.UniversityIdFieldName, 3, new TagScoringParameters(TagsUniversityParameter)),
                            new TagScoringFunction(nameof(Entities.Document.Tags), 1.5, new TagScoringParameters("Tag")),
                        }
                    },
                    new ScoringProfile(ScoringProfile)
                    {
                        TextWeights = new TextWeights(new Dictionary <string, double>
                        {
                            [nameof(Entities.Document.Name)]    = 4,
                            [Entities.Document.CourseNameField] = 3.2,
                            [nameof(Entities.Document.Content)] = 3,
                        }),
                        FunctionAggregation = ScoringFunctionAggregation.Sum,
                        Functions           = new List <ScoringFunction>
                        {
                            new TagScoringFunction(Entities.Document.CourseNameField, 3.2, new TagScoringParameters(TagsCourseParameter)),
                            new TagScoringFunction(Entities.Document.UniversityIdFieldName, 3, new TagScoringParameters(TagsUniversityParameter)),
                            new TagScoringFunction(nameof(Entities.Document.Country), 1.5, new TagScoringParameters(TagsCountryParameter)),
                        }
                    }
                },
            };

            index.Fields.Add(new Field("MetaContent", DataType.String));
            index.Fields.Add(new Field("Language", DataType.String)
            {
                IsFilterable = true
            });
            index.Fields.Add(new Field("Course", DataType.String)
            {
                IsFilterable = true
            });
            index.Fields.Add(new Field("Type", DataType.Int32)
            {
                IsFilterable = true,
                IsFacetable  = true
            });
            return(index);
        }
        private async Task CreateCombinedIndex(ISearchServiceClient serviceClient)
        {
            var indexDefinition = new Index
            {
                Name      = SearchConstants.CombinedIndexName,
                Fields    = FieldBuilder.BuildForType <CombinedIndexedItem>(),
                Analyzers = new List <Analyzer>
                {
                    new CustomAnalyzer
                    {
                        Name         = SearchConstants.AdvancedAnalyzerName,
                        TokenFilters = new List <TokenFilterName>
                        {
                            TokenFilterName.Lowercase,
                            TokenFilterName.AsciiFolding,
                            //TokenFilterName.Phonetic,
                            //TokenFilterName.EdgeNGram
                        },
                        Tokenizer = TokenizerName.Create(SearchConstants.NGramTokenizerName),
                        //Tokenizer = TokenizerName.EdgeNGram,
                    },
                    new CustomAnalyzer
                    {
                        Name         = SearchConstants.AdvancedAnalyzer_2_Name,
                        Tokenizer    = TokenizerName.EdgeNGram,
                        TokenFilters = new List <TokenFilterName>
                        {
                            TokenFilterName.Lowercase,
                            "myNGramTokenFilter"
                        }
                    }
                },
                Tokenizers = new List <Tokenizer>
                {
                    new NGramTokenizer(SearchConstants.NGramTokenizerName)
                    {
                        MinGram    = 4,
                        MaxGram    = 30,
                        TokenChars = new List <TokenCharacterKind>
                        {
                            TokenCharacterKind.Letter,
                            TokenCharacterKind.Digit,
                        }
                    }
                },
                TokenFilters = new List <TokenFilter>
                {
                    new NGramTokenFilterV2
                    {
                        Name    = "myNGramTokenFilter",
                        MinGram = 1,
                        MaxGram = 100
                    }
                }
            };

            await serviceClient.Indexes.CreateAsync(indexDefinition);
        }
 private async Task CreateIndexAsync()
 {
     var definition = new Index()
     {
         Name   = INDEX_NAME,
         Fields = FieldBuilder.BuildForType <SearchPackage>()
     };
     await _client.Indexes.CreateOrUpdateWithHttpMessagesAsync(INDEX_NAME, definition);
 }
 private static void CreateIndexs(string indexName, SearchServiceClient searchServiceClient)
 {
     var definition = new Microsoft.Azure.Search.Models.Index()
     {
         Name   = indexName,
         Fields = FieldBuilder.BuildForType <Hotel>()
     };
     var res = searchServiceClient.Indexes.Create(definition);
 }
 static FindTypeApi()
 {
     // Ensure index exists
     SearchServiceClient.Indexes.CreateOrUpdate(new Index
     {
         Name   = Constants.SearchServiceIndexName,
         Fields = FieldBuilder.BuildForType <PackageDocument>()
     });
 }
Exemple #23
0
        private static void CreateHotelsIndex(SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name   = "hotels",
                Fields = FieldBuilder.BuildForType <Hotel>()
            };

            serviceClient.Indexes.Create(definition);
        }
Exemple #24
0
        // Create an index whose fields correspond to the properties of the Hotel class.
        // The Address property of Hotel will be modeled as a complex field.
        // The properties of the Address class in turn correspond to sub-fields of the Address complex field.
        // The fields of the index are defined by calling the FieldBuilder.BuildForType() method.
        private static void CreateIndex(string indexName, SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name   = indexName,
                Fields = FieldBuilder.BuildForType <Hotel>()
            };

            serviceClient.Indexes.Create(definition);
        }
Exemple #25
0
        static void CreateIndex(string indexName, SearchServiceClient serviceClient)
        {
            Microsoft.Azure.Search.Models.Index index = new Microsoft.Azure.Search.Models.Index()
            {
                Name   = indexName,
                Fields = FieldBuilder.BuildForType <Customer>()
            };

            serviceClient.Indexes.Create(index);
        }
        public async Task RecreateIndexAsync()
        {
            await DeleteIndexAsync();

            await _searchAdminClient.Indexes.CreateAsync(new Index
            {
                Name   = _configuration.IndexName,
                Fields = FieldBuilder.BuildForType <PersonDto>(),
            });
        }
        private async Task CreateIndex <T>(string indexName) where T : ISearchable
        {
            var definition = new Index
            {
                Name   = indexName,
                Fields = FieldBuilder.BuildForType <T>()
            };

            await _searchServiceClient.Indexes.CreateAsync(definition);
        }
Exemple #28
0
        private void CreateIndex(SearchServiceClient client)
        {
            var def = new Index()
            {
                Name   = index,
                Fields = FieldBuilder.BuildForType <SearchIndex>()
            };

            client.Indexes.Create(def);
        }
Exemple #29
0
        public async Task CreateIndexAsync <T>(SearchServiceClient searchClient, string indexName) where T : class
        {
            var indexDefinition = new Index()
            {
                Name   = indexName,
                Fields = FieldBuilder.BuildForType <T>()
            };

            await searchClient.Indexes.CreateAsync(indexDefinition);
        }
        private static void CreateIndex(SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name   = "databaseobject",
                Fields = FieldBuilder.BuildForType <DatabaseObject>()
            };

            serviceClient.Indexes.Create(definition);
        }