public void TestCreateUser()
        {
            var template_key = "test_user_template";
            client.DeleteTemplate(template_key);

            var template = new TemplateSetting(template_key);
            template.Template = "test_user*";
            template.IndexSetting = new TemplateIndexSetting(1, 1);
            var type = new TypeSetting("Staff");
            type.AddStringField("Name");
            type.AddStringField("Email");
            type.AddStringField("EnglishName");

            type.AddNumField("UserID", NumType.Integer);
            template.AddTypeSetting(type);

            var jsonstr = JsonSerializer.Get(template);
            Console.WriteLine(jsonstr);

            var result = client.CreateTemplate(template_key, template);
            Console.WriteLine(result.JsonString);
            Assert.AreEqual(true, result.Success);

            var temp2= client.GetTemplate(template_key);
            Assert.AreEqual("test_user*", temp2[template_key].Template);
            Assert.True(temp2[template_key].Mappings.ContainsKey("Staff"));

            Assert.AreEqual(1, temp2[template_key].IndexSetting.NumberOfReplicas);
            Assert.AreEqual(1, temp2[template_key].IndexSetting.NumberOfShards);

            result= client.DeleteTemplate(template_key);
            Assert.AreEqual(true,result.Success);
        }
        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);
        }
		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");
		}
        public void TestCreatingMapping()
        {
            var index = "index_operate" + Guid.NewGuid();
            var stringFieldSetting = new StringFieldSetting { Analyzer = "standard", Type = "string", NullValue = "mystr" };

            var 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);
        }
Exemple #5
0
 public TypeSetting(string typeNameName, TypeSetting parentType)
 {
     TypeName      = typeNameName;
     ParentSetting = new ParentSetting(parentType.TypeName);
 }
		public void TestFuzzLikeTextQuery()
		{
			string textType = "text_flt";
			var typeSetting = new TypeSetting(textType);
			typeSetting.AddStringField("message").Analyzer="standard";
			client.PutMapping(index, typeSetting);
			client.Refresh();

			var dict = new Dictionary<string, object>();
			dict["message"] = "the quick brown fox jumped over thelazy dog";
			var op= client.Index(index, textType, "text_k1", dict);
			Assert.True(op.Success);
			client.Refresh();

			var flt = new FuzzyLikeThisQuery("message", "lazy dob");

			var result= client.Search(index, textType, flt);

			Assert.AreEqual(1,result.GetTotalCount());

			flt = new FuzzyLikeThisQuery("message", "lazy cat");

			result = client.Search(index, textType, flt);

			Assert.AreEqual(0, result.GetTotalCount());
		}
		public void TestTopChildrenQuery()
		{
			var index = "index_test_parent_child_type123_with_top_child_query_query";
//		    client.DeleteIndex(index);

			#region preparing mapping

			var parentType = new TypeSetting("blog");
			parentType.AddStringField("title");
			client.CreateIndex(index);
			var op = client.PutMapping(index, parentType);

			Assert.AreEqual(true, op.Acknowledged);

			var childType = new TypeSetting("comment", parentType);
			childType.AddStringField("comments");
		    childType.AddStringField("author").Analyzer = "keyword";

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

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

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

			client.Refresh();

			#endregion


			#region preparing test data

			Dictionary<string, object> dict = new Dictionary<string, object>();
			dict["title"] = "this is the blog title";
			client.Index(index, "blog", "1", dict);

			dict = new Dictionary<string, object>();
			dict["title"] = "hello.elasticsearch";
			client.Index(index, "blog", "2", dict);

			//child docs
			dict = new Dictionary<string, object>();
			dict["title"] = "awful,that's bullshit";
			dict["author"] = "lol";
			client.Index(index, "comment", "c1", dict, "1");

			dict = new Dictionary<string, object>();
			dict["title"] = "hey,lol,i can't agree more!";
			dict["author"] = "laday-guagua";
			client.Index(index, "comment", "c2", dict, "1");

			dict = new Dictionary<string, object>();
			dict["title"] = "it rocks";
			dict["author"] = "laday-guagua";
			client.Index(index, "comment", "c3", dict, "2");
			#endregion

			client.Refresh();

			var q = new ConstantScoreQuery(new TopChildrenQuery("comment", new TermQuery("author", "lol")));
			var result = client.Search(index, "blog" , q, 0, 5);

			Assert.AreEqual(1, result.GetTotalCount());
			Assert.AreEqual("1", result.GetHitIds()[0]);


			q = new ConstantScoreQuery(new TopChildrenQuery("comment", new TermQuery("author", "laday-guagua")));
			result = client.Search(index, "blog" , q, 0, 5);

			Assert.AreEqual(2, result.GetTotalCount());

			client.DeleteIndex(index);
		}
		public void TestFuzzQuery()
		{

			string texType = "doc_fuzzy";

			var typeSetting = new TypeSetting(texType);
			typeSetting.AddStringField("f").Analyzer = "standard";
		var op=	client.PutMapping(index, typeSetting);
			Assert.True(op.Success);
			client.Refresh();

			var doc = new IndexItem(texType,"1");
			doc.Add("f","aaaaa");
			op=client.Index(index, doc);
			Assert.True(op.Success);
			doc = new IndexItem(texType,"2");
			doc.Add("f", "aaaab");
			op = client.Index(index, doc);
			Assert.True(op.Success);
			doc = new IndexItem(texType, "3");
			doc.Add("f", "aaabb");
			op = client.Index(index, doc);
			Assert.True(op.Success);
			doc = new IndexItem(texType, "4");
			doc.Add("f", "aabbb");
			op = client.Index(index, doc);
			Assert.True(op.Success);
			doc = new IndexItem(texType, "5");
			doc.Add("f", "abbbb");
			op = client.Index(index, doc);
			Assert.True(op.Success);
			doc = new IndexItem(texType, "6");
			doc.Add("f", "bbbbb");
			op = client.Index(index, doc);
			Assert.True(op.Success);
			doc = new IndexItem(texType, "7");
			doc.Add("f", "ddddd");
			op = client.Index(index, doc);
			Assert.True(op.Success);

			client.Refresh();

			var fuzzyQ = new FuzzyQuery("f", "aaaaa");
			var result=client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(3,result.GetTotalCount());
			fuzzyQ = new FuzzyQuery("f", "aaaaa");
			fuzzyQ.PrefixLength = 1;
			result = client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(3, result.GetTotalCount());
			fuzzyQ = new FuzzyQuery("f", "aaaaa");
			fuzzyQ.PrefixLength = 2;
			result = client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(3, result.GetTotalCount());
			fuzzyQ = new FuzzyQuery("f", "aaaaa");
			fuzzyQ.PrefixLength = 3;
			result = client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(3, result.GetTotalCount());
			fuzzyQ = new FuzzyQuery("f", "aaaaa");
			fuzzyQ.PrefixLength = 4;
			result = client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(2, result.GetTotalCount());
			fuzzyQ = new FuzzyQuery("f", "aaaaa");
			fuzzyQ.PrefixLength = 5;
			result = client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(1, result.GetTotalCount());
			fuzzyQ = new FuzzyQuery("f", "aaaaa");
			fuzzyQ.PrefixLength = 6;
			result = client.Search(index, texType, fuzzyQ);
			Assert.AreEqual(1, result.GetTotalCount());


		}
		public void TestDisMaxQuery()
		{
			string textType = "dismax";
			var typeSetting = new TypeSetting(textType);
			// hed is the most important field, dek is secondary
			typeSetting.AddStringField("hed").Analyzer = "standard";
			typeSetting.AddStringField("dek").Analyzer = "standard";
			client.PutMapping(index, typeSetting);
			client.Refresh();

			// d1 is an "ok" match for:  albino elephant
			var 
			dict = new Dictionary<string, object>();
			dict["id"] = "d1";
			dict["hed"] = "elephant";
			dict["dek"] = "elephant";
			var op = client.Index(index, textType, "d1", dict);
			Assert.True(op.Success);

			// d2 is a "good" match for:  albino elephant
			IndexItem 
			item = new IndexItem(textType, "d2");
			item.Add("id", "d2");
			item.Add("hed", "elephant");
			item.Add("dek", "albino");
			item.Add("dek", "elephant");
			op = client.Index(index,  item);
			Assert.True(op.Success);

			//d3 is a "better" match for:  albino elephant
			item = new IndexItem(textType, "d3");
			item.Add("id", "d3");
			item.Add("hed", "albino");
			item.Add("hed", "elephant");
			op = client.Index(index, item);
			Assert.True(op.Success);

			// d4 is the "best" match for:  albino elephant
			item = new IndexItem(textType, "d4");
			item.Add("id", "d4");
			item.Add("hed", "albino");
			item.Add("hed", "elephant");
			item.Add("dek", "albino");
			op = client.Index(index, item);
			Assert.True(op.Success);


			client.Refresh();


			var dismaxQuery = new DisjunctionMaxQuery(0.0f);
			dismaxQuery.AddQuery(new TermQuery("hed", "albino"));
			dismaxQuery.AddQuery(new TermQuery("hed", "elephant"));
			
			var result = client.Search(index, textType, dismaxQuery);
			Console.WriteLine("all docs should match");
			Assert.AreEqual(4,result.GetTotalCount());
			foreach (var o in result.GetHits().Hits)
			{
				Console.WriteLine(o.ToString());
			}

			dismaxQuery = new DisjunctionMaxQuery(0.0f);
			dismaxQuery.AddQuery(new TermQuery("dek", "albino"));
			dismaxQuery.AddQuery(new TermQuery("dek", "elephant"));

			result = client.Search(index, textType, dismaxQuery);
			Console.WriteLine("3 docs should match");
			Assert.AreEqual(3, result.GetTotalCount());
			foreach (var o in result.GetHits().Hits)
			{
				Console.WriteLine(o.ToString());
			}
			
			dismaxQuery = new DisjunctionMaxQuery(0.0f);
			dismaxQuery.AddQuery(new TermQuery("dek", "albino"));
			dismaxQuery.AddQuery(new TermQuery("dek", "elephant"));
			dismaxQuery.AddQuery(new TermQuery("hed", "albino"));
			dismaxQuery.AddQuery(new TermQuery("hed", "elephant"));

			result = client.Search(index, textType, dismaxQuery);
			Console.WriteLine("all docs should match");
			Assert.AreEqual(4, result.GetTotalCount());
			foreach (var o in result.GetHits().Hits)
			{
				Console.WriteLine(o.ToString());
			}


			dismaxQuery = new DisjunctionMaxQuery(0.01f);
			dismaxQuery.AddQuery(new TermQuery("dek", "albino"));
			dismaxQuery.AddQuery(new TermQuery("dek", "elephant"));

			result = client.Search(index, textType, dismaxQuery);
			Console.WriteLine("3 docs should match");
			float score0 = Convert.ToSingle(result.GetHits().Hits[0].Score);
			float score1 = Convert.ToSingle(result.GetHits().Hits[1].Score);
			float score2 = Convert.ToSingle(result.GetHits().Hits[2].Score);

			foreach (var o in result.GetHits().Hits)
			{
				Console.WriteLine(o.ToString());
			}

			Assert.IsTrue(score0 > score1);
			Assert.AreEqual(score1, score2);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual("d2", result.GetHits().Hits[0].Source["id"]);


		}
		public void TestTextQuery()
		{
			string textType = "text";
			var typeSetting = new TypeSetting(textType);
			typeSetting.AddStringField("message").Analyzer="whitespace";
			client.PutMapping(index, typeSetting);
			client.Refresh();

			var dict = new Dictionary<string, object>();
			dict["message"] = "the quick brown fox jumped over thelazy dog";
			var op= client.Index(index, textType, "text_k1", dict);
			Assert.True(op.Success);
			client.Refresh();
			var textQuery = new TextQuery("message", "fox");
			var result= client.Search(index, textType, textQuery);

			Assert.AreEqual(1,result.GetTotalCount());
		}
		public void Init()
		{
			var typesetting = new TypeSetting("type");
			typesetting.AddFieldSetting("name", new StringFieldSetting() { Index = IndexType.not_analyzed });
			typesetting.AddFieldSetting("id", new NumberFieldSetting() { });
			typesetting.AddFieldSetting("gender", new BooleanFieldSetting() { Index = IndexType.not_analyzed });

			client.CreateIndex(index);
			client.PutMapping(index, typesetting);

			IndexItem item;
			for (int i = 0; i < 100; i++)
			{
				item = new IndexItem("type", i.ToString());
				item.Add("name", Guid.NewGuid().ToString());
				item.Add("id", i);
				item.Add("ids","ids_{0}".Fill(i));
				if (i >= 50)
				{
					item.Add("gender", true);
				}
				else
				{
					item.Add("gender", false);
				}
	
			
			client.Index(index,item);
			}


			item = new IndexItem("type", "addition_key2");
			item.Add("name", "张");
			item.Add("age",25);
			item.Add("type","common");
			client.Index(index, item);

			item = new IndexItem("type", "addition_key3");
			item.Add("name", "张三");
			item.Add("age", 24);
			item.Add("type", "common");
			client.Index(index, item);

			item = new IndexItem("type", "addition_key4");
			item.Add("name", "张三丰");
			item.Add("age", 23);
			item.Add("type", "common");
			client.Index(index, item);

			item = new IndexItem("type", "addition_key5");
			item.Add("name", "二张三张");
			item.Add("age", 22);
			item.Add("type", "common2");
			client.Index(index, item);

			Thread.Sleep(1000);
		}
		public OperateResult PutMapping(string index, TypeSetting typeSetting)
		{
			Contract.Assert(!string.IsNullOrEmpty(index));
			Contract.Assert(typeSetting != null);
			string url = "/{0}/{1}/_mapping".Fill(index.ToLower(), typeSetting.TypeName);

			var mappings = new Dictionary<string, TypeSetting>();
			mappings.Add(typeSetting.TypeName, typeSetting);

			string data = JsonSerializer.Get(mappings);

			RestResponse response = _provider.Put(url, data);

			if (response != null)
			{
				try
				{
					if (response.Status == Transport.IDL.Status.INTERNAL_SERVER_ERROR ||
					    response.Status == Transport.IDL.Status.BAD_REQUEST)
					{
						//auto create index
						CreateIndex(index, new IndexSetting(5, 1));
						//try again
						response = _provider.Put(url, data);

						return GetOperationResult(response);
					}
				}
				catch (System.Exception e)
				{
					_logger.Error(e);
				}
			}
			return GetOperationResult(response);
		}
        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);
         }
        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);
        }
 public TypeSetting(string typeNameName, TypeSetting parentType)
 {
     TypeName = typeNameName;
     ParentSetting = new ParentSetting(parentType.TypeName);
 }  
		public void AddTypeSetting(TypeSetting typeSetting)
		{
			if (Mappings == null) { Mappings = new Dictionary<string, TypeSetting>(); }
			Mappings[typeSetting.TypeName] = typeSetting;
		}