Search() public method

public Search ( string url ) : ElasticSearch.Client.Domain.SearchResult
url string
return ElasticSearch.Client.Domain.SearchResult
Exemplo n.º 1
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);
		}
Exemplo n.º 2
0
        public void AllInOne()
        {
            string indexType = "v1";

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

            dict.Add("age", 22);
            var result = client.Index(app, indexType, "key1", dict);

            Assert.AreEqual(true, result.Success);

            var indexItem1 = new IndexItem("testKey");

            indexItem1.Add("age", 21);
            result = client.Index(app, indexItem1);
            Assert.AreEqual(true, result.Success);

            var indexItem = new IndexItem(indexType, "key2");

            indexItem.Add("age", 23);
            result = client.Index(app, indexItem);
            Assert.AreEqual(true, result.Success);

            client.Refresh();
            var count = client.Count(app, indexType, ExpressionEx.Eq("age", 25));

            Assert.AreEqual(0, count);

            count = client.Count(app, indexType, Conditional.Get(ExpressionEx.Eq("age", 22)));
            Assert.AreEqual(1, count);
            count = client.Count(app, indexType, Conditional.Get(ExpressionEx.Eq("age", 23)));
            Assert.AreEqual(1, count);

            count = client.Count(app, indexType, Conditional.Get(ExpressionEx.Between("age", 22, 23, true)));
            Assert.AreEqual(2, count);

            //a coplex example
            var cond1 = Conditional.Get(ExpressionEx.Eq("name", "jack"))
                        .And(ExpressionEx.Between("age", 22, 30))
                        .And(ExpressionEx.Fuzzy("address", "beijing", 1.0f, 4))
                        .And(ExpressionEx.Le("no", 87));
            Conditional cond2 = Conditional.Or(cond1, Conditional.Not(ExpressionEx.Eq("gender", "male")));

            client.Search("index", "type", cond2.Query);
        }
Exemplo n.º 3
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

		}
        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);
         }