コード例 #1
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);


		}
コード例 #2
0
		public void TestBulk()
		{
			var type = "bulk";
			List<IndexItem> indexItems = new List<IndexItem>();
			var indexItem = new IndexItem(type, "k1");
			indexItem.Add("Name", "medcl");
			indexItems.Add(indexItem);

			indexItem = new IndexItem(type, "k2");
			indexItem.Add("Name", "netease");
			indexItems.Add(indexItem);

			indexItem = new IndexItem("k3");
			indexItem.Add("Name", "sina");
			indexItems.Add(indexItem);

			var result = client.Index(app, indexItems);
			Assert.AreEqual(true, result.Success);
            
		}
コード例 #3
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",0.7f,4))
                .And(ExpressionEx.Le("no",87));
            Conditional cond2 = Conditional.Or(cond1, Conditional.Not(ExpressionEx.Eq("gender", "male")));
            client.Search(app, "type", cond2.Query);


		}
コード例 #4
0
		public void TestBulkWithDuplicatedFieldName()
		{
			var type = "bulk";
			IList<IndexItem> indexItems = new List<IndexItem>();
			var indexItem = new IndexItem(type, "kk1");
			indexItem.Add("Name", "medcl1");
			indexItem.Add("Name", "medcl2");
			indexItems.Add(indexItem);

			indexItem = new IndexItem(type, "kk2");
			indexItem.Add("Name", "网易");
			indexItem.Add("Name", "163");
			indexItems.Add(indexItem);

			var result = client.Index(app,  indexItems);
			Assert.AreEqual(true, result.Success);
		}
コード例 #5
0
		public void TestIdsQuery()
		{
			var constantScoreQuery = new ConstantScoreQuery(new IdsQuery("type", "1", "2", "3"));
			var result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);

			constantScoreQuery = new ConstantScoreQuery(new IdsQuery("type", "1", "2", "3", "1121"));
			result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);


			var item = new IndexItem("type1", "uk111");
			item.Add("iid", 1);
			client.Index(index, item);
			item = new IndexItem("type1", "dk222");
			item.Add("iid", 2);
			client.Index(index, item);

			constantScoreQuery = new ConstantScoreQuery(new IdsQuery("type", "1", "2", "3", "1121", "uk111"));
			result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);


			//ids can't query corss type
			constantScoreQuery = new ConstantScoreQuery(new IdsQuery(new string[] { "type", "type1" }, "1", "2", "3", "1121", "uk111"));
			result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);

			//waiting for refresh
			Thread.Sleep(1000);

			constantScoreQuery = new ConstantScoreQuery(new IdsQuery(new string[] { "type", "type1" }, "1", "2", "3", "1121", "uk111"));
			result = client.Search(index, null, constantScoreQuery, 0, 5);
			Assert.AreEqual(4, result.GetTotalCount());
			Assert.AreEqual(4, result.GetHits().Hits.Count);


			constantScoreQuery = new ConstantScoreQuery(new IdsQuery(new string[] { "type", "type1" }, "1", "2", "3", "1121", "uk111", "dk222"));
			result = client.Search(index, null, constantScoreQuery, 0, 5);
			Assert.AreEqual(5, result.GetTotalCount());
			Assert.AreEqual(5, result.GetHits().Hits.Count);
		}
コード例 #6
0
	    public void TestTermsQueryWithMinuMatch()
	    {
	        var query = new TermsQuery("gender",2, "true", "false");
	        var result = client.Search(index, "type" , query, 0, 5);
	        Assert.AreEqual(0, result.GetTotalCount());
	        Assert.AreEqual(0, result.GetHits().Hits.Count);
			
			
	        var item = new IndexItem("type","addition_key1");
	        item.Add("name", Guid.NewGuid().ToString());
	        item.Add("id", 2012);
	        item.Add("gender", true);
	        item.Add("gender", false);
	        client.Index(index, item);

	        Thread.Sleep(1000);
	        result = client.Search(index, "type" , query, 0, 5);
	        Assert.AreEqual(1, result.GetTotalCount());
	        Assert.AreEqual(1, result.GetHits().Hits.Count);

	    }
コード例 #7
0
		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());


		}
コード例 #8
0
		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"]);


		}
コード例 #9
0
		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);
		}
コード例 #10
0
		public OperateResult Index(string index, IndexItem indexItem)
		{
			return Index(index, indexItem.IndexType, indexItem.IndexKey, indexItem.FieldsToJson(), indexItem.ParentKey);
		}
コード例 #11
0
		private void button4_Click(object sender, EventArgs e)
		{
			var files = Directory.GetFiles(textBox1.Text, "*.*", SearchOption.AllDirectories);

			foreach (var file in files)
			{
				using(var strs=File.Open(file,FileMode.Open,FileAccess.Read))
				{
					StreamReader str=new StreamReader(strs,Encoding.Default,true);
					var data= str.ReadToEnd();
					var indexItem = new IndexItem("File", Guid.NewGuid().ToString().Replace("-", ""));
					indexItem.Add("Content",data);
					currentElasticSearchInstance.Index(textBox2.Text.ToLower(), indexItem);
				}
			}
		}