Пример #1
0
        private static void CreateIndexer(SearchServiceClient searchServiceClient)
        {
            if (!searchServiceClient.DataSources.List().Any(d => d.Name == "concertssql"))
            {
                int test2 = searchServiceClient.DataSources.List().Count();
                Debug.WriteLine(test2);
                bool test3 = searchServiceClient.DataSources.List().Any(d => d.Name == "concertssql");
                Debug.WriteLine(test3);
                string connectionString = WingtipTicketApp.ConstructConnection(WingtipTicketApp.Config.PrimaryDatabaseServer,
                                                                               WingtipTicketApp.Config.TenantDbName);
                searchServiceClient.DataSources.CreateOrUpdate(new DataSource
                {
                    Name = "concertssql",
                    Type = "azuresql",
                    Container = new DataContainer { Name = "ConcertSearch" },
                    Credentials = new DataSourceCredentials { ConnectionString = connectionString },
                    DataChangeDetectionPolicy = new HighWaterMarkChangeDetectionPolicy("RowVersion")
                });
            }

            if (!searchServiceClient.Indexers.List().Any(i => i.Name == "fromsql"))
            {
                    searchServiceClient.Indexers.CreateOrUpdate(new Indexer
                    {
                        Name = "fromsql",
                        DataSourceName = "concertssql",
                        TargetIndexName = "concerts",
                        Schedule = new IndexingSchedule { Interval = TimeSpan.FromMinutes(5), StartTime = DateTimeOffset.Now }
                    });
            }
        }
Пример #2
0
        /*
        public void AddAzurePropertySearchIndex()
        {
            string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
            DeletePropertiesIndexIfExists(serviceClient);
            CreatePropertiesIndex(serviceClient);

            List<Property> allProperties = Repository.Query<Property>(x => x.Deleted != true).ToList();
            if (allProperties != null && allProperties.Count > 0)
            {
                SearchIndexClient indexClient = serviceClient.Indexes.GetClient(PROPERTY_INDEX_NAME);
                List<AzureSearchProperty> transform = new List<AzureSearchProperty>(allProperties.Count);
                foreach (Property property in allProperties)
                    transform.Add(new AzureSearchProperty(property));
                UploadDocuments(indexClient, transform);
            }
        }
        */
        public void AddAzureRetsPropertySearchIndex()
        {
            string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
            DeleteRetsPropertiesIndexIfExists(serviceClient);
            CreateRetsPropertiesIndex(serviceClient);

            List<Property> allProperties = Repository.Query<Property>(x => true).ToList();
            if (allProperties != null && allProperties.Count > 0)
            {
                SearchIndexClient indexClient = serviceClient.Indexes.GetClient(RETS_PROPERTY_INDEX_NAME);
                List<AzureSearchRProperty> transform = new List<AzureSearchRProperty>(allProperties.Count);
                foreach (Property property in allProperties)
                {
                    AzureSearchRProperty tProperty = new AzureSearchRProperty(property);
                    tProperty.SearchLocality = _localities.GetSearchRegion(property.PostalCode.Value);
                    transform.Add(tProperty);
                    if (transform.Count == 500)
                    {
                        UploadDocuments(indexClient, transform);
                        transform.Clear();
                    }
                }
                UploadDocuments(indexClient, transform);
            }
        }
Пример #3
0
 public SearchService(string searchServiceName, string apiKey, string index)
 {
     this.searchServiceName = searchServiceName;
     this.apiKey = apiKey;
     serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
     indexClient = serviceClient.Indexes.GetClient(index);
 }
Пример #4
0
        // This sample shows how to delete, create, upload documents and query an index
        static void Main(string[] args)
        {
            string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

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

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

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

            SearchIndexClient indexClient = serviceClient.Indexes.GetClient("hotels");
            
            Console.WriteLine("{0}", "Uploading documents...\n");
            UploadDocuments(indexClient);
            
            Console.WriteLine("{0}", "Searching documents 'fancy wifi'...\n");
            SearchDocuments(indexClient, searchText: "fancy wifi");

            Console.WriteLine("\n{0}", "Filter documents with category 'Luxury'...\n");
            SearchDocuments(indexClient, searchText: "*", filter: "category eq 'Luxury'");

            Console.WriteLine("{0}", "Complete.  Press any key to end application...\n");
            Console.ReadKey();
        }
 private static void DeleteIndexIfExists(SearchServiceClient serviceClient)
 {
     if (serviceClient.Indexes.Exists("test"))
     {
         serviceClient.Indexes.Delete("test");
     }
 }
Пример #6
0
 private static void CreateIndex(SearchServiceClient searchServiceClient)
 {
     if (!searchServiceClient.Indexes.Exists("concerts"))
     {
         searchServiceClient.Indexes.Create(new Index
         {
             Name = "concerts",
             Fields = new List<Field>
                 {
                     new Field { Name = "ConcertId", Type = DataType.String, IsKey = true, IsFilterable = true, IsRetrievable = true },
                     new Field { Name = "ConcertName", Type = DataType.String, IsRetrievable = true },
                     new Field { Name = "ConcertDate", Type = DataType.DateTimeOffset, IsFilterable = true, IsFacetable = true, IsSortable = true, IsRetrievable = true },
                     new Field { Name = "VenueId", Type = DataType.Int32, IsFilterable = true, IsRetrievable = true },
                     new Field { Name = "VenueName", Type = DataType.String, IsFilterable = true, IsFacetable = true },
                     new Field { Name = "VenueCity", Type = DataType.String, IsFilterable = true, IsFacetable = true },
                     new Field { Name = "VenueState", Type = DataType.String, IsFilterable = true, IsFacetable = true },
                     new Field { Name = "VenueCountry", Type = DataType.String, IsFilterable = true, IsFacetable = true },
                     new Field { Name = "PerformerId", Type = DataType.Int32, IsFilterable = true, IsRetrievable = true },
                     new Field { Name = "PeformerName", Type = DataType.String, IsFilterable = true, IsFacetable = true },
                     new Field { Name = "FullTitle", Type = DataType.String, IsRetrievable = true, IsSearchable = true }
                 },
             Suggesters = new List<Suggester>
                 {
                     new Suggester
                     {
                         Name = "sg",
                         SearchMode = SuggesterSearchMode.AnalyzingInfixMatching,
                         SourceFields = { "FullTitle" }
                     }
                 }
         });
     }
 }
Пример #7
0
        StatusController()
        {
            string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

            // Create an HTTP reference to the catalog index
            _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
        }
        private static SearchServiceClient CreateSearchServiceClient()
        {
            string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            string adminApiKey = ConfigurationManager.AppSettings["SearchServiceAdminApiKey"];

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
            return serviceClient;
        }
        private static void CreateHotelsIndex(SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name = "hotels",
                Fields = FieldBuilder.BuildForType<Hotel>()
            };

            serviceClient.Indexes.Create(definition);
        }
Пример #10
0
        public static void Configure()
        {
            var searchServiceClient = new SearchServiceClient(WingtipTicketApp.Config.SearchServiceName,
                                                              new SearchCredentials(WingtipTicketApp.Config.SearchServiceKey));

            CreateIndex(searchServiceClient);
            CreateIndexer(searchServiceClient);
            //searchServiceClient.Indexers.Run("fromsql");

            WingtipTicketApp.SearchIndexClient = searchServiceClient.Indexes.GetClient("concerts");
        }
Пример #11
0
        public AzureSearchService(IConfigurationRoot configurationRoot)
        {
            var searchServiceName = configurationRoot["AppSettings:SearchServiceName"];
            var searchApiKey = configurationRoot["AppSettings:SearchServiceApiKey"];
            var tweetIndexName = configurationRoot["AppSettings:TweetIndexName"];

            var serviceClient = new SearchServiceClient(
                searchServiceName, new SearchCredentials(searchApiKey));

            _indexClient = serviceClient.Indexes.GetClient(tweetIndexName);
        }
Пример #12
0
        public async override Task ExecuteAsync()
        {
            var searchServiceName = "beerdrinkin";
            string apiKey;

            if (!Services.Settings.TryGetValue("SEARCH_PRIMARY_ADMIN_KEY", out apiKey))
            {
                Services.Log.Error("Failed to find Search Service admin key");
            }

            var serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
            var indexClient = serviceClient.Indexes.GetClient("beers");

            Services.Log.Info("Started updating Beer Drinkin's search engine index!");

            var definition = new Index()
            {
                Name = "beers",
                Fields = new[]
                {
                    new Field("id", DataType.String)                            { IsKey = true },
                    new Field("name", DataType.String)                          { IsSearchable = true, IsFilterable = true },
                    new Field("description", DataType.String)                   { IsSearchable = true, IsFilterable = true },
                    new Field("brewery", DataType.String)                       { IsSearchable = true, IsFilterable = true },
                    new Field("abv", DataType.String)                           { IsFilterable = true, IsSortable = true }
                }
            };

            await serviceClient.Indexes.CreateOrUpdateAsync(definition);

            try
            {
                var context = new BeerDrinkinContext();
                var beerItems = context.Beers;
                if (beerItems.Count() != 0)
                {
                    var beers = beerItems.ToArray();
                    await indexClient.Documents.IndexAsync(IndexBatch.Create(beers.Select(IndexAction.Create)));
                }
                else
                    Services.Log.Info("Failed to find any beers in the DB to index");

            }   
            catch (IndexBatchException e)
            {
                // Sometimes when your Search service is under load, indexing will fail for some of the documents in
                // the batch. Depending on your application, you can take compensating actions like delaying and
                // retrying. For this simple demo, we just log the failed document keys and continue.
                Services.Log.Error( $"Failed to index some of the documents: {string.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key))}");
            }

            Services.Log.Info("Finished updating Beer Drinkin's search engine index!");
        }
 public AutocompleteResult Autocomplete(AzureSuggestQuery q, string suggester, AutocompleteParameters options)
 {
     try
     {
         Microsoft.Azure.Search.SearchServiceClient _searchClient = new Microsoft.Azure.Search.SearchServiceClient(_searchService.Replace("https://", "").Replace(".search.windows.net", ""), new SearchCredentials(_apiKey));
         ISearchIndexClient _indexClient = _searchClient.Indexes.GetClient(IndexName);
         return(_indexClient.Documents.Autocomplete(q.Query, suggester, options));
     }
     catch (Exception e)
     {
         throw e;
     }
 }
        public async Task CreateIndexAsync()
        {
            var searchclient = new SearchServiceClient(new SearchCredentials(_adminkey), new Uri(_baseURI));

            var index = GetIndex();

            var response = await searchclient.Indexes.CreateOrUpdateAsync(index);

            if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
            {
                var dialog = new MessageDialog("Error creating the index");
                await dialog.ShowAsync();
            }
        }
Пример #15
0
 public SearchController()
 {
     //var appSettings = ConfigurationManager.AppSettings;
     //var searchServiceName = appSettings["SearchServiceName"];
     //var searchServiceApiKey = appSettings["SearchServiceApiKey"];
     //var indexName = appSettings["IndexName"];
     //var searchServiceName = CloudConfigurationManager.GetSetting("SearchServiceName");
     //var searchServiceApiKey = CloudConfigurationManager.GetSetting("SearchServiceApiKey");
     //var indexName = CloudConfigurationManager.GetSetting("IndexName");
     _searchServiceName = CloudConfigurationManager.GetSetting("SearchServiceName");
     _searchServiceApiKey = CloudConfigurationManager.GetSetting("SearchServiceApiKey");
     _indexName = CloudConfigurationManager.GetSetting("IndexName");
     _searchServiceClient = new SearchServiceClient(_searchServiceName, new SearchCredentials(_searchServiceApiKey));
     _searchIndexClient = _searchServiceClient.Indexes.GetClient(_indexName);
 }
Пример #16
0
        public StorageController()
        {
            var azureStorageConnectionString = CloudConfigurationManager.GetSetting("AzureStorage.ConnectionString");
            var azureStorageContainerReference = CloudConfigurationManager.GetSetting("AzureStorage.ContainerReference");

            _cloudStorageAccount = CloudStorageAccount.Parse(azureStorageConnectionString);
            _cloudBlobClient = _cloudStorageAccount.CreateCloudBlobClient();
            _cloudBlobContainer = _cloudBlobClient.GetContainerReference(azureStorageContainerReference);

            _searchServiceName = CloudConfigurationManager.GetSetting("SearchServiceName");
            _searchServiceApiKey = CloudConfigurationManager.GetSetting("SearchServiceApiKey");
            _indexName = CloudConfigurationManager.GetSetting("IndexName");
            _searchServiceClient = new SearchServiceClient(_searchServiceName,
                new SearchCredentials(_searchServiceApiKey));
            _searchIndexClient = _searchServiceClient.Indexes.GetClient(_indexName);
        }
Пример #17
0
        static FeaturesSearch()
        {
            try
            {
                string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
                string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

                // Create an HTTP reference to the catalog index
                _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
                _indexClient = _searchClient.Indexes.GetClient("geonames");
            }
            catch (Exception e)
            {
                errorMessage = e.Message.ToString();
            }
        }
        private static void CreateIndex(SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name = "test",
                Fields = new[]
                {
                    new Field("id", DataType.String)                            { IsKey = true },
                    new Field("title", DataType.String)                         { IsSearchable = true, IsFilterable = true },
                    new Field("author", DataType.String)                        { IsSearchable = true, IsFilterable = true },
                    new Field("createdDate", DataType.DateTimeOffset)           { IsFilterable = true, IsSortable = true, IsFacetable = true },
                }
            };

            serviceClient.Indexes.Create(definition);
        }
Пример #19
0
        static AzureSearchHelper()
        {
            try
            {
                string searchServiceName = ValidationHelper.GetString(ConfigurationManager.AppSettings["AzureSearchName"], "");
                string apiKey = ValidationHelper.GetString(ConfigurationManager.AppSettings["AzureSearchAPIKey"], "");
                _indexName = ValidationHelper.GetString(ConfigurationManager.AppSettings["AzureSearchIndexName"], "");

                // Create an HTTP reference to the catalog index
                _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
                _indexClient = _searchClient.Indexes.GetClient(_indexName);
            }
            catch (Exception e)
            {
                errorMessage = e.Message.ToString();
            }
        }
        // This Sample shows how to delete, create, upload documents and query an index
        static void Main(string[] args)
        {
            string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"];
            string apiKey = ConfigurationManager.AppSettings["SearchServiceApiKey"];

            // Create an HTTP reference to the catalog index
            _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
            _indexClient = _searchClient.Indexes.GetClient(GeoNamesIndex);

            Console.WriteLine("{0}", "Deleting index, data source, and indexer...\n");
            if (DeleteIndexingResources())
            {
                Console.WriteLine("{0}", "Creating index...\n");
                CreateIndex();
                Console.WriteLine("{0}", "Sync documents from Azure SQL...\n");
                SyncDataFromAzureSQL();
            }
            Console.WriteLine("{0}", "Complete.  Press any key to end application...\n");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            string searchServiceName = "habitat-test";

            string apiKey = "A00BE6F7A72E84BB7BB129057D3E692C";

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

            SearchIndexClient indexClient = serviceClient.Indexes.GetClient("test");

            Item item = Database.GetDatabase("web").GetRootItem();

            indexActions = new List<IndexAction<ItemSearchModel>>();

            AddItemToIndex(item);

            IndexBatch<ItemSearchModel> itemBatch = IndexBatch.Create<ItemSearchModel>(indexActions.AsEnumerable());

            indexClient.Documents.Index(itemBatch);

        }
Пример #22
0
        private static void CreateHotelsIndex(SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name = "hotels",
                Fields = new[] 
                { 
                    new Field("hotelId", DataType.String)                       { IsKey = true },
                    new Field("hotelName", DataType.String)                     { IsSearchable = true, IsFilterable = true },
                    new Field("baseRate", DataType.Double)                      { IsFilterable = true, IsSortable = 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, 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 }
                }
            };

            serviceClient.Indexes.Create(definition);
        }
Пример #23
0
        public static void CreateIndex(SearchServiceClient serviceClient, string indexName)
        {

            if (serviceClient.Indexes.Exists(indexName))
            {
                serviceClient.Indexes.Delete(indexName);
            }

            var definition = new Index()
            {
                Name = indexName,
                Fields = new[]
                {
                    new Field("fileId", DataType.String)                       { IsKey = true },
                    new Field("fileName", DataType.String)                     { IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false },
                    new Field("ocrText", DataType.String)                      { IsSearchable = true, IsFilterable = false, IsSortable = false, IsFacetable = false }
                }
            };

            serviceClient.Indexes.Create(definition);
        }
        public IHttpActionResult Analyze(EmailMessageForm form)
        {
            if (form != null && !string.IsNullOrEmpty(form.Subject))
            {
                string searchServiceName = ConfigurationManager.AppSettings["Azure.Search.ServiceName"];
                string apiKey = ConfigurationManager.AppSettings["Azure.Search.Key"];

                SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
                SearchIndexClient indexClient = serviceClient.Indexes.GetClient(ConfigurationManager.AppSettings["Azure.Search.Index"]);

                int maxResults = 25;
                if (Int32.TryParse(ConfigurationManager.AppSettings["Azure.Search.MaxResults"], out maxResults))
                {
                    if (maxResults < 0)
                    {
                        maxResults = 25;
                    }
                }

                var sp = new SearchParameters();
                sp.Top = maxResults;

                DocumentSearchResult<SearchResultDto> response = indexClient.Documents.Search<SearchResultDto>(form.Subject, sp);

                List<AnswerDto> answers = new List<AnswerDto>();

                foreach (var result in response.Results)
                {
                    answers.Add(new AnswerDto { Title = result.Document.Title, URL = result.Document.URL });
                }

                return Ok(answers);
            }

            return NotFound();
        }
        public void Process()
        {
            string searchServiceName = "habitat-test";

            string apiKey = "A00BE6F7A72E84BB7BB129057D3E692C";

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

            CreateIndex(serviceClient);

            SearchIndexClient indexClient = serviceClient.Indexes.GetClient("test");

            //Item item = Database.GetDatabase("web").GetRootItem();

            Item item = Sitecore.Configuration.Factory.GetDatabase("master").GetItem("/sitecore/content/Home");

            IndexActions = new List<IndexAction<ItemSearchModel>>();

            AddItemToIndex(item);

            IndexBatch<ItemSearchModel> itemBatch = IndexBatch.Create<ItemSearchModel>(IndexActions.AsEnumerable());

            indexClient.Documents.Index(itemBatch);
        }
Пример #26
0
 static SearchService()
 {
     client = new SearchServiceClient(AzureEndpoints.SearchAccount, new SearchCredentials(AzureEndpoints.SearchKey));
     indexClient = client.Indexes.GetClient("entries");
 }
Пример #27
0
 public BlogPostSearchIndex(string searchServiceName, string apiKey)
 {
     serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
     Task.Run(() => CreateBlogPostsIndex()).Wait();
 }
Пример #28
0
        public static void InitializeSearchService()
        {
            var searchServiceClient = new SearchServiceClient(Config.SearchServiceName, new SearchCredentials(Config.SearchServiceKey));

            SearchIndexClient = searchServiceClient.Indexes.GetClient("concerts");
        }
Пример #29
0
        static void Main(string[] args)
        {
            //Download Mahout distribution and store in Azure Blob
            DownloadAndUnzipMahoutDistribution();
            UploadMahoutDistributiontoBlobStorage();

            UploadHiveQueryToBlobStorage();

            //Setup the Azure SQL Database
            CreateAzureSQLTables();
            BCPDataIntoAzureSQLTable();

            //Setup the Azure Search Index
            // Create an HTTP reference to the catalog index
            _searchClient = new SearchServiceClient(searchServiceName, new SearchCredentials(apiKey));
            _indexClient = _searchClient.Indexes.GetClient(IndexName);

            Console.WriteLine("{0}", "Deleting index...\n");
            DeleteIndex();
            Console.WriteLine("{0}", "Creating index...\n");
            CreateIndex();
            Console.WriteLine("{0}", "Uploading documents...\n");
            UploadDocuments("data1.json");
            UploadDocuments("data2.json");
            Console.WriteLine("{0}", "Creating the Azure Search Indexer...\n");
            CreateIndexer();
            Console.WriteLine("{0}", "Complete.  Press any key to end application...\n");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // Ingest data from Mahout outout and merge into Azure Search
            string line;
            int currentID = -1;
            int docCounter = 0;
            List<Recommendation> recList = new List<Recommendation>();
            List<SearchIndexSchema> sisList = new List<SearchIndexSchema>();
            List<string> recs = new List<string>();

            // Configure cloud storage and search connections
            StorageCredentials credentials = new StorageCredentials(StorageAccountName, StorageApiKey);
            CloudStorageAccount storageAccount = new CloudStorageAccount(credentials, true);
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(StorageContainer);
            CloudBlockBlob blob = container.GetBlockBlobReference("output\\part-r-00000");  // In large implementations there may be multiple part files
            serviceClient = new SearchServiceClient(SearchServiceName, new SearchCredentials(SearchApiKey));
            indexClient = serviceClient.Indexes.GetClient(IndexName);

            // Open and parse mahout output file
            using (var stream = blob.OpenRead())
            {
                using (StreamReader file = new StreamReader(stream))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        Recommendation rec = new Recommendation();
                        char[] delimiters = new char[] { '\t' };
                        string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
                        rec.itemID = Convert.ToInt32(parts[0]);
                        rec.recItemID = Convert.ToInt32(parts[1]);
                        rec.percentSimilar = Convert.ToDouble(parts[2]);
                        if (currentID != rec.itemID)
                        {
                            docCounter++;
                            if (recList.Count > 0)
                            {
                                recList = recList.OrderByDescending(w => w.percentSimilar).Take(5).ToList();    // Take the 5 most similar items
                                foreach (var item in recList)
                                    recs.Add(item.recItemID.ToString());
                                recList.Clear();
                                sisList.Add(new SearchIndexSchema { id = currentID.ToString(), recommendations = recs.ToArray() });
                                recs.Clear();
                                if (sisList.Count == 500)
                                {
                                    MergeDocument(sisList);
                                    sisList.Clear();
                                    Console.WriteLine("{0} Docs written to Index...", docCounter - 1);
                                }
                            }
                            currentID = rec.itemID;
                        }
                        recList.Add(rec);
                    }
                    file.Close();
                    if (sisList.Count > 0)
                    {
                        MergeDocument(sisList);
                        sisList.Clear();
                        Console.WriteLine("{0} Docs written to Index...", docCounter - 1);
                    }
                }
            }
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
Пример #31
0
        static void Main(string[] args)
        {
            string searchServiceName = args[0];
            var credentials = new SearchCredentials(args[1]);
            var searchClient = new SearchServiceClient(searchServiceName, credentials);
            try
            {
                IndexDefinitionResponse getResponse = searchClient.Indexes.Get(IndexName);
                if (getResponse?.Index != null)
                {
                    Console.WriteLine("Deleting and recreating index " + IndexName);
                    searchClient.Indexes.Delete(IndexName);
                }
            }
            catch (CloudException)
            {
                // We expect this if the index does not yet exist.
            }

            IndexDefinitionResponse createIndexResponse = searchClient.Indexes.Create(new Index(
                IndexName,
                new[]
                {
                    new Field("ItemId", DataType.String) { IsKey = true },
                    new Field("Title", DataType.String) { IsSearchable = true },
                    new Field("Content", DataType.String) { IsSearchable = true },
                    new Field("CommentThreadId", DataType.Int32),
                    new Field("TimelineEntryId", DataType.Int32),
                    new Field("MediaAlbumId", DataType.Int32),
                    new Field("UserMediaId", DataType.Int32)
                }));

            Index index = createIndexResponse.Index;
            var indexClient = new SearchIndexClient(searchServiceName, IndexName, credentials);
            using (var dbContext = new ApplicationDbContext(args[2]))
            {
                IEnumerable<TimelineEntry> timelineEntries = dbContext.TimelineEntries
                    .Include(te => te.Message)
                    .Include(te => te.CommentThread.Comments.Select(c => c.Text));

                foreach (TimelineEntry entry in timelineEntries)
                {
                    var batchActions = new List<IndexAction<MessageIndexEntry>>();

                    batchActions.Add(new IndexAction<MessageIndexEntry>(
                        IndexActionType.Upload,
                        new MessageIndexEntry
                        {
                            ItemId = "timeline-" + entry.TimelineEntryId,
                            Content = entry.Message.Content,
                            TimelineEntryId = entry.TimelineEntryId
                        }));

                    if (entry.CommentThread != null)
                    {
                        foreach (Comment comment in entry.CommentThread.Comments)
                        {
                            batchActions.Add(new IndexAction<MessageIndexEntry>(
                                IndexActionType.Upload,
                                new MessageIndexEntry
                                {
                                    ItemId = "comment-" + comment.CommentId,
                                    Content = comment.Text.Content,
                                    TimelineEntryId = entry.TimelineEntryId,
                                    CommentThreadId = comment.CommentThreadId
                                }));
                        }
                    }
                    var batch = new IndexBatch<MessageIndexEntry>(batchActions);
                    DocumentIndexResponse indexDocumentsResponse = indexClient.Documents.Index(batch);
                }

                IEnumerable<MediaAlbum> albums = dbContext.MediaAlbums
                    .Include(a => a.CommentThread.Comments.Select(c => c.Text));

                foreach (MediaAlbum album in albums)
                {
                    var batchActions = new List<IndexAction<MessageIndexEntry>>();

                    batchActions.Add(new IndexAction<MessageIndexEntry>(
                        IndexActionType.Upload,
                        new MessageIndexEntry
                        {
                            ItemId = "album-" + album.MediaAlbumId,
                            Title = album.Title,
                            Content = album.Description,
                            MediaAlbumId = album.MediaAlbumId
                        }));

                    if (album.CommentThread != null)
                    {
                        foreach (Comment comment in album.CommentThread.Comments)
                        {
                            batchActions.Add(new IndexAction<MessageIndexEntry>(
                                IndexActionType.Upload,
                                new MessageIndexEntry
                                {
                                    ItemId = "comment-" + comment.CommentId,
                                    Content = comment.Text.Content,
                                    MediaAlbumId = album.MediaAlbumId,
                                    CommentThreadId = comment.CommentThreadId
                                }));
                        }
                    }
                    var batch = new IndexBatch<MessageIndexEntry>(batchActions);
                    DocumentIndexResponse indexDocumentsResponse = indexClient.Documents.Index(batch);
                }


                IEnumerable<UserMedia> medias = dbContext.UserMedias
                    .Include(m => m.Description)
                    .Include(m => m.CommentThread.Comments.Select(c => c.Text));

                foreach (UserMedia media in medias)
                {
                    var batchActions = new List<IndexAction<MessageIndexEntry>>();

                    batchActions.Add(new IndexAction<MessageIndexEntry>(
                        IndexActionType.Upload,
                        new MessageIndexEntry
                        {
                            ItemId = "media-" + media.UserMediaId,
                            Title = media.Title,
                            Content = media.Description?.Content,
                            UserMediaId = media.UserMediaId,
                            MediaAlbumId = media.MediaAlbumId
                        }));

                    if (media.CommentThread != null)
                    {
                        foreach (Comment comment in media.CommentThread.Comments)
                        {
                            batchActions.Add(new IndexAction<MessageIndexEntry>(
                                IndexActionType.Upload,
                                new MessageIndexEntry
                                {
                                    ItemId = "comment-" + comment.CommentId,
                                    Content = comment.Text.Content,
                                    UserMediaId = media.UserMediaId,
                                    MediaAlbumId = media.MediaAlbumId,
                                    CommentThreadId = comment.CommentThreadId
                                }));
                        }
                    }
                    var batch = new IndexBatch<MessageIndexEntry>(batchActions);
                    DocumentIndexResponse indexDocumentsResponse = indexClient.Documents.Index(batch);
                }
            }
        }