Пример #1
0
        void Log_Events_Expected()
        {
            // Dev note - this builds the index, but need to run tests again to work
            var connection = new ElasticConnection("localhost", 9200);
            if (IsIndexExists(ElasticSearchStorage.GetIndex(), connection))
            {
                connection.Delete(new DeleteCommand(ElasticSearchStorage.GetIndex()));
            }

            for (var i = 1; i <= 100; i++)
            {
                Log.Trace("Entry Message : {0}", i);
                Log.Info("Entry Message : {0}", i);
                Log.Error("Entry Message : {0}", i);
            }

            Thread.Sleep(TimeSpan.FromSeconds(5));

            var serializer = new JsonNetSerializer();

            // Build the Search Query
            var query = new QueryBuilder<Entry>().Build();

            // Execute the search
            string result = connection.Post(Commands.Search(ElasticSearchStorage.GetIndex(), "Log"), query);
            var searchResult = serializer.ToSearchResult<Entry>(result);

            // Check all log entries in search index
            Assert.AreEqual(300, searchResult.hits.total);

            // Secondary check, use the instance based loger
            using (var log = Log.GetLogger())
            {
                for (var i = 1; i <= 100; i++)
                {
                    log.Trace("Entry Message : {0}", i);
                    log.Info("Entry Message : {0}", i);
                    log.Error("Entry Message : {0}", i);
                }

                Thread.Sleep(TimeSpan.FromSeconds(5));

                // Build the Search Query
                query = new QueryBuilder<Entry>().Build();

                // Execute the search
                result = connection.Post(Commands.Search(ElasticSearchStorage.GetIndex(), "Log"), query);
                searchResult = serializer.ToSearchResult<Entry>(result);

                // Check all log entries in search index
                Assert.AreEqual(600, searchResult.hits.total);
            }
        }
Пример #2
0
        private static IEnumerable<Tweet> SearchTweets(ElasticConnection connection, JsonNetSerializer serializer)
        {
            string searchCommand = Commands.Search("twitter", "tweet").Pretty();

            /*
{
    "query": {
        "term": {
            "User": {
                "value": "testuser",
                "boost": "5"
            }
        }
    }
}
             */


            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)
                     )
                    // Alternate way 
                    //.Custom(" 'term': {{  '{0}': {{ 'value': '{1}', 'boost': '5' }} }}".AltQuote(), "User", "testuser")
                ).BuildBeautified();

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

            var searchResult = serializer.ToSearchResult<Tweet>(results);


            PrintSearchResults(searchResult, searchCommand, query, results);

            return searchResult.Documents;
        }
Пример #3
0
        void Log_Disabled_Events_Expected()
        {
            Settings.LoggingEnabled = false;

            // Dev note - this builds the index, but need to run tests again to work
            var connection = new ElasticConnection("localhost", 9200);

            for (var i = 1; i <= 100; i++)
            {
                Log.Trace("Entry Message : {0}", i);
                Log.Info("Entry Message : {0}", i);
                Log.Error("Entry Message : {0}", i);
            }

            Thread.Sleep(TimeSpan.FromSeconds(5));

            var serializer = new JsonNetSerializer();

            // Build the Search Query
            var query = new QueryBuilder<Entry>().Build();

            // Execute the search
            string result = connection.Post(Commands.Search(ElasticSearchStorage.GetIndex(), "Log"), query);
            var searchResult = serializer.ToSearchResult<Entry>(result);

            // Check all log entries in search index
            Assert.AreEqual(600, searchResult.hits.total);
        }
Пример #4
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;
                           });
        }