ElasticSearch.Client license:Apache2 author:medcl url:http://log.medcl.net
示例#1
0
        public void CanScanDiceGames()
        {
            var client = new ElasticSearchClient("ec2-107-22-42-34.compute-1.amazonaws.com", 9500, TransportType.Thrift);

            var startDate = DateTime.UtcNow.AddYears(-10).ToString("o");
            var endDate = DateTime.UtcNow.ToString("o");

            var lastTurnDate = new RangeQuery("LastTurnDate", startDate, endDate, true, true);

            var results = client.Scan<GameDataForList>("dice", new[] {"gameindex"}, lastTurnDate, null, 10, "1m");

            int i = 0;

            foreach (var result in results)
            {
                i++;
                Console.WriteLine("{0}: {1}", i, result.GameId);

                Assert.IsNotNull(result.GameId);

                if(i == 100)
                {
                    return;
                }
            }
        }
        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);
        }
示例#3
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);
		}
		public AnalyzeTest(ElasticSearchClient currentElasticSearchInstance, string indexName)
		{
			InitializeComponent();
			this.currentElasticSearchInstance = currentElasticSearchInstance;
			this.indexName = indexName;
			this.button1.Visible = false;
		}
		public void TestBulk()
		{
			var client = new ElasticSearchClient("localhost");
			var fields = new Dictionary<string, object>();
			fields.Add("name", "jack");
			fields.Add("age", 25);
			
			var index = "index_123123123121";
			try
			{
				client.DeleteIndex(index);
				client.CreateIndex(index);
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
			var result = client.Bulk(new List<BulkObject>()
			                         	{
			                         		new BulkObject() { Id = "1", Index = index, Type = "type", Fields = fields }, 
											new BulkObject() { Id = "2", Index = index, Type = "type", Fields = fields }, 
											new BulkObject() { Id = "3", Index = index, Type = "type", Fields = fields }
			                         	});
			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" });
			Assert.AreEqual(true, result.Success);

			result=client.DeleteIndex(index);
			Assert.AreEqual(true, result.Success);
		}
		public void TestDynamicUserClusters()
		{
			ElasticSearchClient client = new ElasticSearchClient("127.0.0.1", 9200, TransportType.Http);

			List<string> indices= client.GetIndices();

			client=new ElasticSearchClient("127.0.0.1",9500,TransportType.Thrift,true);

			indices= client.GetIndices();
		}
		public PostMapping(ElasticSearchClient currentElasticSearchInstance, string index, string type)
		{
			InitializeComponent();
		    jsonViewer = new JsonViewer();
			jsonViewer.ShowTab(Tabs.Viewer);
			jsonViewer.Dock = DockStyle.Fill;
			panel2.Controls.Add(jsonViewer);

			textBox1.Text = index;
			textBox2.Text = type;
			this.currentElasticSearchInstance = currentElasticSearchInstance;
		}
示例#8
0
        public void CanIndexGetGemsGame()
        {
            var id = Guid.NewGuid().ToString();

            var client = new ElasticSearchClient("ec2-107-22-42-34.compute-1.amazonaws.com", 9500, TransportType.Thrift);
            var data = new Dictionary<string, object> {{"GameId", "asdf"}};

            var bulkObject = new BulkObject("gems","gameindex" , id, data);

            client.Bulk(new List<BulkObject> {bulkObject});

            var loaded = client.Get("gems", "gameindex", id);

            Assert.IsNotNull(loaded);
            Assert.IsNotNull(loaded.GetFields());
            Assert.AreEqual("asdf", loaded.GetFields()["GameId"]);
        }
示例#9
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);
		}
示例#10
0
		private void InitTree(string clusterName, ElasticSearchClient instance)
		{
			var model = new TreeModel();
			if (instance != null)
			{
				var indices = instance.Status();
				if(indices==null){throw new ServerException("can't connect to server");}
				currentCluster = clusterName;
				currentElasticSearchInstance = instance;
				var node = new ElasticNode(clusterName);
				model.Root.Nodes.Add(node);
				var sortedIndices = indices.IndexStatus.OrderBy(d => d.Key);
				foreach (var index in sortedIndices)
				{
					var tempNode = new ElasticNode(string.Format("{0} ({1})", index.Key, index.Value.DocStatus.NumDocs));
					tempNode.ElasticSearchInstance = instance;
					tempNode.Tag = index;
					tempNode.IndexName = index.Key;
					tempNode.IndexStatus = index.Value;
					node.Nodes.Add(tempNode);
				}
			}
			treeViewAdv1.Model = model;
			treeViewAdv1.Refresh();
		}
示例#11
0
		private void connectToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if (toolStripComboBox1.Text != null && !string.IsNullOrEmpty(toolStripComboBox1.Text))
			{
				string clusterName = toolStripComboBox1.Text;
				var srcClient = new ElasticSearchClient(clusterName);

				InitTree(clusterName, srcClient);
			}
		}
示例#12
0
		/// <summary>
		/// reload tree
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void aDDToolStripMenuItem_Click(object sender, EventArgs e)
		{
			
			if (connect.ShowDialog() == DialogResult.OK)
			{
				var srcClient = new ElasticSearchClient(connect.Host, connect.Port, connect.Type);
				var cluster = string.Format("{0}:{1}", connect.Host, connect.Port);
				InitTree(cluster, srcClient);
				//DuplicateCheck();
			}
		}
示例#13
0
		private void IndexTransfer(string index, string toIndex, int from, int limit, int bulkSize, ElasticSearchClient srcClient, ElasticSearchClient descClient, bool complicatedSource,bool resolveTenant,bool showLog)
		{
			var docs = srcClient.Search(index, "*", from, limit, "_id:asc");
			WriteLog("Search:{0},{1},{2}", index, from, limit);
			int i = 0;
			var bulkObjects = new List<BulkObject>();
			
			if(complicatedSource)
			{
				//complicated object
				if (!string.IsNullOrEmpty(docs.Response))
				{
					var obj = JObject.Parse(docs.Response);
					var hits = obj["hits"]["hits"];
					foreach (var hit in hits)
					{
						var source = ((Newtonsoft.Json.Linq.JObject)(hit["_source"])).ToString().Replace("\r\n",string.Empty);// hit["_source"].Value<string>();
						var _type = hit["_type"].Value<string>();
						var _id = hit["_id"].Value<string>();
                        

						i++;
						
						if(showLog)
						{
							WriteLog("curl -XPOST http://localhost:9200/{0}/{1}/{2} -d'{3}'", toIndex, _type, _id, source);
						}

						bulkObjects.Add(new BulkObject(toIndex,_type, _id, source));
						if (i > bulkSize)
						{
							descClient.Bulk(bulkObjects);
							bulkObjects.Clear();
							i = 0;
							WriteLog("Buik Commit.");
						}
					}
				}
			}
			else
			{
			foreach (var variable in docs.GetHits().Hits)
			{
				
				#region logging

				//				WriteLog("\tIndex:{0}", variable.Index);
				//				WriteLog("\tType:{0}", variable.Type);
				//				WriteLog("\tId:{0}", variable.Id);
				Dictionary<string, object> fields = variable.Fields;
				//				WriteLog("\tTotalFieldsCount:{0}", fields.Count);

				#endregion

				#region

				//					                           			foreach (var VARIABLE in fields)
				//					                           			{
				//WriteLog(string.Format("\t\t{0}:{1}", VARIABLE.Key, VARIABLE.Value));
				//					                           			}

				#endregion
				


				#region BulkInsert

				i++;
				bulkObjects.Add(new BulkObject( toIndex, variable.Type,variable.Id, fields));


				if (i > bulkSize)
				{
					descClient.Bulk(bulkObjects);
					bulkObjects.Clear();
					i = 0;
					WriteLog("Buik Commit.");
				}

				#endregion
			}
			}

			#region final cleanup

			if (i > 0)
			{
				descClient.Bulk(bulkObjects);
				WriteLog("Final Cleanup,{0}.", bulkObjects.Count);
				bulkObjects.Clear();
			}

			#endregion

		}
示例#14
0
		private void ExportDataToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Connect connect = new Connect();
			if (connect.ShowDialog() == DialogResult.OK)
			{
				ElasticSearchClient descClient = new ElasticSearchClient(connect.Host, connect.Port, connect.Type);

				Export export = new Export();
				if (export.ShowDialog() == DialogResult.OK)
				{
					var toIndex = export.IndexName;
					var limit = export.LimitSize;
					var buffer = export.BufferSize;
					var bulkSize = export.BulkSize;
					var complicatedSource = export.ComplicatedSource;
					var resolveTenant = export.ResolveTenant;
					var showLog = export.ShowLog;
				    var skipCount = export.SkipCount;

					foreach (var selectedNode in treeViewAdv1.SelectedNodes)
					{
						var elasticNode = ((ElasticNode)(selectedNode.Tag));
						var index = elasticNode.IndexName;
						WriteLog("Export for: {0}", selectedNode.Tag);

						int bufferSize = buffer;
						int limitSize = limit;
						new Thread(new ThreadStart(delegate()
													{
                                                        try
                                                        {

                                                            var start = DateTime.Now;
                                                            var total = elasticNode.ElasticSearchInstance.Count(index,
                                                                                                                "*");
                                                            if (limitSize > total) limitSize = total;
                                                            if (bufferSize > limitSize) bufferSize = limitSize;
                                                            WriteLog(
                                                                "Transform Index : {0} To Index : {1},{2} Pending Docs",
                                                                index, toIndex, limitSize);

                                                            for (int i = skipCount; i < limitSize; i += bufferSize)
                                                            {
                                                                IndexTransfer(index, toIndex, i, bufferSize, bulkSize,
                                                                              elasticNode.ElasticSearchInstance,
                                                                              descClient, complicatedSource,
                                                                              resolveTenant, showLog);
                                                            }


                                                            WriteLog(
                                                                "Index : {0} Transform Finished,Time Elapsed : {1}",
                                                                index,
                                                                DateTime.Now - start);
                                                        }catch(Exception ex)
                                                        {
                                                            //todo
                                                        }

													})).Start();

					}
				}
			}

		}
		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);
		}
示例#16
0
 public void Setup()
 {
     client = new ElasticSearchClient("localhost");
 }
		public void Setup()
		{
			client=new ElasticSearchClient("localhost");
		}
        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);
         }