public void GetDocumentThrowsWhenRequestIsMalformed()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var indexedDoc =
                    new Hotel()
                {
                    HotelId     = "3",
                    BaseRate    = 279.99,
                    Description = "Surprisingly expensive"
                };

                var batch = IndexBatch.Create(IndexAction.Create(indexedDoc));
                client.Documents.Index(batch);

                string[] selectedFields = new[] { "hotelId", "ThisFieldDoesNotExist" };

                CloudException e = Assert.Throws <CloudException>(() => client.Documents.Get("3", selectedFields));

                Assert.Equal(HttpStatusCode.BadRequest, e.Response.StatusCode);
                Assert.Contains(
                    "Invalid expression: Could not find a property named 'ThisFieldDoesNotExist' on type 'search.document'.",
                    e.Message);
            });
        }
Exemplo n.º 2
0
        public void StaticallyTypedDateTimesRoundTripAsUtc()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index =
                    new Index()
                {
                    Name   = TestUtilities.GenerateName(),
                    Fields = new[]
                    {
                        new Field("ISBN", DataType.String)
                        {
                            IsKey = true
                        },
                        new Field("PublishDate", DataType.DateTimeOffset)
                    }
                };

                IndexDefinitionResponse createIndexResponse = serviceClient.Indexes.Create(index);
                Assert.Equal(HttpStatusCode.Created, createIndexResponse.StatusCode);

                SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name);

                DateTime localDateTime       = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);
                DateTime utcDateTime         = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                DateTime unspecifiedDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);

                var batch =
                    IndexBatch.Create(
                        new[]
                {
                    IndexAction.Create(new Book()
                    {
                        ISBN = "1", PublishDate = localDateTime
                    }),
                    IndexAction.Create(new Book()
                    {
                        ISBN = "2", PublishDate = utcDateTime
                    }),
                    IndexAction.Create(new Book()
                    {
                        ISBN = "3", PublishDate = unspecifiedDateTime
                    })
                });

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

                DocumentGetResponse <Book> getResponse = indexClient.Documents.Get <Book>("1");
                Assert.Equal(localDateTime.ToUniversalTime(), getResponse.Document.PublishDate);

                getResponse = indexClient.Documents.Get <Book>("2");
                Assert.Equal(utcDateTime, getResponse.Document.PublishDate);

                getResponse = indexClient.Documents.Get <Book>("3");
                Assert.Equal(utcDateTime, getResponse.Document.PublishDate);
            });
        }
        public void CanGetStaticallyTypedDocumentWithNullOrEmptyValues()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var expectedDoc =
                    new Hotel()
                {
                    HotelId            = "1",
                    BaseRate           = null,
                    HotelName          = null,
                    Tags               = new string[0],
                    ParkingIncluded    = null,
                    LastRenovationDate = null,
                    Rating             = null,
                    Location           = null
                };

                var batch = IndexBatch.Create(IndexAction.Create(expectedDoc));
                client.Documents.Index(batch);
                SearchTestUtilities.WaitForIndexing();

                DocumentGetResponse <Hotel> getResponse = client.Documents.Get <Hotel>("1");
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.Equal(expectedDoc, getResponse.Document);
            });
        }
Exemplo n.º 4
0
        private IEnumerable <string> IndexThousandsOfDocuments(SearchIndexClient client)
        {
            int existingDocumentCount = Data.TestDocuments.Length;

            IEnumerable <string> hotelIds =
                Enumerable.Range(existingDocumentCount + 1, 2001 - existingDocumentCount).Select(id => id.ToString());

            IEnumerable <Hotel> hotels = hotelIds.Select(id => new Hotel()
            {
                HotelId = id
            });
            IEnumerable <IndexAction <Hotel> > actions = hotels.Select(h => IndexAction.Create(h));

            var batch = IndexBatch.Create(actions.Take(1000));

            client.Documents.Index(batch);

            SearchTestUtilities.WaitForIndexing();

            batch = IndexBatch.Create(actions.Skip(1000));
            client.Documents.Index(batch);

            SearchTestUtilities.WaitForIndexing();
            return(hotelIds);
        }
        public void CanGetStaticallyTypedDocument()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var expectedDoc =
                    new Hotel()
                {
                    HotelId            = "1",
                    BaseRate           = 199.0,
                    Description        = "Best hotel in town",
                    DescriptionFr      = "Meilleur hôtel en ville",
                    HotelName          = "Fancy Stay",
                    Category           = "Luxury",
                    Tags               = new[] { "pool", "view", "wifi", "concierge" },
                    ParkingIncluded    = false,
                    SmokingAllowed     = false,
                    LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.Zero),
                    Rating             = 5,
                    Location           = GeographyPoint.Create(47.678581, -122.131577)
                };

                var batch = IndexBatch.Create(IndexAction.Create(expectedDoc));
                client.Documents.Index(batch);

                DocumentGetResponse <Hotel> response = client.Documents.Get <Hotel>("1");
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(expectedDoc, response.Document);
            });
        }
        public void RoundTrippingDateTimeOffsetNormalizesToUtc()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var indexedDoc =
                    new Hotel()
                {
                    HotelId            = "1",
                    LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8))
                };

                var expectedDoc =
                    new Hotel()
                {
                    HotelId            = "1",
                    Tags               = new string[0], // null arrays become empty arrays during indexing.
                    LastRenovationDate = new DateTimeOffset(2010, 6, 27, 8, 0, 0, TimeSpan.Zero)
                };

                var batch = IndexBatch.Create(IndexAction.Create(indexedDoc));
                client.Documents.Index(batch);

                DocumentGetResponse <Hotel> response = client.Documents.Get <Hotel>("1");
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(expectedDoc, response.Document);
            });
        }
Exemplo n.º 7
0
        private IEnumerable <string> IndexDocuments(SearchIndexClient client, int totalDocCount)
        {
            int existingDocumentCount = Data.TestDocuments.Length;

            IEnumerable <string> hotelIds =
                Enumerable.Range(existingDocumentCount + 1, totalDocCount - existingDocumentCount)
                .Select(id => id.ToString());

            IEnumerable <Hotel> hotels = hotelIds.Select(id => new Hotel()
            {
                HotelId = id
            });
            List <IndexAction <Hotel> > actions = hotels.Select(h => IndexAction.Create(h)).ToList();

            for (int i = 0; i < actions.Count; i += 1000)
            {
                IEnumerable <IndexAction <Hotel> > nextActions = actions.Skip(i).Take(1000);

                if (!nextActions.Any())
                {
                    break;
                }

                var batch = IndexBatch.Create(nextActions);
                client.Documents.Index(batch);

                SearchTestUtilities.WaitForIndexing();
            }

            return(hotelIds);
        }
        public DocumentsFixture()
        {
            SearchIndexClient indexClient = this.GetSearchIndexClient();

            var batch = IndexBatch.Create(TestDocuments.Select(d => IndexAction.Create(d)));

            indexClient.Documents.Index(batch);

            SearchTestUtilities.WaitForIndexing();
        }
Exemplo n.º 9
0
 private static void MergeDocument(List <SearchIndexSchema> recList)
 {
     // Merge recommendations into Azure Search with existing documents
     try
     {
         indexClient.Documents.Index(IndexBatch.Create((recList.Select(doc => IndexAction.Create(IndexActionType.MergeOrUpload, doc)))));
     }
     catch (IndexBatchException e)
     {
         Console.WriteLine("Failed to index some of the documents: {0}",
                           String.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key)));
     }
 }
Exemplo n.º 10
0
        protected void TestCanSuggestWithDateTimeInStaticModel()
        {
            SearchServiceClient serviceClient = Data.GetSearchServiceClient();

            Index index =
                new Index()
            {
                Name   = TestUtilities.GenerateName(),
                Fields = new[]
                {
                    new Field("ISBN", DataType.String)
                    {
                        IsKey = true
                    },
                    new Field("Title", DataType.String)
                    {
                        IsSearchable = true
                    },
                    new Field("Author", DataType.String),
                    new Field("PublishDate", DataType.DateTimeOffset)
                },
                Suggesters = new[] { new Suggester("sg", SuggesterSearchMode.AnalyzingInfixMatching, "Title") }
            };

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

            var doc1 = new Book()
            {
                ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien"
            };
            var doc2 = new Book()
            {
                ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18)
            };
            var batch = IndexBatch.Create(IndexAction.Create(doc1), IndexAction.Create(doc2));

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

            var parameters = new SuggestParameters()
            {
                Select = new[] { "ISBN", "Title", "PublishDate" }
            };
            DocumentSuggestResponse <Book> response = indexClient.Documents.Suggest <Book>("War", "sg", parameters);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            Assert.Equal(1, response.Results.Count);
            Assert.Equal(doc2, response.Results[0].Document);
        }
Exemplo n.º 11
0
        private IndexBatch <ECADocument> DoDeleteDocuments(List <DocumentKey> documentKeys)
        {
            var actions   = new List <IndexAction>();
            var documents = new List <ECADocument>();

            documentKeys.Distinct().ToList().ForEach(x =>
            {
                documents.Add(new ECADocument
                {
                    Id = x.ToString()
                });
            });

            return(IndexBatch.Create(documents.Select(d => IndexAction.Create(IndexActionType.Delete, d))));
        }
Exemplo n.º 12
0
        public async Task RemoveBuildsFromIndexAsync(IEnumerable <Build> b)
        {
            var batch = IndexBatch.Create(b.Select(doc => IndexAction.Create(IndexActionType.Delete, SearchFriendlyBuild.FromBuild(doc))));

            try
            {
                await this._indexClient.Documents.IndexAsync(batch);
            }
            catch (IndexBatchException e)
            {
                _log.LogError("Failed to de-index some of the documents: {0}, due to: {1}",
                              String.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key)),
                              e.Message);
            }
        }
Exemplo n.º 13
0
        public void IndexDoesNotThrowWhenAllActionsSucceed()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var batch = IndexBatch.Create(new[] { IndexAction.Create(new Hotel()
                    {
                        HotelId = "1"
                    }) });

                DocumentIndexResponse indexResponse = client.Documents.Index(batch);
                Assert.Equal(HttpStatusCode.OK, indexResponse.StatusCode);

                Assert.Equal(1, indexResponse.Results.Count);
                AssertIndexActionSucceeded("1", indexResponse.Results[0]);
            });
        }
        /// <summary>
        /// Inserts/Updates a customer
        /// </summary>
        public void UpsertCustomer(Interface.GlobalEnum.IndexerIndexName indexName, Model.Search.SearchCustomerModel searchCustomerModel)
        {
            // only check once per run
            if (!doesIndexExistsCheck.Contains(indexName.ToString().ToLower()))
            {
                CreateIndexIfNotExists(indexName, Interface.GlobalEnum.IndexerRepositoryIndexType.SystemDefined);
                doesIndexExistsCheck.Add(indexName.ToString().ToLower());
            }

            SearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName.ToString().ToLower());

            // Can be done in batches, but since we are using batching we can do one by one for retries
            List <Model.Search.SearchCustomerModel> itemsToIndex = new List <Model.Search.SearchCustomerModel>();

            itemsToIndex.Add(searchCustomerModel);

            indexClient.Documents.Index(IndexBatch.Create(itemsToIndex.Select(doc => IndexAction.Create(IndexActionType.MergeOrUpload, doc))));
        } // UpsertCustomer
Exemplo n.º 15
0
        public void CanIndexWithPascalCaseFields()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index =
                    new Index()
                {
                    Name   = TestUtilities.GenerateName(),
                    Fields = new[]
                    {
                        new Field("ISBN", DataType.String)
                        {
                            IsKey = true
                        },
                        new Field("Title", DataType.String),
                        new Field("Author", DataType.String)
                    }
                };

                IndexDefinitionResponse createIndexResponse = serviceClient.Indexes.Create(index);
                Assert.Equal(HttpStatusCode.Created, createIndexResponse.StatusCode);

                SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name);

                var batch =
                    IndexBatch.Create(
                        new[]
                {
                    IndexAction.Create(
                        new Book()
                    {
                        ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien"
                    })
                });

                DocumentIndexResponse indexResponse = indexClient.Documents.Index(batch);
                Assert.Equal(HttpStatusCode.OK, indexResponse.StatusCode);

                Assert.Equal(1, indexResponse.Results.Count);
                AssertIndexActionSucceeded("123", indexResponse.Results[0]);
            });
        }
Exemplo n.º 16
0
        private IndexBatch <ECADocument> DoAddOrupdate <T>(List <T> documents, IDocumentConfiguration configuration) where T : class
        {
            if (configuration == null)
            {
                throw new NotSupportedException(String.Format("The configuration for the type [{0}] was not found.", typeof(T)));
            }
            Contract.Assert(configuration.IsConfigurationForType(typeof(T)),
                            String.Format("The IDocumentConfiguration {0} is not valid for the type {1}.", configuration.GetType(), typeof(T)));
            var actions      = new List <IndexAction>();
            var ecaDocuments = new List <ECADocument>();

            documents.ForEach(x =>
            {
                var ecaDocument = new ECADocument <T>(configuration, x);
                Contract.Assert(!String.IsNullOrWhiteSpace(ecaDocument.Id), String.Format("The document for object of type {0} must have a valid id.", typeof(T)));
                Contract.Assert(!String.IsNullOrWhiteSpace(ecaDocument.DocumentTypeId), String.Format("The document for object of type {0} must have a valid document type id.", typeof(T)));
                Contract.Assert(!String.IsNullOrWhiteSpace(ecaDocument.DocumentTypeName), String.Format("The document for object of type {0} must have a valid document type name.", typeof(T)));
                ecaDocuments.Add(ecaDocument);
            });
            return(IndexBatch.Create(ecaDocuments.Select(d => IndexAction.Create(IndexActionType.MergeOrUpload, d))));
        }
Exemplo n.º 17
0
 public static async void ProcessTweet([QueueTrigger(Strings.MarchMadnessQueueName)] MarchMadnessTweet tweet)
 {
     try
     {
         var indexClient = searchClient.Indexes.GetClient(searchIndex);
         await indexClient.Documents.IndexAsync(IndexBatch.Create(IndexAction.Create(IndexActionType.MergeOrUpload, tweet)));
     }
     catch (IndexBatchException ex)
     {
         // Sometimes when your Search service is under load, indexing will fail for some of the documents in
         // the batch. Depending on your application, you can take compensating actions like delaying and
         // retrying. For this simple demo, we just log the failed document keys and continue.
         Console.Error.WriteLine("Index batch exception: " + ex);
         throw;
     }
     catch (Exception ex)
     {
         Console.Error.WriteLine("Error processing tweet: " + tweet + ". Exception: " + ex);
         throw;
     }
 }
Exemplo n.º 18
0
        public void GetStaticallyTypedDocumentSetsUnselectedFieldsToNull()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var indexedDoc =
                    new Hotel()
                {
                    HotelId            = "2",
                    BaseRate           = 79.99,
                    Description        = "Cheapest hotel in town",
                    DescriptionFr      = "Hôtel le moins cher en ville",
                    HotelName          = "Roach Motel",
                    Category           = "Budget",
                    Tags               = new[] { "motel", "budget" },
                    ParkingIncluded    = true,
                    SmokingAllowed     = true,
                    LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero),
                    Rating             = 1,
                    Location           = GeographyPoint.Create(49.678581, -122.131577)
                };

                var expectedDoc =
                    new Hotel()
                {
                    Description = "Cheapest hotel in town",
                    HotelName   = "Roach Motel"
                };

                var batch = IndexBatch.Create(IndexAction.Create(indexedDoc));
                client.Documents.Index(batch);

                DocumentGetResponse <Hotel> response =
                    client.Documents.Get <Hotel>("2", new[] { "description", "hotelName" });
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(expectedDoc, response.Document);
            });
        }
        } // UpsertCustomer

        /// <summary>
        /// Removes a customer
        /// </summary>
        /// <param name="indexName"></param>
        /// <param name="searchDocument"></param>
        public void DeleteCustomer(Interface.GlobalEnum.IndexerIndexName indexName, Model.Search.SearchCustomerModel searchCustomerModel)
        {
            // only check once per run
            if (!doesIndexExistsCheck.Contains(indexName.ToString().ToLower()))
            {
                CreateIndexIfNotExists(indexName, Interface.GlobalEnum.IndexerRepositoryIndexType.SystemDefined);
                doesIndexExistsCheck.Add(indexName.ToString().ToLower());
            }

            SearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName.ToString().ToLower());

            // Can be done in batches, but since we are using batching we can do one by one for retries
            List <Model.Search.SearchCustomerModel> itemsToIndex = new List <Model.Search.SearchCustomerModel>();

            itemsToIndex.Add(searchCustomerModel);

            indexClient.Documents.Index(IndexBatch.Create(itemsToIndex.Select(doc => IndexAction.Create(IndexActionType.Delete, doc))));

            // 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.
        } // DeleteCustomer
Exemplo n.º 20
0
        public void CanGetStaticallyTypedDocumentWithPascalCaseFields()
        {
            Run(() =>
            {
                SearchServiceClient serviceClient = Data.GetSearchServiceClient();

                Index index =
                    new Index()
                {
                    Name   = TestUtilities.GenerateName(),
                    Fields = new[]
                    {
                        new Field("ISBN", DataType.String)
                        {
                            IsKey = true
                        },
                        new Field("Title", DataType.String),
                        new Field("Author", DataType.String)
                    }
                };

                IndexDefinitionResponse createIndexResponse = serviceClient.Indexes.Create(index);
                Assert.Equal(HttpStatusCode.Created, createIndexResponse.StatusCode);

                SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name);

                var expectedDoc = new Book()
                {
                    ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien"
                };
                var batch = IndexBatch.Create(IndexAction.Create(expectedDoc));
                indexClient.Documents.Index(batch);

                DocumentGetResponse <Book> response = indexClient.Documents.Get <Book>("123");
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                Assert.Equal(expectedDoc, response.Document);
            });
        }
Exemplo n.º 21
0
        public static void UploadDocuments(SearchIndexClient indexClient, string fileId, string fileName, string ocrText)
        {
            var documents =
                new OCRTextIndex[]
            {
                new OCRTextIndex()
                {
                    fileId   = fileId,
                    fileName = fileName,
                    ocrText  = ocrText
                }
            };

            try
            {
                indexClient.Documents.Index(IndexBatch.Create(documents.Select(doc => IndexAction.Create(doc))));
            }
            catch (IndexBatchException e)
            {
                // Sometimes when your Search service is under load, indexing will fail for some of the documents in
                // the batch. Depending on your application, you can take compensating actions like delaying and
                // retrying. For this simple demo, we just log the failed document keys and continue.
                Console.WriteLine(
                    "Failed to index some of the documents: {0}",
                    String.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key)));
            }
        }
Exemplo n.º 22
0
        private static void UploadDocuments(SearchIndexClient indexClient)
        {
            var documents =
                new Hotel[]
            {
                new Hotel()
                {
                    HotelId            = "1058-441",
                    HotelName          = "Fancy Stay",
                    BaseRate           = 199.0,
                    Category           = "Luxury",
                    Tags               = new[] { "pool", "view", "concierge" },
                    ParkingIncluded    = false,
                    LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.Zero),
                    Rating             = 5,
                    Location           = GeographyPoint.Create(47.678581, -122.131577)
                },
                new Hotel()
                {
                    HotelId            = "666-437",
                    HotelName          = "Roach Motel",
                    BaseRate           = 79.99,
                    Category           = "Budget",
                    Tags               = new[] { "motel", "budget" },
                    ParkingIncluded    = true,
                    LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero),
                    Rating             = 1,
                    Location           = GeographyPoint.Create(49.678581, -122.131577)
                },
                new Hotel()
                {
                    HotelId            = "970-501",
                    HotelName          = "Econo-Stay",
                    BaseRate           = 129.99,
                    Category           = "Budget",
                    Tags               = new[] { "pool", "budget" },
                    ParkingIncluded    = true,
                    LastRenovationDate = new DateTimeOffset(1995, 7, 1, 0, 0, 0, TimeSpan.Zero),
                    Rating             = 4,
                    Location           = GeographyPoint.Create(46.678581, -122.131577)
                },
                new Hotel()
                {
                    HotelId            = "956-532",
                    HotelName          = "Express Rooms",
                    BaseRate           = 129.99,
                    Category           = "Budget",
                    Tags               = new[] { "wifi", "budget" },
                    ParkingIncluded    = true,
                    LastRenovationDate = new DateTimeOffset(1995, 7, 1, 0, 0, 0, TimeSpan.Zero),
                    Rating             = 4,
                    Location           = GeographyPoint.Create(48.678581, -122.131577)
                },
                new Hotel()
                {
                    HotelId         = "566-518",
                    HotelName       = "Surprisingly Expensive Suites",
                    BaseRate        = 279.99,
                    Category        = "Luxury",
                    ParkingIncluded = false
                }
            };

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

            // Wait a while for indexing to complete.
            Thread.Sleep(2000);
        }
Exemplo n.º 23
0
        public string LoadIndex()
        {
            try
            {
                //Get the quotes
                var quotes = QuoteMVCProvider.GetQuoteMVCs();

                //Build up a json post of the quote data
                List <Quote> lstQuotes = new List <Quote>();

                foreach (QuoteMVC quote in quotes)
                {
                    Quote qt = new Quote();
                    qt.DocumentID    = quote.DocumentID.ToString();
                    qt.NodeAliasPath = quote.NodeAliasPath;
                    qt.QuoteAuthor   = quote.GetValue("QuoteAuthor").ToString();
                    qt.QuoteText     = quote.GetValue("QuoteText").ToString().Replace("'", "''").Replace("\"", "''");
                    qt.QuoteDate     = quote.GetValue("QuoteDate").ToString();

                    lstQuotes.Add(qt);
                }

                _indexClient.Documents.Index(IndexBatch.Create(lstQuotes.Select(qt => IndexAction.Create(qt))));


                return("Index loaded!");
            }
            catch (Exception ex)
            {
                return("There was an issue loading the index: {0}\r\n" + ex.Message.ToString());
            }
        }
Exemplo n.º 24
0
        private static void UploadDocuments(SearchIndexClient indexClient)
        {
            Console.WriteLine("{0}", "Uploading movies documents...\n");
            try
            {
                string query = @"SELECT top 9500 ID,Title,Year,Rating,NumVotes,imdbID,Type,Released,Runtime,Genres,Directors,Metascore,imdbRating,imdbVotes,tomatoMeter,tomatoRating
                                    ,tomatoRotten,tomatoUserMeter,tomatoUserRating,poster_path,release_date FROM Movie";

                using (SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["SourceSqlConnectionString"]))
                {
                    using (SqlCommand cmd = new SqlCommand(query, con))
                    {
                        cmd.Connection.Open();
                        using (SqlDataReader row = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                        {
                            int          count  = 0;
                            List <Movie> movies = new List <Movie>();
                            while (row.Read())
                            {
                                Movie movie = new Movie()
                                {
                                    ID               = row["ID"] is DBNull ? "" : row["ID"].ToString(),
                                    Title            = row["Title"] is DBNull ? "" : (string)row["Title"],
                                    Year             = row["Year"] is DBNull ? 0 : Convert.ToInt32(row["Year"]),
                                    Rating           = row["Rating"] is DBNull ? 0 : (double)row["Rating"],
                                    NumVotes         = row["NumVotes"] is DBNull ? 0 : Convert.ToInt32(row["NumVotes"]),
                                    imdbID           = row["imdbID"] is DBNull ? "" : (string)row["imdbID"],
                                    Type             = row["Type"] is DBNull ? "" : (string)row["Type"],
                                    Released         = row["Released"] is DBNull ? DateTime.Now : (DateTime)row["Released"],
                                    Runtime          = row["Runtime"] is DBNull ? 0 : Convert.ToInt32(row["Runtime"]),
                                    Genres           = row["Genres"] is DBNull ? "" : (string)row["Genres"],
                                    Directors        = row["Directors"] is DBNull ? "" : (string)row["Directors"],
                                    Metascore        = row["Metascore"] is DBNull ? 0 : (double)row["Metascore"],
                                    imdbRating       = row["imdbRating"] is DBNull ? 0 : (double)row["imdbRating"],
                                    imdbVotes        = row["imdbVotes"] is DBNull ? "" : (string)row["imdbVotes"],
                                    tomatoMeter      = row["tomatoMeter"] is DBNull ? 0 : (double)row["tomatoMeter"],
                                    tomatoRating     = row["tomatoRating"] is DBNull ? 0 : (double)row["tomatoRating"],
                                    tomatoRotten     = row["tomatoRotten"] is DBNull ? 0 : (double)row["tomatoRotten"],
                                    tomatoUserMeter  = row["tomatoUserMeter"] is DBNull ? 0 : (double)row["tomatoUserMeter"],
                                    tomatoUserRating = row["tomatoUserRating"] is DBNull ? 0 : (double)row["tomatoUserRating"],
                                    poster_path      = row["poster_path"] is DBNull ? "" : "http://image.tmdb.org/t/p/w90" + (string)row["poster_path"],
                                    release_date     = row["release_date"] is DBNull ? DateTime.Now : (DateTime)row["release_date"],
                                };

                                if (movies.Count > 990)
                                {
                                    indexClient.Documents.Index(IndexBatch.Create(movies.Select(doc => IndexAction.Create(doc))));
                                    movies.Clear();
                                }

                                movies.Add(movie);

                                count++;
                            }

                            if (movies.Count > 0)
                            {
                                indexClient.Documents.Index(IndexBatch.Create(movies.Select(doc => IndexAction.Create(doc))));
                                movies.Clear();
                            }
                        }
                    }
                }
            }
            catch (IndexBatchException e)
            {
                Console.WriteLine(
                    "Failed to index some of the documents: {0}",
                    String.Join(", ", e.IndexResponse.Results.Where(r => !r.Succeeded).Select(r => r.Key)));
            }

            Thread.Sleep(5000);
        }
Exemplo n.º 25
0
        public void CanIndexStaticallyTypedDocuments()
        {
            Run(() =>
            {
                SearchIndexClient client = Data.GetSearchIndexClient();

                var batch = IndexBatch.Create(new[]
                {
                    IndexAction.Create(
                        IndexActionType.Upload,
                        new Hotel()
                    {
                        HotelId            = "1",
                        BaseRate           = 199.0,
                        Description        = "Best hotel in town",
                        DescriptionFr      = "Meilleur hôtel en ville",
                        HotelName          = "Fancy Stay",
                        Category           = "Luxury",
                        Tags               = new[] { "pool", "view", "wifi", "concierge" },
                        ParkingIncluded    = false,
                        SmokingAllowed     = false,
                        LastRenovationDate = new DateTimeOffset(2010, 6, 27, 0, 0, 0, TimeSpan.FromHours(-8)),
                        Rating             = 5,
                        Location           = GeographyPoint.Create(47.678581, -122.131577)
                    }),
                    IndexAction.Create(
                        IndexActionType.Upload,
                        new Hotel()
                    {
                        HotelId            = "2",
                        BaseRate           = 79.99,
                        Description        = "Cheapest hotel in town",
                        DescriptionFr      = "Hôtel le moins cher en ville",
                        HotelName          = "Roach Motel",
                        Category           = "Budget",
                        Tags               = new[] { "motel", "budget" },
                        ParkingIncluded    = true,
                        SmokingAllowed     = true,
                        LastRenovationDate = new DateTimeOffset(1982, 4, 28, 0, 0, 0, TimeSpan.Zero),
                        Rating             = 1,
                        Location           = GeographyPoint.Create(49.678581, -122.131577)
                    }),
                    IndexAction.Create(
                        IndexActionType.Merge,
                        new Hotel()
                    {
                        HotelId            = "3",
                        BaseRate           = 279.99,
                        Description        = "Surprisingly expensive",
                        LastRenovationDate = null
                    }),
                    IndexAction.Create(IndexActionType.Delete, new Hotel()
                    {
                        HotelId = "4"
                    }),
                    IndexAction.Create(
                        IndexActionType.MergeOrUpload,
                        new Hotel()
                    {
                        HotelId   = "5",
                        BaseRate  = Double.NaN,
                        HotelName = null,
                        Tags      = new string[0]
                    })
                });

                IndexBatchException e = Assert.Throws <IndexBatchException>(() => client.Documents.Index(batch));
                AssertIsPartialFailure(e, batch, "3");

                Assert.Equal(5, e.IndexResponse.Results.Count);

                AssertIndexActionSucceeded("1", e.IndexResponse.Results[0]);
                AssertIndexActionSucceeded("2", e.IndexResponse.Results[1]);
                AssertIndexActionFailed("3", e.IndexResponse.Results[2], "Document not found.");
                AssertIndexActionSucceeded("4", e.IndexResponse.Results[3]);
                AssertIndexActionSucceeded("5", e.IndexResponse.Results[4]);

                SearchTestUtilities.WaitForIndexing();

                DocumentCountResponse countResponse = client.Documents.Count();
                Assert.Equal(HttpStatusCode.OK, countResponse.StatusCode);
                Assert.Equal(3, countResponse.DocumentCount);
            });
        }