Exemple #1
0
        public static void Scroll_Should_Return_Document_With_ScrollId()
        {
            // arrage
            var options = ElasticClientMock.GetOptions();
            var query   = new ElasticsearchQuery <Person>(options);
            var person  = new Person {
                Id = "1", Name = "Thiago Barradas", Age = 27, CreateDate = new DateTime(2019, 01, 01)
            };
            var scrollOptions = new ScrollOptions {
                Scroll = "10m", ScrollId = "1234567", Size = 1
            };
            var queryToFilter = Query <Person> .Match(i => i.Field("name").Query("Thiago Barradas"));

            query.ElasticClient = ElasticClientMock.GetElasticClientMock(options, null, person, "1234567");

            // act
            var result = query.Scroll(queryToFilter, scrollOptions);

            // assert
            Assert.NotNull(result);
            Assert.Single(result.Items);
            Assert.Equal(1, result.Total);
            Assert.Equal("1234567", result.ScrollId);
            Assert.Equal("1", result.Items.First().Id);
            Assert.Equal("Thiago Barradas", result.Items.First().Name);
            Assert.Equal("Scroll", ElasticClientMock.LastElasticClientAction);
            Assert.Equal("{\"scroll\":\"10m\",\"scroll_id\":\"1234567\"}", ElasticClientMock.LastQueryBody);
        }
Exemple #2
0
        public ScrollResult <T> Scroll(QueryContainer query, ScrollOptions scrollOptions)
        {
            ISearchResponse <T> elasticResponse = null;

            if (!string.IsNullOrWhiteSpace(scrollOptions.ScrollId))
            {
                ScrollRequest scrollRequest = new ScrollRequest(scrollOptions.ScrollId, scrollOptions.Scroll);
                elasticResponse = this.ElasticClient.Scroll <T>(scrollRequest);
            }
            else
            {
                SearchDescriptor <T> descriptor = new SearchDescriptor <T>();

                descriptor.Index($"{this.Options.DefaultIndexName}*")
                .Type(this.Options.DefaultTypeName)
                .AddQuery(query)
                .AddScroll(scrollOptions)
                .AddSorting(scrollOptions);

                elasticResponse        = this.ElasticClient.Search <T>(descriptor);
                scrollOptions.ScrollId = elasticResponse.ScrollId;
            }

            return(new ScrollResult <T>
            {
                Total = elasticResponse.Total,
                Items = elasticResponse.Documents,
                ScrollId = elasticResponse.ScrollId
            });
        }
Exemple #3
0
 /// <summary>
 /// Waits for element to be present and visible and then clicks it
 /// </summary>
 /// <param name="element"></param>
 /// <param name="scroll"></param>
 /// <param name="scrollDistance"></param>
 /// <param name="scrollToTop"></param>
 public IWebElement Click(By element, ScrollOptions scroll = ScrollOptions.none, int scrollDistance = 50, bool scrollToTop = false)
 {
     WaitForElementPresent(element);
     if (scroll != ScrollOptions.none)
     {
         ScrollToElement(element, scroll, scrollDistance, scrollToTop);
     }
     WaitForEnabled(element);
     return(JustClick(element));
 }
        public static void AddScroll_Should_Not_Add_When_Param_Is_Null()
        {
            // arrange
            var           descriptor = new SearchDescriptor <object>();
            ScrollOptions options    = null;

            // act
            descriptor.AddScroll(options);

            // assert
            var resultAsString = SerializeUtil.Serialize(descriptor);

            Assert.Equal("{}", resultAsString);
        }
Exemple #5
0
        public ScrollResult <TEntity> Scroll(QueryContainer query, ScrollOptions scrollOptions, string index = null, string type = null)
        {
            ISearchResponse <TEntity> elasticResponse = null;

            if (!string.IsNullOrWhiteSpace(scrollOptions.ScrollId))
            {
                ScrollRequest scrollRequest = new ScrollRequest(scrollOptions.ScrollId, scrollOptions.Scroll);

                try
                {
                    elasticResponse = this.ElasticClient.Scroll <TEntity>(scrollRequest);
                }
                catch (Exception e)
                {
                    throw new OperationCanceledException("Invalid result on scroll by id", e);
                }
            }
            else
            {
                SearchDescriptor <TEntity> descriptor = new SearchDescriptor <TEntity>();

                descriptor.Index(index ?? $"{this.Options.DefaultIndexName}*")
                .Type(this.Options.DefaultTypeName)
                .AddQuery(query)
                .AddScroll(scrollOptions)
                .AddSorting(scrollOptions)
                .IgnoreUnavailable(true)
                .AllowNoIndices(true)
                .AllowPartialSearchResults(true);

                try
                {
                    elasticResponse = this.ElasticClient.Search <TEntity>(descriptor);
                }
                catch (Exception e)
                {
                    throw new OperationCanceledException("Invalid result on new scroll", e);
                }

                scrollOptions.ScrollId = elasticResponse.ScrollId;
            }

            return(new ScrollResult <TEntity>
            {
                Total = elasticResponse.Total,
                Items = elasticResponse.Documents,
                ScrollId = elasticResponse.ScrollId
            });
        }
        public static void AddSorting_Should_Add_Desc_Sorting_When_Param_Is_NotNull_And_ScrollOptions()
        {
            // arrange
            var           descriptor = new SearchDescriptor <object>();
            ScrollOptions options    = new ScrollOptions {
                SortField = "createDate", SortMode = Models.SortMode.DESC
            };

            // act
            descriptor.AddSorting(options);

            // assert
            var resultAsString = SerializeUtil.Serialize(descriptor);

            Assert.Equal("{\"sort\":[{\"createDate\":{\"order\":\"desc\"}}]}", resultAsString);
        }
Exemple #7
0
        public static void Scroll_Should_Return_Zero_Document()
        {
            // arrage
            var options       = ElasticClientMock.GetOptions();
            var query         = new ElasticsearchQuery <Person>(options);
            var scrollOptions = new ScrollOptions {
                Scroll = "10m", Size = 1
            };
            var queryToFilter = Query <Person> .Match(i => i.Field("name").Query("Ralph Barradas"));

            query.ElasticClient = ElasticClientMock.GetElasticClientMock(options, null, null, null);

            // act
            var result = query.Scroll(queryToFilter, scrollOptions);

            // assert
            Assert.NotNull(result);
            Assert.Null(result.ScrollId);
            Assert.Empty(result.Items);
            Assert.Equal(0, result.Total);
            Assert.Equal("{\"query\":{\"match\":{\"name\":{\"query\":\"Ralph Barradas\"}}},\"size\":1}", ElasticClientMock.LastQueryBody);
        }
Exemple #8
0
 /// <summary>
 /// Attempts to use javascript to scroll to a particular element
 /// </summary>
 /// <param name="element">By element</param>
 /// <param name="scrollOption">Scroll option</param>
 /// <param name="amountToScrollPast">Scroll past the element by 30 pixels in the indicated direction (to avoid floating elements)</param>
 /// <param name="scrollToTop">true = scroll to top, false (default) = scroll to bottom</param>
 public void ScrollToElement(By element, ScrollOptions scrollOption = ScrollOptions.none, int amountToScrollPast = 30, bool scrollToTop = false)
 {
     try
     {
         WaitForElementPresent(element);                 //just wait for present since it's not in view in IE if it isn't visible.
     }
     catch { }
     if (scrollOption == ScrollOptions.none)
     {
         ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0," + (driver.FindElement(element).Location.Y) + ");");
     }
     else if (scrollOption == ScrollOptions.up)
     {
         ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0," + (driver.FindElement(element).Location.Y - amountToScrollPast) + ");");
     }
     else if (scrollOption == ScrollOptions.down)
     {
         ((IJavaScriptExecutor)driver).ExecuteScript("window.scrollTo(0," + (driver.FindElement(element).Location.Y + amountToScrollPast) + ");");
     }
     else if (scrollOption == ScrollOptions.intoView)
     {
         ((IJavaScriptExecutor)driver).ExecuteScript($"arguments[0].scrollIntoView({scrollToTop.ToString().ToLower()});");                 //scrollIntoView(true) brakes search results page
     }
 }
        static void Main(string[] args)
        {
            // init
            var elasticsearchOptions = new ElasticsearchOptions
            {
                WriteUrl         = "http://localhost:9200",
                ReadUrl          = "http://localhost:9200",
                DefaultTypeName  = "docs",
                DefaultIndexName = "my-index",
                TimeoutInSeconds = 60,
                MaximumRetries   = 5,
                Environment      = "MyServer"
            };

            PersonCommand = new ElasticsearchCommand <Person>(elasticsearchOptions, Person.GenerateIndexName);
            PersonQuery   = new ElasticsearchQuery <Person>(elasticsearchOptions);

            try
            {
                PersonQuery.Get("zz");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            // bulk
            Console.Write("Bulk:");
            var persons = new List <Person>
            {
                { new Person {
                      Id = "1", Name = "Thiago Barradas", Age = 27, CreateDate = new DateTime(2019, 01, 01)
                  } },
                { new Person {
                      Id = "8", Name = "Ralph Legal", Age = 29, CreateDate = new DateTime(2018, 12, 01)
                  } },
                { new Person {
                      Id = "9", Name = "James Bond", Age = 30, CreateDate = new DateTime(2018, 12, 10)
                  } },
                { new Person {
                      Id = "10", Name = "John Doe", Age = 54, CreateDate = new DateTime(2018, 11, 01)
                  } },
                { new Person {
                      Id = "11", Name = "Jow Troll Moon Do", Age = 58, CreateDate = new DateTime(2018, 05, 25)
                  } }
            };

            persons.ForEach(person => Console.WriteLine(person.Name));
            PersonCommand.Bulk(persons);

            // upsert
            Console.WriteLine("Upsert:");
            var otherPerson = new Person {
                Id = "2", Name = "Rafael Barradas", Age = 25, CreateDate = new DateTime(2018, 12, 01)
            };

            Console.WriteLine(otherPerson.Name);
            PersonCommand.Upsert(otherPerson);

            // get by id
            Console.WriteLine("Get:");
            Person personX = PersonQuery.Get("8"); // null
            Person personY = PersonQuery.Get("4"); // John Doe

            Console.WriteLine(personX?.Name);
            Console.WriteLine(personY?.Name);

            // search
            Console.WriteLine("Search:");
            var searchOptions = new SearchOptions
            {
                Page      = 1,
                Size      = 1,
                SortField = "name.keyword",
                SortMode  = BulkAndSearch.Models.SortMode.ASC
            };

            var query = Query <Person> .DateRange(i => i.Field("createDate").LessThan("2018-12-01"));

            var searchResult = PersonQuery.Search(query, searchOptions);

            searchResult.Items.ToList().ForEach(person => Console.WriteLine(person.Name));

            // scroll
            Console.WriteLine("Scroll:");
            var scrollOptions = new ScrollOptions
            {
                Scroll    = "1m",
                Size      = 2,
                SortField = "name.keyword",
                SortMode  = BulkAndSearch.Models.SortMode.ASC
            };

            List <Person> scrollPersons = new List <Person>();

            var scrollResult = PersonQuery.Scroll(query, scrollOptions);

            while (scrollResult.Items.Any())
            {
                scrollPersons.AddRange(scrollResult.Items);

                scrollResult = PersonQuery.Scroll(query, scrollOptions);
            }

            scrollPersons.ForEach(person => Console.WriteLine(person.Name));
        }
        public static SearchDescriptor <T> AddScroll <T>(this SearchDescriptor <T> descriptor, ScrollOptions options) where T : class
        {
            if (options != null)
            {
                var size = options.Size;

                descriptor.Size(size).Scroll(options.Scroll);
            }

            return(descriptor);
        }