Exemple #1
0
 public void construct_client_with_null()
 {
     Assert.Throws<ArgumentNullException>(() =>
     {
         var client = new ElasticClient(null);
     });
 }
Exemple #2
0
        public void connect_to_unknown_hostname()
        {
            Assert.DoesNotThrow(() =>
            {
                var settings = new ConnectionSettings("youdontownthis.domain.do.you", 80);
                var client = new ElasticClient(settings);
                ConnectionStatus connectionStatus;
                client.TryConnect(out connectionStatus);

                Assert.False(client.IsValid);
                Assert.True(connectionStatus != null);
                Assert.True(connectionStatus.Error.HttpStatusCode == System.Net.HttpStatusCode.BadGateway
                    || connectionStatus.Error.ExceptionMessage.StartsWith("The remote name could not be resolved"));
            });
        }
Exemple #3
0
        static void Main(string[] args)
        {
            var filePath = args.First();
            Program.ResourceLock = new SemaphoreSlim(15);

            var elasticSettings = new ConnectionSettings("127.0.0.1.", 9200)
                                        .SetDefaultIndex("mpdreamz")
                                        .SetMaximumAsyncConnections(50);
            var client = new ElasticClient(elasticSettings);
            ConnectionStatus connectionStatus;
            if (!client.TryConnect(out connectionStatus))
            {
                Console.Error.WriteLine("Could not connect to {0}:\r\n{1}",
                    elasticSettings.Host, connectionStatus.Error.OriginalException.Message);
                Console.Read();
                return;
            }

            var reader = new XmlTextReader(filePath);
            Post post = new Post();
            PostMetaData meta = new PostMetaData();

            int processed = 0, dropped = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var postQueue = new List<Post>();
            try
            {
                while (reader.Read())
                {
                    var name = reader.Name;

                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (name == "HackerNews")
                            continue;

                        if (name == "ID")
                            post.Id = reader.ReadElementContentAsInt();

                        else if (name == "ParentID")
                            post.ParentId = reader.ReadElementContentAsInt();
                        else if (name == "Url")
                            post.Url = reader.ReadElementContentAsString();
                        else if (name == "Title")
                            post.Title = reader.ReadElementContentAsString();
                        else if (name == "Text")
                            post.Text = reader.ReadElementContentAsString();
                        else if (name == "Username")
                            meta.Username = reader.ReadElementContentAsString();
                        else if (name == "Points")
                            meta.Points = reader.ReadElementContentAsInt();
                        else if (name == "Type")
                            meta.Type = reader.ReadElementContentAsInt();
                        else if (name == "Timestamp")
                            meta.Created = reader.ReadElementContentAsDateTime();
                        else if (name == "CommentCount")
                            meta.CommentsCount = reader.ReadElementContentAsInt();
                    }

                    if (reader.NodeType == XmlNodeType.EndElement
                        && name == "row")
                    {
                        post.Meta = meta;

                        postQueue.Add(post);
                        if (postQueue.Count() == 1000)
                        {
                            client.IndexAsync<Post>(postQueue, (c) =>
                            {
                                if (!c.Success)
                                    dropped++;
                            });

                            postQueue = new List<Post>();
                            processed++;
                        }

                        Console.Write("\rProcessed:{0}, Dropped:{2} in {1}", processed, sw.Elapsed, dropped);

                        post = new Post();
                        meta = new PostMetaData();
                    }

                }
                sw.Stop();
                Console.WriteLine("\nDone! {0}", sw.Elapsed);
            }
            catch (Exception e)
            {

            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            var connectionSettings = new ConnectionSettings("127.0.0.1.", 9200)
                                        .SetDefaultIndex("mpdreamz");
            var client = new ElasticClient(connectionSettings);
            ConnectionStatus status;
            if (client.TryConnect(out status))
            {
                var version = client.VersionInfo;
                Console.WriteLine("Connected to ElasticSearch {0} released {1}", version.Number, version.Date);
                /*
                var blogAmmount = 100;
                var blogPosts = StaticData.GetBlogPosts(blogAmmount);

                blogPosts.ForEachWithIndex((b,i)=>
                {
                    Console.Write("\rIndexing blog post {0} out of {1}", i + 1, blogAmmount);
                    client.Index(b);
                });
                Console.WriteLine("\nDone indexing {0} items, \nSleeping 3 seconds to catch up with ES Near real time indexing.", blogAmmount);

                Thread.Sleep(3000); //If ES is NRT 3 seconds ought to be sufficient right ?

                Console.WriteLine("Getting blog post 66 as stored in ES..");
                var originalPost = blogPosts[66];
                var post = client.Get<Blog>(66);
                var isEqual = originalPost.Title == post.Title;
                Console.WriteLine("Blog 66 in memory is {0} to blog post 66 as indexed in ES.", (isEqual) ? "equal" : "not equal");

                */
                var post = client.Get<Blog>(66);
                string firstName = post.Author.FirstName;
                string lastName = post.Author.LastName;

                var q = Query<Blog>
                            .Fuzzy(b => b.Author.FirstName, firstName)
                            .AndFuzzy(b => b.Author.LastName, lastName);

                var results = client.Search(q);

                QueryResponse<Blog> queryResults = client.Search<Blog>(new Search()
                {
                    Query = new Query(new Fuzzy("author.firstName", firstName, 1.0))

                }.Skip(0).Take(10)
                );
                var thisPageCount = queryResults.Documents.Count();

                Console.WriteLine("Found {0} document matching author.FirstName:{1}", queryResults.Total, firstName);
                queryResults.Documents.ForEachWithIndex((d, i) =>
                {
                    Console.WriteLine("First page, doc no. {0} entitled {1}", i, d.Title);
                });
            }
            else
            {
                Console.WriteLine("Error connecting to {0} because:\r\n {1}", connectionSettings.Host, status.Error.ExceptionMessage);

            }

            Console.ReadLine();
        }