Example #1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            FuzzyQuery term = (FuzzyQuery)value;

            if (term != null)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("fuzzy");
                writer.WriteStartObject();

                writer.WritePropertyName(term.Field);
                writer.WriteStartObject();

                writer.WritePropertyName("value");
                writer.WriteValue(term.Value);

                if (!string.IsNullOrEmpty(term.MinSimilarity))
                {
                    writer.WritePropertyName("min_similarity");
                    writer.WriteValue(term.MinSimilarity);
                }

                if (term.PrefixLength > 0)
                {
                    writer.WritePropertyName("prefix_length");
                    writer.WriteValue(term.PrefixLength);
                }

                if (term.Boost > 0)
                {
                    writer.WritePropertyName("boost");
                    writer.WriteValue(term.Boost);
                }

                writer.WriteEndObject();

                writer.WriteEndObject();
                writer.WriteEndObject();
            }
        }
		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());


		}