public virtual void TestBasicFloat() { Directory d = NewDirectory(); RandomIndexWriter w = new RandomIndexWriter(Random(), d, Similarity, TimeZone); Document doc = new Document(); SingleDocValuesField field = new SingleDocValuesField("field", 0.0f); doc.Add(field); for (long l = 0; l < 100; l++) { field.SetSingleValue(l); w.AddDocument(doc); } IndexReader r = w.Reader; FacetsCollector fc = new FacetsCollector(); IndexSearcher s = NewSearcher(r); s.Search(new MatchAllDocsQuery(), fc); Facets facets = new DoubleRangeFacetCounts("field", new SingleFieldSource("field"), fc, new DoubleRange("less than 10", 0.0f, true, 10.0f, false), new DoubleRange("less than or equal to 10", 0.0f, true, 10.0f, true), new DoubleRange("over 90", 90.0f, false, 100.0f, false), new DoubleRange("90 or above", 90.0f, true, 100.0f, false), new DoubleRange("over 1000", 1000.0f, false, double.PositiveInfinity, false)); Assert.AreEqual("dim=field path=[] value=21 childCount=5\n less than 10 (10)\n less than or equal to 10 (11)\n over 90 (9)\n 90 or above (10)\n over 1000 (0)\n", facets.GetTopChildren(10, "field").ToString()); IOUtils.Close(w, r, d); }
/// <summary> /// Adds a new <see cref="SingleDocValuesField"/> field with the specified 32-bit <see cref="float"/> value </summary> /// <param name="document">This <see cref="Document"/>.</param> /// <param name="name"> field name </param> /// <param name="value"> 32-bit <see cref="float"/> value </param> /// <returns>The field that was added to this <see cref="Document"/>.</returns> /// <exception cref="System.ArgumentNullException"> if the field name is <c>null</c> </exception> public static SingleDocValuesField AddSingleDocValuesField(this Document document, string name, float value) { var field = new SingleDocValuesField(name, value); document.Add(field); return(field); }
public void TestAddSingleDocValuesField() { SingleDocValuesField field = null; float value = 123.456f; AssertDocumentExtensionAddsToDocument(document => field = document.AddSingleDocValuesField("theName", value)); Assert.AreEqual("theName", field.Name); Assert.AreEqual(J2N.BitConversion.SingleToRawInt32Bits(value), field.GetSingleValueOrDefault()); }
/// <summary> /// Adds a new <see cref="SingleDocValuesField"/> field with the specified 32-bit <see cref="float"/> value </summary> /// <param name="document">This <see cref="Document"/>.</param> /// <param name="name"> field name </param> /// <param name="value"> 32-bit <see cref="float"/> value </param> /// <returns>The field that was added to this <see cref="Document"/>.</returns> /// <exception cref="ArgumentNullException"> if this <paramref name="document"/> or the field <paramref name="name"/> is <c>null</c> </exception> public static SingleDocValuesField AddSingleDocValuesField(this Document document, string name, float value) { if (document is null) { throw new ArgumentNullException(nameof(document)); } var field = new SingleDocValuesField(name, value); document.Add(field); return(field); }
public virtual void TestSimple() { Directory dir = NewDirectory(); RandomIndexWriter iw = new RandomIndexWriter( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, dir); Document doc = new Document(); Field field = NewTextField("foo", "", Field.Store.NO); doc.Add(field); Field dvField = new SingleDocValuesField("foo_boost", 0.0F); doc.Add(dvField); Field field2 = NewTextField("bar", "", Field.Store.NO); doc.Add(field2); field.SetStringValue("quick brown fox"); field2.SetStringValue("quick brown fox"); dvField.SetSingleValue(2f); // boost x2 iw.AddDocument(doc); field.SetStringValue("jumps over lazy brown dog"); field2.SetStringValue("jumps over lazy brown dog"); dvField.SetSingleValue(4f); // boost x4 iw.AddDocument(doc); IndexReader ir = iw.GetReader(); iw.Dispose(); // no boosting IndexSearcher searcher1 = NewSearcher( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif ir, false); Similarity @base = searcher1.Similarity; // boosting IndexSearcher searcher2 = NewSearcher( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif ir, false); searcher2.Similarity = new PerFieldSimilarityWrapperAnonymousInnerClassHelper(this, field, @base); // in this case, we searched on field "foo". first document should have 2x the score. TermQuery tq = new TermQuery(new Term("foo", "quick")); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, tq, searcher1); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, tq, searcher2); TopDocs noboost = searcher1.Search(tq, 10); TopDocs boost = searcher2.Search(tq, 10); Assert.AreEqual(1, noboost.TotalHits); Assert.AreEqual(1, boost.TotalHits); //System.out.println(searcher2.Explain(tq, boost.ScoreDocs[0].Doc)); Assert.AreEqual(boost.ScoreDocs[0].Score, noboost.ScoreDocs[0].Score * 2f, SCORE_EPSILON); // this query matches only the second document, which should have 4x the score. tq = new TermQuery(new Term("foo", "jumps")); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, tq, searcher1); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, tq, searcher2); noboost = searcher1.Search(tq, 10); boost = searcher2.Search(tq, 10); Assert.AreEqual(1, noboost.TotalHits); Assert.AreEqual(1, boost.TotalHits); Assert.AreEqual(boost.ScoreDocs[0].Score, noboost.ScoreDocs[0].Score * 4f, SCORE_EPSILON); // search on on field bar just for kicks, nothing should happen, since we setup // our sim provider to only use foo_boost for field foo. tq = new TermQuery(new Term("bar", "quick")); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, tq, searcher1); QueryUtils.Check( #if FEATURE_INSTANCE_TESTDATA_INITIALIZATION this, #endif Random, tq, searcher2); noboost = searcher1.Search(tq, 10); boost = searcher2.Search(tq, 10); Assert.AreEqual(1, noboost.TotalHits); Assert.AreEqual(1, boost.TotalHits); Assert.AreEqual(boost.ScoreDocs[0].Score, noboost.ScoreDocs[0].Score, SCORE_EPSILON); ir.Dispose(); dir.Dispose(); }