示例#1
0
        static void Main(string[] args)
        {
            IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();
            // You only require a mapping if the default settings are not good enough
            //elasticsearchMappingResolver.AddElasticSearchMappingForEntityType(typeof(Skill), new SkillElasticsearchMapping());

            using (var elasticSearchContext = new ElasticsearchContext("http://localhost:9200/", elasticsearchMappingResolver))
            {
                elasticSearchContext.TraceProvider = new TraceProvider("tracingExample");
                elasticSearchContext.AddUpdateDocument(TestData.SkillEf, TestData.SkillEf.Id);
                elasticSearchContext.AddUpdateDocument(TestData.SkillOrm, TestData.SkillOrm.Id);
                elasticSearchContext.AddUpdateDocument(TestData.SkillSQLServer, TestData.SkillSQLServer.Id);
                elasticSearchContext.AddUpdateDocument(TestData.SkillGermanWithFunnyLetters, TestData.SkillGermanWithFunnyLetters.Id);
                elasticSearchContext.AddUpdateDocument(TestData.SkillLevel, TestData.SkillLevel.Id);

                var addEntitiesResult = elasticSearchContext.SaveChanges();

                Console.WriteLine(addEntitiesResult.PayloadResult);
                Console.WriteLine(addEntitiesResult.Status);
                Console.WriteLine(addEntitiesResult.Description);
            }

            using (var elasticSearchContext = new ElasticsearchContext("http://localhost:9200/", elasticsearchMappingResolver))
            {
                elasticSearchContext.TraceProvider = new TraceProvider("tracingExample");
                // get a entity and update it, then delete an entity
                Skill singleEntityWithId = elasticSearchContext.GetDocument<Skill>("11");
                singleEntityWithId.Updated = DateTime.UtcNow;
                elasticSearchContext.AddUpdateDocument(TestData.SkillOrm, TestData.SkillOrm.Id);
                elasticSearchContext.DeleteDocument<Skill>(TestData.SkillEf.Id);
                elasticSearchContext.SaveChanges();

                elasticSearchContext.AddUpdateDocument(TestData.SkillEf, TestData.SkillEf.Id);
                var nextResult = elasticSearchContext.SaveChanges();

                Console.WriteLine(nextResult.PayloadResult);
                Console.WriteLine(nextResult.Status);
                Console.WriteLine(nextResult.Description);
            }

            using (var elasticSearchContext = new ElasticsearchContext("http://localhost:9200/", elasticsearchMappingResolver))
            {
                elasticSearchContext.TraceProvider = new TraceProvider("tracingExample");
                // deleting indexes are usually not required...
                elasticSearchContext.AllowDeleteForIndex = true;
                var result = elasticSearchContext.DeleteIndexAsync<SkillLevel>();
                result.Wait();
                var result1 = elasticSearchContext.DeleteIndexAsync<Skill>();
                result1.Wait();
                //var result = elasticSearchContext.DeleteIndex<Skill>();
                elasticSearchContext.SaveChanges();
                //Console.WriteLine(result.Result.PayloadResult);
                //Console.WriteLine(result.Result.Status);
                //Console.WriteLine(result.Result.Description);
                //Console.ReadLine();
            }
        }
        public Person GetPersonFromElasticsearch(int id)
        {
            Person person;
            IElasticsearchMappingResolver ElasticsearchMappingResolver = new ElasticsearchMappingResolver();
            using (var ElasticsearchContext = new ElasticsearchContext("http://localhost:9200/", ElasticsearchMappingResolver))
            {
                person = ElasticsearchContext.GetDocument<Person>(id);
            }

            return person;
        }
        public Address GetAddressFromElasticsearch(int id)
        {
            Address address;
            IElasticsearchMappingResolver ElasticsearchMappingResolver = new ElasticsearchMappingResolver();
            using (var ElasticsearchContext = new ElasticsearchContext("http://localhost:9200/", ElasticsearchMappingResolver))
            {
                address = ElasticsearchContext.GetDocument<Address>(id);
            }

            return address;
        }
		public void SaveToElasticsearchStateProvinceIfitDoesNotExist()
		{
			IElasticsearchMappingResolver elasticsearchMappingResolver = new ElasticsearchMappingResolver();
			using (var elasticSearchContext = new ElasticsearchContext("http://localhost:9200/", new ElasticsearchSerializerConfiguration(elasticsearchMappingResolver, true, true)))
			{
				if (!elasticSearchContext.IndexTypeExists<StateProvince>())
				{
					elasticSearchContext.TraceProvider = new ConsoleTraceProvider();
					using (var databaseEfModel = new EfModel())
					{
						int pointer = 0;
						const int interval = 20;
						bool firstRun = true;
						int length = databaseEfModel.StateProvince.Count();

						while (pointer < length)
						{
							_stopwatch.Start();
							var collection =
								databaseEfModel.StateProvince.OrderBy(t => t.StateProvinceID)
									.Skip(pointer)
									.Take(interval)
									.ToList<StateProvince>();
							_stopwatch.Stop();
							Console.WriteLine("Time taken for select {0} Address: {1}", interval, _stopwatch.Elapsed);
							_stopwatch.Reset();

							_stopwatch.Start();
							foreach (var item in collection)
							{
								var ee = item.CountryRegion.Name;
								elasticSearchContext.AddUpdateDocument(item, item.StateProvinceID);
							}

							if (firstRun)
							{
								elasticSearchContext.SaveChangesAndInitMappings();
								firstRun = false;
							}
							else
							{
								elasticSearchContext.SaveChanges();
							}

							_stopwatch.Stop();
							Console.WriteLine("Time taken to insert {0} Address documents: {1}", interval, _stopwatch.Elapsed);
							_stopwatch.Reset();
							pointer = pointer + interval;
							Console.WriteLine("Transferred: {0} items", pointer);
						}
					}
				}
			}
		}
        public void SaveToElasticsearchPerson()
        {
            IElasticsearchMappingResolver ElasticsearchMappingResolver = new ElasticsearchMappingResolver();
            using (var ElasticsearchContext = new ElasticsearchContext("http://localhost:9200/", ElasticsearchMappingResolver))
            {
                //ElasticsearchContext.TraceProvider = new ConsoleTraceProvider();
                using (var modelPerson = new ModelPerson())
                {
                    int pointer = 0;
                    const int interval = 500;
                    int length = modelPerson.Person.Count();

                    while (pointer < length)
                    {
                        stopwatch.Start();
                        var collection = modelPerson.Person.OrderBy(t => t.BusinessEntityID).Skip(pointer).Take(interval).ToList<Person>();
                        stopwatch.Stop();
                        Console.WriteLine("Time taken for select {0} persons: {1}", interval,stopwatch.Elapsed);
                        stopwatch.Reset();

                        foreach (var item in collection)
                        {
                            ElasticsearchContext.AddUpdateDocument(item, item.BusinessEntityID);
                            string t = "yes";
                        }

                        stopwatch.Start();
                        ElasticsearchContext.SaveChanges();
                        stopwatch.Stop();
                        Console.WriteLine("Time taken to insert {0} person documents: {1}", interval, stopwatch.Elapsed);
                        stopwatch.Reset();
                        pointer = pointer + interval;
                        Console.WriteLine("Transferred: {0} items", pointer);
                    }
                }
            }
        }