Пример #1
0
        public void TestCreatingMapping()
        {
            var index = "index_operate" + Guid.NewGuid().ToString();
            StringFieldSetting stringFieldSetting = new StringFieldSetting()
            {
                Analyzer = "standard", Type = "string", NullValue = "mystr"
            };

            TypeSetting typeSetting = new TypeSetting("custom_type");

            typeSetting.AddFieldSetting("medcl", stringFieldSetting);

            var typeSetting2 = new TypeSetting("hell_type1");
            var numfield     = new NumberFieldSetting()
            {
                Store = Store.yes, NullValue = 0.00
            };

            typeSetting2.AddFieldSetting("name", numfield);

            client.CreateIndex(index);
            var result = client.PutMapping(index, typeSetting);

            Assert.AreEqual(true, result.Success);

            result = client.PutMapping(index, typeSetting2);
            Assert.AreEqual(true, result.Success);

            var result2 = client.DeleteIndex(index);

            Assert.AreEqual(true, result2.Success);
        }
Пример #2
0
        public void TestTemplate()
        {
            var tempkey = "test_template_key1";

            client.DeleteTemplate(tempkey);

            var template = new TemplateSetting(tempkey);

            template.Template     = "business_*";
            template.IndexSetting = new TemplateIndexSetting(3, 2);

            var type1 = new TypeSetting("mytype")
            {
            };

            type1.AddNumField("identity", NumType.Float);
            type1.AddDateField("datetime");

            var type2 = new TypeSetting("mypersontype");

            type2.AddStringField("personid");

            type2.SourceSetting         = new SourceSetting();
            type2.SourceSetting.Enabled = false;

            template.AddTypeSetting(type1);
            template.AddTypeSetting(type2);

            var jsonstr = JsonSerializer.Get(template);

            Console.WriteLine(jsonstr);

            var result = client.CreateTemplate(tempkey, template);

            Console.WriteLine(result.JsonString);
            Assert.AreEqual(true, result.Success);

            result = client.CreateIndex("business_111");
            Assert.AreEqual(true, result.Success);
            result = client.CreateIndex("business_132");
            Assert.AreEqual(true, result.Success);
            result = client.CreateIndex("business_31003");
            Assert.AreEqual(true, result.Success);

            client.Refresh();
            var temp = client.GetTemplate(tempkey);

            TemplateSetting result1;

            Assert.AreEqual(true, temp.TryGetValue(tempkey, out result1));
            Assert.AreEqual(template.Order, result1.Order);
            Assert.AreEqual(template.Template, result1.Template);

            client.DeleteIndex("business_111");
            client.DeleteIndex("business_132");
            client.DeleteIndex("business_31003");
        }
Пример #3
0
        public void TestBulkIndexWithParentId()
        {
            var client = new ElasticSearchClient("localhost");

            var fields = new Dictionary <string, object>();

            fields.Add("name", "jack");
            fields.Add("age", 25);
            var index = "index_12312312311";

            try
            {
                client.DeleteIndex(index);
                client.CreateIndex(index);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            var jsondata = JsonSerializer.Get(fields);

            var result = client.Bulk(new List <BulkObject>()
            {
                new BulkObject()
                {
                    Id = "1", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "2", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "3", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                }
            });

            Assert.AreEqual(true, result.Success);
            client.Refresh();
            var c = client.Count(index, "type", "age:25");

            Assert.AreEqual(3, c);
            result = client.Delete(index, "type", new string[] { "1", "2", "3" }, "1");
            Assert.AreEqual(true, result.Success);
            client.Refresh();
            c = client.Count(index, "type", "age:25");
            Assert.AreEqual(0, c);
            client.DeleteIndex(index);
        }
Пример #4
0
        public void SimpleTests()
        {
            var indexName = "myindex_" + Guid.NewGuid();
            var indexType = "type";

            var client = new ElasticSearchClient("localhost");

            var result = client.Index(indexName, indexType, "testkey", "{\"a\":\"b\",\"c\":\"d\"}");

            Assert.AreEqual(true, result.Success);

            client.Refresh(indexName);

            var doc = client.Search(indexName, indexType, "c:d");

            Console.WriteLine(doc.Response);
            Assert.AreEqual(1, doc.GetHits().Hits.Count);
            Assert.AreEqual("b", doc.GetHits().Hits[0].Source["a"]);

            client.Delete(indexName, indexType, "testkey");

            client.Refresh(indexName);

            var doc1 = client.Get(indexName, indexType, "testkey");

            Assert.AreEqual(null, doc1);

            client.DeleteIndex(indexName);
        }
Пример #5
0
        public void TestCreateParentChildType()
        {
            var index = "index_test_parent_child_type";



            var parentType = new TypeSetting("blog");

            parentType.AddStringField("title");

            var client = new ElasticSearchClient("localhost");

            client.CreateIndex(index);

            var op = client.PutMapping(index, parentType);

            Assert.AreEqual(true, op.Acknowledged);

            var childType = new TypeSetting("comment", parentType);

            childType.AddStringField("comments");

            op = client.PutMapping(index, childType);
            Assert.AreEqual(true, op.Acknowledged);

            var mapping = client.GetMapping(index, "comment");

            Assert.True(mapping.IndexOf("_parent") > 0);

            client.DeleteIndex(index);
        }
Пример #6
0
        public void TestBulkForFramdThrift()
        {
            var client = new ElasticSearchClient("localhost");

            var fields = new Dictionary <string, object>();

            fields.Add("name", "jack");
            fields.Add("age", 25);
            var index = "index_bulk_framed";

            try
            {
                client.DeleteIndex(index);
                client.CreateIndex(index);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            var jsondata = JsonSerializer.Get(fields);

            var result = client.Bulk(new List <BulkObject>()
            {
                new BulkObject()
                {
                    Id = "1", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "2", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "3", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "4", Index = index, Type = "type", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "5", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "6", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "7", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
                new BulkObject()
                {
                    Id = "8", Index = index, Type = "type", ParentId = "1", JsonData = jsondata
                },
            });

            Assert.AreEqual(true, result.Success);
        }
Пример #7
0
        static void Main(string[] args)
        {
            var client  = new ElasticSearchClient("10.129.8.58", 9500, TransportType.Thrift);
            int failure = 0;

            var data = "{\"book\": \"The Hitchhiker's Guide to the Galaxy\"" +
                       ",\"id\":" + 1 +
                       ",\"chapter\": \"Chapter 11\",\"text1\": \"All the doors in this spaceship have a cheerful and sunny disposition. It is their pleasure to open for you, and their satisfaction to close again with the knowledge of a job well done.\"}";

            client.Index("benchmark_test", "default", Guid.NewGuid().ToString(), data);

            int count = int.Parse(args[0]);

            var begin = DateTime.Now;

            for (var i = 0; i < count; i++)
            {
                data = "{\"book\": \"The Hitchhiker's Guide to the Galaxy\"" +
                       ",\"id\":" + i +
                       ",\"chapter\": \"Chapter 11\",\"text1\": \"All the doors in this spaceship have a cheerful and sunny disposition. It is their pleasure to open for you, and their satisfaction to close again with the knowledge of a job well done.\"}";
                client.Index("benchmark_test", "default", Guid.NewGuid().ToString(), data);

                var response = client.Index("benchmark_test", "default", Guid.NewGuid().ToString(), data);
                if (!response.Success)
                {
                    failure++;
                }
            }
            var end = DateTime.Now;

            var timespan = end - begin;

            var avg = timespan.TotalMilliseconds / count;

            Console.WriteLine("iteration:{0},failure:{3},elapsed time:{1},avg time:{2}ms", count, timespan, avg, failure);

            client.DeleteIndex("benchmark_test");

            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
Пример #8
0
        public void TestHighlight()
        {
            ElasticSearch.Client.ElasticSearchClient client = new ElasticSearchClient("localhost", 9200, TransportType.Http);


            string indexName = Guid.NewGuid().ToString();


            client.CreateIndex(indexName);


            TypeSetting type = new TypeSetting("type");

            type.AddStringField("title").Analyzer   = "whitespace";
            type.AddStringField("snippet").Analyzer = "whitespace";
            client.PutMapping(indexName, type);

            //index sample
            Dictionary <string, object> dict = new Dictionary <string, object>();

            dict["title"]   = "quick fox jump away";
            dict["snippet"] = "quick fox jump away,where are you?";
            client.Index(indexName, "type", "1", dict);

            dict            = new Dictionary <string, object>();
            dict["title"]   = "fox river is nearby";
            dict["snippet"] = "where is fox river,where is it?";
            client.Index(indexName, "type", "2", dict);


            ElasticQuery query = new ElasticQuery(
                new QueryStringQuery("fox")
                .AddField("title", 5)
                .AddField("snippet", 5), null, 0, 5);

            query.AddHighlightField(new HightlightField("title"));
            query.AddHighlightField(new HightlightField("snippet"));

            client.Refresh(indexName);

            var result = client.Search(indexName, query);

            Console.Out.WriteLine(result.Query);
            Console.Out.WriteLine(result.Response);

            Console.Out.WriteLine("---");
            HitStatus hits = result.GetHits();

            if (hits != null)
            {
                foreach (var o in hits.Hits)
                {
                    foreach (var pair in o.Highlight)
                    {
                        Console.Out.WriteLine(pair.Key + ":");
                        foreach (var field in pair.Value)
                        {
                            Console.Out.WriteLine(field);
                        }

                        Console.Out.WriteLine();
                    }
                }
            }


            client.DeleteIndex(indexName);
        }
Пример #9
0
 public void TestCleanUp()
 {
     client.DeleteIndex("notypIndex");
 }