示例#1
0
        public void TestCreateAnalysisMapping()
        {
            var template = new TemplateSetting("index_1*");
            var type     = new TypeSetting("type88");

            type.AddStringField("name", null, Store.no, IndexType.analyzed, TermVector.no, 1, null, true, true, "keyword", "keyword", "keyword");
            template.AddTypeSetting(type);
            var result = client.CreateTemplate("template_123", template).Success;

            Assert.AreEqual(true, result);
            result = client.CreateIndex("index_1123").Success;
            Assert.AreEqual(true, result);
            var a = client.Index("index_1123", "type88", "key1", "{name:\"张三\"}");

            Assert.AreEqual(true, a.Success);
            a = client.Index("index_1123", "type88", "key2", "{name:\"张三丰\"}");
            Assert.AreEqual(true, a.Success);
            a = client.Index("index_1123", "type88", "key3", "{name:\"大张旗鼓\"}");
            Assert.AreEqual(true, a.Success);
            a = client.Index("index_1123", "type88", "key4", "{name:\"子张怡\"}");
            Assert.AreEqual(true, a.Success);
            Thread.Sleep(2000);
            var count = client.Count("index_1123", "type88", "name:张");

            Assert.AreEqual(0, count);
            count = client.Count("index_1123", "type88", "name:张三");
            Assert.AreEqual(1, count);
            count = client.Count("index_1123", "type88", "name:张三丰");
            Assert.AreEqual(1, count);
            count = client.Count("index_1123", "type88", "name:张三*");
            Assert.AreEqual(2, count);
            count = client.Count("index_1123", "type88", "name:张三*");
            Assert.AreEqual(2, count);
            count = client.Count("index_1123", "type88", "name:张三?");
            Assert.AreEqual(1, count);
            count = client.Count("index_1123", "type88", "name:*张*");
            Assert.AreEqual(4, count);
            count = client.Count("index_1123", "type88", "name:?张*");
            Assert.AreEqual(2, count);

            //test for _all
            //only works after setting default_analysis to keyword(or set against filed: _all )
//			count = client.Count("index_1123", "type88", "_all:张");
//			Assert.AreEqual(0, count);
//			count = client.Count("index_1123", "type88", "_all:张三");
//			Assert.AreEqual(1, count);
//			count = client.Count("index_1123", "type88", "_all:张三丰");
//			Assert.AreEqual(1, count);
//			count = client.Count("index_1123", "type88", "_all:张三*");
//			Assert.AreEqual(2, count);
//			count = client.Count("index_1123", "type88", "_all:张三*");
//			Assert.AreEqual(2, count);
//			count = client.Count("index_1123", "type88", "_all:张三?");
//			Assert.AreEqual(1, count);
//			count = client.Count("index_1123", "type88", "_all:*张*");
//			Assert.AreEqual(4, count);
//			count = client.Count("index_1123", "type88", "_all:?张*");
//			Assert.AreEqual(2, count);
        }
示例#2
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);
        }
示例#3
0
        private void forEachUpdateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var tempNode = (ElasticNode)treeViewAdv1.SelectedNode.Tag;

            if (tempNode != null)
            {
                var start = DateTime.Now;

                var      size = 500;
                string   type = "bc8c7a80-c2fc-4d62-9de3-ec6b9f59ba95";
                GetInput g    = new GetInput(type, "Type");
                if (g.ShowDialog() == DialogResult.OK)
                {
                    type = g.Input;
                }

                string query = "*";
                g = new GetInput(query, "Query");
                if (g.ShowDialog() == DialogResult.OK)
                {
                    query = g.Input;
                }

                var total = currentElasticSearchInstance.Count(tempNode.IndexName, query);

                string sort = "__TIME";
                g = new GetInput(sort, "SortBy");
                if (g.ShowDialog() == DialogResult.OK)
                {
                    sort = g.Input;
                }
                string content = "{\"visibleType\":\"0\"}";

                g = new GetInput(content, "update content");
                if (g.ShowDialog() == DialogResult.OK)
                {
                    content = g.Input;
                }

                int hit = 0;
                for (int i = 0; i < total; i += size)
                {
                    WriteLog("Index From:{0},Size:{1}".Fill(i, size));

                    SearchResult docs = currentElasticSearchInstance.Search(tempNode.IndexName, type,
                                                                            new QueryStringQuery(query), new SortItem(sort, SortType.Asc), i, size);
                    WriteLog("QueryResult:{0} // {1}".Fill(docs.GetHitIds().Count, docs.GetTotalCount()));
                    List <string> ids = docs.GetHitIds();
                    foreach (var id in ids)
                    {
                        var result = currentElasticSearchInstance.PartialUpdate(tempNode.IndexName, type, id, content);
                        if (result)
                        {
                            hit += 1;
                        }
                        else
                        {
                            WriteLog("Index Failed:{0}".Fill(id));
                        }
                    }
                }
                var end = DateTime.Now;
                WriteLog("Index:{0} Updating Finished.total {1} updated,time:{2},{3} ".Fill(tempNode.IndexName, hit, end - start, hit));
            }
        }