Example #1
0
        public List <Book> Search(string keyword)
        {
            ElasticConnection client = new ElasticConnection("222.186.190.241", 9200);
            //第一个参数是数据库,第二个参数是表
            SearchCommand cmd   = new SearchCommand("xiaoshuo", "xiaoshuo_url");
            var           query = new QueryBuilder <Book>()
                                  .Query(b =>
                                         b.Bool(m =>
                                                //并且关系
                                                m.Must(t =>
                                                //分词的最小单位或关系查询
                                                       t.QueryString(t1 => t1.DefaultField("Name").Query(keyword))
                                                       )
                                                )
                                         ).Size(5).Build();
            var         result     = client.Post(cmd, query);
            var         serializer = new JsonNetSerializer();
            var         results    = serializer.ToSearchResult <Book>(result); //把结果序列化
            List <Book> list       = new List <Book>();
            Book        book;

            foreach (var doc in results.Documents)
            {
                book = new Book()
                {
                    Name = doc.Name,
                    Url  = doc.Url
                };
                list.Add(book);
            }
            return(list);
        }
Example #2
0
        static void Main1(string[] args)
        {
            /*
             * Person p1 = new Person();
             * p1.Id = 1;
             * p1.Age = 10;
             * p1.Name = "欧阳帅帅";
             * p1.Desc = "欧阳锋家的帅哥公子,人送外号‘小杨中科’";*/

            Person p1 = new Person();

            p1.Id   = 2;
            p1.Age  = 8;
            p1.Name = "丑娘娘";
            p1.Desc = "二丑家的姑娘,是最美丽的女孩";

            ElasticConnection client = new ElasticConnection("localhost", 9200);
            var serializer           = new JsonNetSerializer();
            //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键”
            IndexCommand cmd = new IndexCommand("zsz", "persons", p1.Id.ToString());
            //Put()第二个参数是要插入的数据
            OperationResult result      = client.Put(cmd, serializer.Serialize(p1));
            var             indexResult = serializer.ToIndexResult(result.Result);

            if (indexResult.created)
            {
                Console.WriteLine("创建了");
            }
            else
            {
                Console.WriteLine("没创建" + indexResult.error);
            }
            Console.ReadKey();
        }
        public void DoubleDisposeDoesNotThrow()
        {
            var connection = new ElasticConnection(endpoint, UserName, Password);

            connection.Dispose();
            connection.Dispose();
        }
Example #4
0
        private static void IndexTweet(Tweet tweet, string id, ElasticConnection connection, JsonNetSerializer serializer)
        {
            /*
             * $ curl -XPUT 'http://localhost:9200/twitter/tweet/1?pretty=true' -d '{
             *       "User" : "testUser",
             *       "Message" : "trying out Elastic Search"
             *   }'
             */

            // This is url that will be requested from ES. We can grab it and put to any ES admin console (like ElasticHead) to debug ES behavior.
            string indexCommand = Commands.Index(index: "twitter", type: "tweet", id: id)
                                  .Refresh(true)
                                  .Pretty(); // this will generate: twitter/tweet/1?pretty=true

            // This variable contains JSON of serialized tweet, thus we can check if our object serialized correctly
            // or use it directly in ES admin console.
            string tweetJson = serializer.ToJson(tweet);

            var result = connection.Put(indexCommand, tweetJson);

            // Parse index result.
            IndexResult indexResult = serializer.ToIndexResult(result);


            PrintIndexCommand(result, indexResult, indexCommand, tweetJson);
        }
Example #5
0
        private static long CountTweets(ElasticConnection connection, JsonNetSerializer serializer)
        {
            string countCommand = Commands.Count("twitter", "tweet").Pretty();

            string query = new SingleQueryBuilder <Tweet>()
                           .Term(t => t
                                 .Field(x => x.User)
                                 .Value("testuser")
                                 )
                           .BuildBeautified();           // or .Buid(); to get condensed single line query.

            /* or alternatively
             * query = new TermQuery<Tweet>()
             *              .Field(x => x.User)
             *              .Value("testuser")
             *              .BuildBeautified();  // or .Buid(); to get condensed single line query.
             */

            var results = connection.Post(countCommand, query);

            var searchResult = serializer.ToCountResult(results);

            PrintCountResults(searchResult, countCommand, query, results);

            return(searchResult.count);
        }
Example #6
0
 public Elastic(string hostName, int port)
 {
     this._hostName = hostName ?? ElasticConfig.HostName;
     this._port     = port == 0 ? ElasticConfig.Port : port;
     Client         = new ElasticConnection(_hostName, _port, ElasticConfig.TimeOutSeconds);
     serializer     = new JsonNetSerializer();
 }
Example #7
0
        public static void ParseResponseReturnsParsedResponseGivenValidStream()
        {
            const int    took   = 2;
            const int    shards = 1;
            const int    hits   = 1;
            const double score  = 0.3141;
            const string index  = "testIndex";
            const string type   = "testType";
            const string id     = "testId";

            var responseString = BuildResponseString(took, shards, hits, score, index, type, id);

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(responseString)))
            {
                var response = ElasticConnection.ParseResponse(stream, log);
                Assert.NotNull(response);
                Assert.Equal(took, response.took);
                Assert.Equal(hits, response.hits.total);
                Assert.Equal(score, response.hits.max_score);

                Assert.NotEmpty(response.hits.hits);
                Assert.Equal(score, response.hits.hits[0]._score);
                Assert.Equal(index, response.hits.hits[0]._index);
                Assert.Equal(type, response.hits.hits[0]._type);
                Assert.Equal(id, response.hits.hits[0]._id);
            }
        }
Example #8
0
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var messageHandler = new SpyMessageHandler();
            var spyLog         = new SpyLog();

            messageHandler.Response.Content = new StringContent(responseString);
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass", index: "SearchIndex");
            var request         = new SearchRequest {
                DocumentType = "abc123", Size = 2112
            };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                token,
                spyLog);

            Assert.Equal(4, spyLog.Entries.Count);
            Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message);
            Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message);
            Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success);
            Assert.True(new Regex(@"Deserialized \d+ bytes into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success);
        }
Example #9
0
        private static Task <IEnumerable <Tweet> > SearchTweetsAsync(ElasticConnection connection, JsonNetSerializer serializer)
        {
            string searchCommand = Commands.Search("twitter", "tweet").Pretty();

            string query = new QueryBuilder <Tweet>()
                           .Query(qry => qry
                                  .Term(term => term
                                        .Field(tweet => tweet.User)
                                        .Value("testUser".ToLower()) // by default terms query requires lowercased values.
                                        .Boost(5)
                                        )
                                  ).BuildBeautified();


            return(connection.PostAsync(searchCommand, query)
                   // process search results asynchronously
                   .ContinueWith(searchTask => {
                OperationResult results = searchTask.Result;
                var searchResult = serializer.ToSearchResult <Tweet>(results);

                Console.WriteLine("ASYNC Search Results: \r\n");
                PrintSearchResults(searchResult, searchCommand, query, results);

                return searchResult.Documents;
            }));
        }
Example #10
0
        static void Main(string[] args)
        {
            ElasticConnection client = new ElasticConnection("localhost", 9200);
            SearchCommand     cmd    = new SearchCommand("MyTest", "persons");
            var query = new QueryBuilder <Person>().Query(b => b.Bool(m =>
                        //并且关系
                                                                      m.Must(t =>
                        //分词的最小单位或关系查询
                                                                             t.QueryString(t1 => t1.DefaultField("Name").Query("求")))))
                        //分页
                        //        .From(0)
                        //        .Size(10)
                        ////排序
                        //.Sort(c => c.Field("age", SortDirection.desc))
                        //////添加高亮
                        //.Highlight(h => h
                        //        .PreTags("<b>")
                        //        .PostTags("</b>")
                        //        .Fields(
                        //            f => f.FieldName("Name").Order(HighlightOrder.score))
                        //)
                        .Build();
            var result     = client.Post(cmd, query);
            var serializer = new JsonNetSerializer();
            var list       = serializer.ToSearchResult <Person>(result);

            foreach (var doc in list.Documents)
            {
                Console.WriteLine(doc.Id);
            }
        }
Example #11
0
        static void Main1()
        {
            Person p1 = new Person();

            p1.Id   = 1;
            p1.Age  = 8;
            p1.Name = "ll";
            p1.Desc = "最美丽的女孩";

            ElasticConnection client = new ElasticConnection("localhost", 9200);
            var serializer           = new JsonNetSerializer();
            //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键”
            IndexCommand cmd = new IndexCommand("Hlxzsz", "persons", p1.Id.ToString());
            //Put()第二个参数是要插入的数据
            OperationResult result      = client.Put(cmd, serializer.Serialize(p1));
            var             indexResult = serializer.ToIndexResult(result.Result);

            if (indexResult.created)
            {
                Console.WriteLine("创建了");
            }
            else
            {
                Console.WriteLine("没创建" + indexResult.error);
            }
            Console.ReadKey();
        }
Example #12
0
        /// <summary>
        /// elasticSearch(搜索下面方法中添加的数据)
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ElasticConnection client = new ElasticConnection("localhost", 9200);
            SearchCommand     cmd    = new SearchCommand("verycd", "items");
            //首先创建我要查询的结果(VerycdItem)
            var query = new QueryBuilder <VerycdItem>()
                        .Query(b =>
                               b.Bool(m =>
                                      //并且关系
                                      m.Must(t =>
                                      //分词的最小单位或关系查询   //DefaultField是模糊匹配,Term是精确匹配(int,long类型),Must是必须(and),Should是或者(or),MustNot是否定(不能含有)
                                             t.QueryString(t1 => t1.DefaultField("content").Query("成龙"))
                                             ).Must(t => t.QueryString(t1 => t1.DefaultField("category2").Query("纪录")))
                                      )
                               ).Size(100)//默认返回10条,Size(100)表示返回最多100条
                        .Build();
            //elasticSearch中没有更新操作,先删在加
            // DeleteCommand delCmd = new DeleteCommand()
            //得到查询结果
            var result     = client.Post(cmd, query);
            var serializer = new JsonNetSerializer();
            //把查询结果序列化
            var searchResult = serializer.ToSearchResult <VerycdItem>(result);

            //searchResult.hits.total; //一共有多少匹配结果  10500
            // searchResult.Documents;//当前页的查询结果
            foreach (var doc in searchResult.Documents)
            {
                Console.WriteLine(doc.title + "," + doc.category1 + "," + doc.category2);
            }
            Console.ReadKey();
        }
Example #13
0
        static void Main(string[] args)
        {
            ElasticConnection client = new ElasticConnection("localhost", 9200);
            SearchCommand     cmd    = new SearchCommand("movies", "电影");
            var query = new QueryBuilder <VerycdItem>()
                        .Query(b =>
                               b.Bool(m =>
                                      //并且关系
                                      m.Must(t =>
                                      //分词的最小单位或关系查询
                                             t.QueryString(t1 => t1.DefaultField("content").Query("成龙"))
                                             ).Must(t => t.QueryString(t1 => t1.DefaultField("category2").Query("纪录")))
                                      )
                               ).Size(100)
                        .Build();
            // DeleteCommand delCmd = new DeleteCommand()
            var result       = client.Post(cmd, query);
            var serializer   = new JsonNetSerializer();
            var searchResult = serializer.ToSearchResult <VerycdItem>(result);

            //searchResult.hits.total; //一共有多少匹配结果  10500
            // searchResult.Documents;//当前页的查询结果
            foreach (var doc in searchResult.Documents)
            {
                Console.WriteLine(doc.title + "," + doc.category1 + "," + doc.category2);
            }
            Console.ReadKey();
        }
Example #14
0
        public async Task RunSaveToElasticSearchPipelineAsync(string index, string type = null, string primaryKeyField = null)
        {
            var it = this.Data;

            var     elastic = new ElasticConnection(index, GlobalSettings.ElasticSearchConnectionString, type: type);
            JObject obj     = new JObject(it);
            var     json    = obj.ToString();

            if (primaryKeyField == null)
            {
                var ret = await elastic.IndexAsync(json);

                if (!ret.Success)
                {
                    Log.Error(ret.Body);
                }
            }
            else
            {
                var id  = (string)it[primaryKeyField];
                var ret = await elastic.IndexAsync(id, json);

                if (!ret.Success)
                {
                    Log.Error($"{id} -> {ret.Body}");
                }
            }
        }
        public ElasticClientProvider(
            ILogger <IElasticClientProvider> logger,
            IConfiguration config,
            IElasticClientFactory elasticClientFactory,
            ElasticConnection connection)
        {
            this.elasticClientFactory = elasticClientFactory;
            this.logger = logger;

            this.connection = connection;

            // Reading credentials from IConfiguration directly allows to have credentials be stored either
            // in the appsettings file (for development), or in the environment variables (for staging/production).
            this.readerUsername = config.GetValue <string>("ES_READER_USERNAME");
            this.readerPassword = config.GetValue <string>("ES_READER_PASSWORD");
            this.writerUsername = config.GetValue <string>("ES_WRITER_USERNAME");
            this.writerPassword = config.GetValue <string>("ES_WRITER_PASSWORD");

            var missingValues = string.Join(
                "/",
                new[]
            {
                this.readerUsername == null ? "Reader Username" : null, this.readerPassword == null ? "Reader Password" : null,
                this.writerUsername == null ? "Writer Username" : null, this.writerPassword == null ? "Writer Password" : null
            }.Where(x => x != null));

            if (!string.IsNullOrEmpty(missingValues))
            {
                var exception = new ElasticConnectionException(missingValues);
                this.logger.LogWarning(exception.Message);
                throw exception;
            }
        }
Example #16
0
        static void Main1(string[] args)
        {
            Person p2 = new Person();

            p2.Id   = 3;
            p2.Age  = 10;
            p2.Name = "handsome person";
            p2.Desc = "handsome look, ugly heart";

            //Person p1 = new Person();
            //p1.Id = 4;
            //p1.Age = 8;
            //p1.Name = "ugly person";
            //p1.Desc = "ugly look, nice heart";

            ElasticConnection client = new ElasticConnection("localhost", 9200);
            var serializer           = new JsonNetSerializer();
            //第一个参数相当于“数据库”,第二个参数相当于“表”,第三个参数相当于“主键”
            IndexCommand cmd = new IndexCommand("zsz", "persons", p2.Id.ToString());
            //Put()第二个参数是要插入的数据
            OperationResult result      = client.Put(cmd, serializer.Serialize(p2));
            var             indexResult = serializer.ToIndexResult(result.Result);

            if (indexResult.created)
            {
                Console.WriteLine("created");
            }
            else
            {
                Console.WriteLine("faled: " + indexResult.error);
            }
            Console.ReadKey();
        }
Example #17
0
        public void Search()
        {
            BookHelper        bookHelper = new BookHelper();
            ElasticConnection client     = new ElasticConnection("222.186.190.241", 9200);
            //第一个参数是数据库,第二个参数是表
            SearchCommand cmd   = new SearchCommand("xiaoshuo", "xiaoshuo_url");
            var           query = new QueryBuilder <Book>()
                                  .Query(b =>
                                         b.Bool(m =>
                                                //并且关系
                                                m.Must(t =>
                                                //分词的最小单位或关系查询
                                                       t.QueryString(t1 => t1.DefaultField("Name").Query("总裁"))
                                                       )
                                                )
                                         ).Size(5).Build();
            var  result     = client.Post(cmd, query);
            var  count      = result.Result.Count();
            var  serializer = new JsonNetSerializer();
            var  results    = serializer.ToSearchResult <Book>(result); //把结果序列化
            Book book;

            foreach (var doc in results.Documents)
            {
                book = new Book()
                {
                    Name = doc.Name,
                    Url  = doc.Url
                };
                Console.WriteLine(book.Name);
                Console.WriteLine(book.Url);
            }
        }
        public async Task DisposeKillsHttpClient()
        {
            var connection = new ElasticConnection(endpoint, UserName, Password);

            connection.Dispose();

            await Assert.ThrowsAsync <NullReferenceException>(() => connection.HttpClient.GetAsync(new Uri("http://something.com")));
        }
        public void ConstructorCreatesHttpClientWithSpecifiedTimeout()
        {
            var timeout    = TimeSpan.FromSeconds(3);
            var connection = new ElasticConnection(endpoint, UserName, Password, timeout);

            Assert.NotNull(connection.HttpClient);
            Assert.Equal(timeout, connection.Timeout);
        }
        public void ConstructorCreatesHttpClientWithSpecifiedTimeout()
        {
            var timeout = TimeSpan.FromSeconds(3);
            var connection = new ElasticConnection(endpoint, UserName, Password, timeout);

            Assert.NotNull(connection.HttpClient);
            Assert.Equal(timeout, connection.Timeout);
        }
        public void ConstructorWithThreeArgsSetsPropertiesFromParameters()
        {
            var connection = new ElasticConnection(endpoint, UserName, Password);

            Assert.Equal(endpoint, connection.Endpoint);
            Assert.Equal(UserName, connection.UserName);
            Assert.Equal(Password, connection.Password);
        }
        public void ConstructorWithThreeArgsSetsPropertiesFromParameters()
        {
            var connection = new ElasticConnection(endpoint, UserName, Password);

            Assert.Equal(endpoint, connection.Endpoint);
            Assert.Equal(UserName, connection.UserName);
            Assert.Equal(Password, connection.Password);
        }
Example #23
0
        public LogController()
        {
            //ActivityLogService
            var logElasticConnection = new ElasticConnection <UserActivityLog>(new UserActivityLogElasticConfig());

            _activityLogService = new ActivityLogService <UserActivityLog>
                                      (new ActivityLogElasticRepository <UserActivityLog>(logElasticConnection));
        }
        public void ConstructorCreatesHttpClientWithDefaultTimeout()
        {
            var defaultTimeout = TimeSpan.FromSeconds(10);
            var connection     = new ElasticConnection(endpoint, UserName, Password);

            Assert.NotNull(connection.HttpClient);
            Assert.Equal(defaultTimeout, connection.Timeout);
        }
        public void ConstructorCreatesHttpClientWithDefaultTimeout()
        {
            var defaultTimeout = TimeSpan.FromSeconds(10);
            var connection = new ElasticConnection(endpoint, UserName, Password);

            Assert.NotNull(connection.HttpClient);
            Assert.Equal(defaultTimeout, connection.Timeout);
        }
Example #26
0
        public void UriFormatting(string index, string documentType, string expectedUri)
        {
            var connection = new ElasticConnection(new Uri("http://a.b.com:9000/"), index: index);

            Assert.Equal(expectedUri, connection.GetSearchUri(new SearchRequest {
                DocumentType = documentType
            }).ToString());
        }
        public void UrlPathContainsIndexSpecifier()
        {
            const string expectedIndex = "myIndex";
            var indexConnection = new ElasticConnection(defaultConnection.Endpoint, index: expectedIndex);
            var formatter = new PostBodyRequestFormatter(indexConnection, mapping, new ElasticSearchRequest { Type = "type1" });

            Assert.Contains(expectedIndex, formatter.Uri.AbsolutePath);
        }
Example #28
0
        public void Execute()
        {
            Console.WriteLine("PLAIN SAMPLE");

            var connection = new ElasticConnection("localhost", 9200);
            var serializer = new JsonNetSerializer();

            var tweet = new Tweet
            {
                User    = "******",
                Message = "trying out Elastic Search"
            };

            var anotherTweet = new Tweet
            {
                User    = "******",
                Message = "one more message"
            };


            var tweets = new List <Tweet> {
                new Tweet
                {
                    User    = "******",
                    Message = "first bulk tweet"
                },
                new Tweet
                {
                    User    = "******",
                    Message = "second bulk tweet"
                },
                new Tweet
                {
                    User    = "******",
                    Message = "third bulk tweet"
                },
            };


            IndexTweet(tweet, "1", connection, serializer);

            IndexTweet(anotherTweet, "2", connection, serializer);

            BulkTweetIndex(tweets, connection, serializer);

            GetTweet("1", serializer, connection);

            SearchTweets(connection, serializer);

            SearchTweetsAsync(connection, serializer).Wait();

            CountTweets(connection, serializer);

            DeleteTweeterIndex(connection, serializer);

            Console.WriteLine("Press any key");
            Console.ReadKey();
        }
Example #29
0
        /// <summary>
        /// Create a new SearchRequestFormatter for the given connection, mapping and search request.
        /// </summary>
        /// <param name="connection">The ElasticConnection to prepare the SearchRequest for.</param>
        /// <param name="mapping">The IElasticMapping used to format the SearchRequest.</param>
        /// <param name="searchRequest">The SearchRequest to be formatted.</param>
        public SearchRequestFormatter(ElasticConnection connection, IElasticMapping mapping, SearchRequest searchRequest)
        {
            this.connection    = connection;
            this.mapping       = mapping;
            this.searchRequest = searchRequest;

            body = new Lazy <string>(() => CreateBody().ToString(connection.Options.Pretty ? Formatting.Indented : Formatting.None));
            uri  = CreateUri();
        }
Example #30
0
        public static ElasticConnection ElsaticConnection()
        {
            string ElasticLink = ConfigurationManager.AppSettings["ElasticLink"]; //local
            string ElasticPort = ConfigurationManager.AppSettings["ElasticPort"];

            var connection = new ElasticConnection(ElasticLink, Int32.Parse(ElasticPort));

            return(connection);
        }
        /// <summary>
        /// Create a new SearchRequestFormatter for the given connection, mapping and search request.
        /// </summary>
        /// <param name="connection">The ElasticConnection to prepare the SearchRequest for.</param>
        /// <param name="mapping">The IElasticMapping used to format the SearchRequest.</param>
        /// <param name="searchRequest">The SearchRequest to be formatted.</param>
        public SearchRequestFormatter(ElasticConnection connection, IElasticMapping mapping, SearchRequest searchRequest)
        {
            this.connection = connection;
            this.mapping = mapping;
            this.searchRequest = searchRequest;

            body = new Lazy<string>(() => CreateBody().ToString(connection.Options.Pretty ? Formatting.Indented : Formatting.None));
            uri = CreateUri();
        }
        public void UriFormatting(string index, string documentType, string expectedUri)
        {
            var connection = new ElasticConnection(new Uri("http://a.b.com:9000/"), index: index);
            var formatter  = new SearchRequestFormatter(connection, mapping, new SearchRequest {
                DocumentType = documentType
            });

            Assert.Equal(expectedUri, formatter.Uri.ToString());
        }
Example #33
0
        public void UrlPathContainsIndexSpecifier()
        {
            const string expectedIndex   = "myIndex";
            var          indexConnection = new ElasticConnection(defaultConnection.Endpoint, index: expectedIndex);
            var          formatter       = new PostBodyRequestFormatter(indexConnection, mapping, new ElasticSearchRequest {
                Type = "type1"
            });

            Assert.Contains(expectedIndex, formatter.Uri.AbsolutePath);
        }
        public static async Task NoAuthorizationWithEmptyUserName()
        {
            var messageHandler = new SpyMessageHandler();
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"));
            var processor = new ElasticRequestProcessor(localConnection, mapping, log, retryPolicy);
            var request = new SearchRequest { DocumentType = "docType" };

            await processor.SearchAsync(request);

            Assert.Null(messageHandler.Request.Headers.Authorization);
        }
Example #35
0
        public EsConnector(string host, int port, string login, string password)
        {
            var credentials = !login.IsNullOrEmpty() && !password.IsNullOrEmpty()
                                ? new NetworkCredential(login, password)
                                : null;

            Connection = new ElasticConnection(host, port)
            {
                Credentials = credentials
            };
        }
Example #36
0
        public void BodyDoesNotContainMinScoreWhenUnspecified()
        {
            var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.Zero);

            var formatter = new SearchRequestFormatter(connection, mapping, new SearchRequest());
            var body      = JObject.Parse(formatter.Body);

            var result = body["min_score"];

            Assert.Null(result);
        }
        public static async void NonSuccessfulHttpRequestThrows()
        {
            var messageHandler = new SpyMessageHandler { Response = { StatusCode = HttpStatusCode.NotFound } };
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass");
            var processor = new ElasticRequestProcessor(localConnection, mapping, log, retryPolicy);
            var request = new SearchRequest { DocumentType = "docType" };

            var ex = await Record.ExceptionAsync(() => processor.SearchAsync(request, CancellationToken.None));

            Assert.IsType<HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
        public ElasticRequestProcessor(ElasticConnection connection, IElasticMapping mapping, ILog log, IRetryPolicy retryPolicy)
        {
            Argument.EnsureNotNull("connection", connection);
            Argument.EnsureNotNull("mapping", mapping);
            Argument.EnsureNotNull("log", log);
            Argument.EnsureNotNull("retryPolicy", retryPolicy);

            this.connection = connection;
            this.mapping = mapping;
            this.log = log;
            this.retryPolicy = retryPolicy;
        }
        public static void NonSuccessfulHttpRequestThrows()
        {
            var messageHandler = new SpyMessageHandler();
            messageHandler.Response.StatusCode = HttpStatusCode.NotFound;
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass");
            var processor = new ElasticRequestProcessor(localConnection, mapping, log, retryPolicy);
            var request = new ElasticSearchRequest { Type = "docType" };

            var ex = Record.Exception(() => processor.SearchAsync(request).GetAwaiter().GetResult());

            Assert.IsType<HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
        public static async Task ForcesBasicAuthorizationWhenProvidedWithUsernameAndPassword()
        {
            var messageHandler = new SpyMessageHandler();
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass");
            var processor = new ElasticRequestProcessor(localConnection, mapping, log, retryPolicy);
            var request = new SearchRequest { DocumentType = "docType" };

            await processor.SearchAsync(request);

            var auth = messageHandler.Request.Headers.Authorization;
            Assert.NotNull(auth);
            Assert.Equal("Basic", auth.Scheme);
            Assert.Equal("myUser:myPass", Encoding.ASCII.GetString(Convert.FromBase64String(auth.Parameter)));
        }
        public void ConstructorWithAllArgsSetsPropertiesFromParameters()
        {
            var expectedEndpoint = new Uri("http://coruscant.gov");
            var expectedTimeout = TimeSpan.FromSeconds(1234);
            const string expectedIndex = "h2g2";
            var expectedOptions = new ElasticConnectionOptions { Pretty = true };

            var actual = new ElasticConnection(expectedEndpoint, UserName, Password, expectedTimeout, expectedIndex, expectedOptions);

            Assert.Equal(expectedEndpoint, actual.Endpoint);
            Assert.Equal(expectedTimeout, actual.Timeout);
            Assert.Equal(expectedIndex, actual.Index);
            Assert.Equal(expectedOptions, actual.Options);
        }
        public static async void SearchAsyncCapturesRequestInfoOnFailure()
        {
            var spyLog = new SpyLog();
            var brokenConnection = new ElasticConnection(new Uri("http://localhost:12"), index: "MyIndex");
            var processor = new ElasticRequestProcessor(brokenConnection, mapping, spyLog, new RetryPolicy(spyLog, 100, 1));
            var searchRequest = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(brokenConnection, mapping, searchRequest);

            var ex = await Record.ExceptionAsync(() => processor.SearchAsync(searchRequest, CancellationToken.None));

            Assert.IsType<RetryFailedException>(ex);
            var retryLogEntry = Assert.Single(spyLog.Entries, s => s.AdditionalInfo.ContainsKey("category") && s.AdditionalInfo["category"].Equals("retry"));
            Assert.Equal("MyIndex", retryLogEntry.AdditionalInfo["index"]);
            Assert.Equal(brokenConnection.GetSearchUri(searchRequest), retryLogEntry.AdditionalInfo["uri"]);
            Assert.Equal(formatter.Body, retryLogEntry.AdditionalInfo["query"]);
        }
	// Use this for initialization
	void Start () {

		//MapCameraControl.main = this;
		//mainCamera = this.transform.FindChild("PrimaryRotator").FindChild("Main Camera");
		centralCube = GameObject.Find("CentralCube").transform;//this.transform.FindChild("CentralCube");


		mapMaker = this.gameObject.GetComponent("MapMaker") as MapMaker;
		elasticConnection = this.gameObject.GetComponent<ElasticConnection>();

		//regularRotation = new Vector3(0f, 65f, 270f);
		/*regularRotation = mainCamera.localRotation.eulerAngles;
		regularRotationM = transform.localRotation.eulerAngles;
		birdsRotation = new Vector3(0f, 0f, 0f);
		birdsRotationM = new Vector3(0f, 0f, 0f);

		postPos = new Vector3(126,126,-240);
		postPosM = new Vector3(0,0,0);*/

		activeScreen = UIName.Landing;
		initUI();
		MapMaker.ActiveFloor = MapMaker.floors[0];
	}
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var messageHandler = new SpyMessageHandler();
            var log = new SpyLog();
            messageHandler.Response.Content = new StringContent(responseString);
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass", index: "SearchIndex");
            var processor = new ElasticRequestProcessor(localConnection, mapping, log, retryPolicy);
            var request = new SearchRequest { DocumentType = "abc123", Size = 2112 };

            await processor.SearchAsync(request);

            Assert.Equal(4, log.Messages.Count);
            Assert.Equal(@"[VERBOSE] Request: POST http://localhost/SearchIndex/abc123/_search", log.Messages[0]);
            Assert.Equal(@"[VERBOSE] Body:" +'\n' + @"{""size"":2112,""timeout"":""10s""}", log.Messages[1]);
            Assert.True(new Regex(@"\[VERBOSE\] Response: 200 OK \(in \d+ms\)").Match(log.Messages[2]).Success);
            Assert.True(new Regex(@"\[VERBOSE\] Deserialized \d+ bytes into 1 hits in \d+ms").Match(log.Messages[3]).Success);
        }
        public void BodyContainsTimeoutWhenSpecified()
        {
            const string expectedTimeout = "15s";
            var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.FromSeconds(15));

            var formatter = new PostBodyRequestFormatter(connection, mapping, new ElasticSearchRequest());
            var body = JObject.Parse(formatter.Body);

            var result = TraverseWithAssert(body, "timeout");
            Assert.Equal(expectedTimeout, result);
        }
        public void ConstructorWithOneArgSetsPropertyFromParameter()
        {
            var connection = new ElasticConnection(endpoint);

            Assert.Equal(endpoint, connection.Endpoint);
        }
        public static async void SearchAsyncThrowsTaskCancelledExceptionWithAlreadyCancelledCancellationToken()
        {
            var spyLog = new SpyLog();
            var localConnection = new ElasticConnection(new Uri("http://localhost"), index: "SearchIndex");
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                formatter.Body,
                request,
                new CancellationToken(true),
                spyLog));

            Assert.IsType<TaskCanceledException>(ex);
        }
        public void PrettyChangesUriQueryParameterWhenDifferentValueAlreadyExists()
        {
            var connection = new ElasticConnection(new Uri("http://coruscant.gov/some?pretty=false&human=true"),
                options: new ElasticConnectionOptions { Pretty = true });
            var prettyUri = connection.GetSearchUri(new SearchRequest { DocumentType = "type1", Filter = criteria });

            var parameters = prettyUri.GetComponents(UriComponents.Query, UriFormat.Unescaped).Split('&');
            Assert.Equal(2, parameters.Length);
            Assert.Contains("human=true", parameters);
            Assert.Contains("pretty=true", parameters);
        }
        public void UriFormatting(string index, string documentType, string expectedUri)
        {
            var connection = new ElasticConnection(new Uri("http://a.b.com:9000/"), index: index);

            Assert.Equal(expectedUri, connection.GetSearchUri(new SearchRequest { DocumentType = documentType }).ToString());
        }
        public static async Task LogsDebugMessagesDuringExecution()
        {
            var responseString = BuildResponseString(2, 1, 1, 0.3141, "testIndex", "testType", "testId");
            var messageHandler = new SpyMessageHandler();
            var spyLog = new SpyLog();
            messageHandler.Response.Content = new StringContent(responseString);
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass", index: "SearchIndex");
            var request = new SearchRequest { DocumentType = "abc123", Size = 2112 };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                token,
                spyLog);

            Assert.Equal(4, spyLog.Entries.Count);
            Assert.Equal(@"Request: POST http://localhost/SearchIndex/abc123/_search", spyLog.Entries[0].Message);
            Assert.Equal(@"Body:" + '\n' + @"{""size"":2112,""timeout"":""10s""}", spyLog.Entries[1].Message);
            Assert.True(new Regex(@"Response: 200 OK \(in \d+ms\)").Match(spyLog.Entries[2].Message).Success);
            Assert.True(new Regex(@"Deserialized \d+ bytes into 1 hits in \d+ms").Match(spyLog.Entries[3].Message).Success);
        }
        public static async void NonSuccessfulHttpRequestThrows()
        {
            var messageHandler = new SpyMessageHandler();
            messageHandler.Response.StatusCode = HttpStatusCode.NotFound;
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"), "myUser", "myPass");
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            var ex = await Record.ExceptionAsync(() => localConnection.SearchAsync(
                formatter.Body,
                request,
                token,
                log));

            Assert.IsType<HttpRequestException>(ex);
            Assert.Equal("Response status code does not indicate success: 404 (Not Found).", ex.Message);
        }
Example #52
0
 protected RequestFormatter(ElasticConnection connection, ElasticSearchRequest searchRequest)
 {
     Connection = connection;
     SearchRequest = searchRequest;
 }
        public void BodyContainsTermsFacetWithDefaultSizeFromConnection()
        {
            const int expectedSize = 678;
            var sizedConnection = new ElasticConnection(defaultConnection.Endpoint, options:new ElasticConnectionOptions { SearchSizeDefault = expectedSize });
            var expectedFacet = new TermsFacet("Totals", null, "OrderTotal", "OrderCost");
            var searchRequest = new SearchRequest { Facets = new List<IFacet>(new[] { expectedFacet }) };

            var formatter = new SearchRequestFormatter(sizedConnection, mapping, searchRequest);
            var body = JObject.Parse(formatter.Body);

            var result = body.TraverseWithAssert("facets", expectedFacet.Name, expectedFacet.Type);

            Assert.Equal(expectedSize.ToString(CultureInfo.InvariantCulture), result.TraverseWithAssert("size").ToString());
        }
        public async Task DisposeKillsHttpClient()
        {
            var connection = new ElasticConnection(endpoint, UserName, Password);

            connection.Dispose();

            await Assert.ThrowsAsync<NullReferenceException>(() => connection.HttpClient.GetAsync(new Uri("http://something.com")));
        }
        public void ConstructorCreatesDefaultOptions()
        {
            var actual = new ElasticConnection(endpoint);

            Assert.NotNull(actual.Options);
        }
        public void BodyDoesNotContainTimeoutWhenZero()
        {
            var connection = new ElasticConnection(new Uri("http://localhost/"), timeout: TimeSpan.Zero);

            var formatter = new PostBodyRequestFormatter(connection, mapping, new ElasticSearchRequest());
            var body = JObject.Parse(formatter.Body);

            var result = body["timeout"];
            Assert.Null(result);
        }
        public void DoubleDisposeDoesNotThrow()
        {
            var connection = new ElasticConnection(endpoint, UserName, Password);

            connection.Dispose();
            connection.Dispose();
        }
        public void PrettySetsUriQueryWhenNoOtherQueryUriParameters()
        {
            var connection = new ElasticConnection(new Uri("http://coruscant.gov/some"), options: new ElasticConnectionOptions { Pretty = true });
            var prettyUri = connection.GetSearchUri(new SearchRequest { DocumentType = "type1", Filter = criteria });

            Assert.Equal("pretty=true", prettyUri.GetComponents(UriComponents.Query, UriFormat.Unescaped));
        }
        public static async Task NoAuthorizationWithEmptyUserName()
        {
            var messageHandler = new SpyMessageHandler();
            var localConnection = new ElasticConnection(messageHandler, new Uri("http://localhost"));
            var request = new SearchRequest { DocumentType = "docType" };
            var formatter = new SearchRequestFormatter(localConnection, mapping, request);

            await localConnection.SearchAsync(
                formatter.Body,
                request,
                token,
                log);

            Assert.Null(messageHandler.Request.Headers.Authorization);
        }
 public PostBodyRequestFormatter(ElasticConnection connection, IElasticMapping mapping, ElasticSearchRequest searchRequest)
     : base(connection, searchRequest)
 {
     this.mapping = mapping;
     body = new Lazy<string>(() => CreateJsonPayload().ToString(Formatting.None));
 }