public virtual void TestBasic()
		{
			// create a sort field and sort by it (reverse order)
			Query query = new TermQuery(new Term("body", "contents"));
			IndexReader r = searcher.IndexReader;
			// Just first pass query
			TopDocs hits = searcher.Search(query, 10);
			AreEqual(3, hits.TotalHits);
			AreEqual("3", r.Document(hits.ScoreDocs[0].Doc).Get("id"));
			AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id"));
			AreEqual("2", r.Document(hits.ScoreDocs[2].Doc).Get("id"));
			// Now, rescore:
			Expression e = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
			SimpleBindings bindings = new SimpleBindings();
			bindings.Add(new SortField("popularity", SortField.Type_e.INT));
			bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
			Rescorer rescorer = e.GetRescorer(bindings);
			hits = rescorer.Rescore(searcher, hits, 10);
			AreEqual(3, hits.TotalHits);
			AreEqual("2", r.Document(hits.ScoreDocs[0].Doc).Get("id"));
			AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id"));
			AreEqual("3", r.Document(hits.ScoreDocs[2].Doc).Get("id"));
			string expl = rescorer.Explain(searcher, searcher.Explain(query, hits.ScoreDocs[0].Doc), hits.ScoreDocs[0].Doc).ToString();
			// Confirm the explanation breaks out the individual
			// variables:
			IsTrue(expl.Contains("= variable \"popularity\""));
			// Confirm the explanation includes first pass details:
			IsTrue(expl.Contains("= first pass score"));
			IsTrue(expl.Contains("body:contents in"));
		}
Example #2
0
        public virtual void TestRangeScorer()
        {
            Expression     expr     = JavascriptCompiler.Compile("2*popularity");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("popularity", SortFieldType.INT64));
            ValueSource vs = expr.GetValueSource(bindings);

            Assert.AreEqual(1, reader.Leaves.Count);
            AtomicReaderContext leaf   = reader.Leaves[0];
            FunctionValues      values = vs.GetValues(new Dictionary <string, object>(), leaf);
            // everything
            ValueSourceScorer scorer = values.GetRangeScorer(leaf.Reader, "4"
                                                             , "40", true, true);

            Assert.AreEqual(-1, scorer.DocID);
            Assert.AreEqual(0, scorer.NextDoc());
            Assert.AreEqual(1, scorer.NextDoc());
            Assert.AreEqual(2, scorer.NextDoc());
            Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, scorer.NextDoc());
            // just the first doc
            scorer = values.GetRangeScorer(leaf.Reader, "4", "40", false, false);
            Assert.AreEqual(-1, scorer.DocID);
            Assert.AreEqual(0, scorer.NextDoc());
            Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, scorer.NextDoc());
        }
Example #3
0
        // TODO: ideally we'd test > Short.MAX_VALUE too, but compilation is currently recursive.
        // so if we want to test such huge expressions, we need to instead change parser to use an explicit Stack
        /// <exception cref="System.Exception"></exception>
        private void DoTestLotsOfBindings(int n)
        {
            SimpleBindings bindings = new SimpleBindings();
            StringBuilder  sb       = new StringBuilder();

            for (int i = 0; i < n; i++)
            {
                if (i > 0)
                {
                    sb.Append("+");
                }
                sb.Append("x" + i);
                bindings.Add(new SortField("x" + i, SortFieldType.SCORE));
            }
            var          expr  = JavascriptCompiler.Compile(sb.ToString());
            var          sort  = new Sort(expr.GetSortField(bindings, true));
            Query        query = new TermQuery(new Term("body", "contents"));
            TopFieldDocs td    = searcher.Search(query, null, 3, sort, true, true);

            for (int i_1 = 0; i_1 < 3; i_1++)
            {
                FieldDoc d        = (FieldDoc)td.ScoreDocs[i_1];
                float    expected = n * d.Score;
                float    actual   = (float)((double)d.Fields[0]);
                AreEqual(expected, actual, CheckHits.ExplainToleranceDelta(expected, actual));
            }
        }
Example #4
0
        public virtual void TestBasic()
        {
            // create a sort field and sort by it (reverse order)
            Query       query = new TermQuery(new Term("body", "contents"));
            IndexReader r     = searcher.IndexReader;
            // Just first pass query
            TopDocs hits = searcher.Search(query, 10);

            AreEqual(3, hits.TotalHits);
            AreEqual("3", r.Document(hits.ScoreDocs[0].Doc).Get("id"));
            AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id"));
            AreEqual("2", r.Document(hits.ScoreDocs[2].Doc).Get("id"));
            // Now, rescore:
            Expression     e        = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("popularity", SortFieldType.INT32));
            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            Rescorer rescorer = e.GetRescorer(bindings);

            hits = rescorer.Rescore(searcher, hits, 10);
            AreEqual(3, hits.TotalHits);
            AreEqual("2", r.Document(hits.ScoreDocs[0].Doc).Get("id"));
            AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id"));
            AreEqual("3", r.Document(hits.ScoreDocs[2].Doc).Get("id"));
            string expl = rescorer.Explain(searcher, searcher.Explain(query, hits.ScoreDocs[0].Doc), hits.ScoreDocs[0].Doc).ToString();

            // Confirm the explanation breaks out the individual
            // variables:
            IsTrue(expl.Contains("= variable \"popularity\""));
            // Confirm the explanation includes first pass details:
            IsTrue(expl.Contains("= first pass score"));
            IsTrue(expl.Contains("body:contents in"));
        }
 public virtual void TestEquals()
 {
     Expression expr = JavascriptCompiler.Compile("sqrt(a) + ln(b)");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("a", SortField.Type_e.INT));
     bindings.Add(new SortField("b", SortField.Type_e.INT));
     ValueSource vs1 = expr.GetValueSource(bindings);
     // same instance
     AreEqual(vs1, vs1);
     // null
     IsFalse(vs1.Equals(null));
     // other object
     IsFalse(vs1.Equals("foobar"));
     // same bindings and expression instances
     ValueSource vs2 = expr.GetValueSource(bindings);
     AreEqual(vs1.GetHashCode(), vs2.GetHashCode());
     AreEqual(vs1, vs2);
     // equiv bindings (different instance)
     SimpleBindings bindings2 = new SimpleBindings();
     bindings2.Add(new SortField("a", SortField.Type_e.INT));
     bindings2.Add(new SortField("b", SortField.Type_e.INT));
     ValueSource vs3 = expr.GetValueSource(bindings2);
     AreEqual(vs1, vs3);
     // different bindings (same names, different types)
     SimpleBindings bindings3 = new SimpleBindings();
     bindings3.Add(new SortField("a", SortField.Type_e.LONG));
     bindings3.Add(new SortField("b", SortField.Type_e.INT));
     ValueSource vs4 = expr.GetValueSource(bindings3);
     IsFalse(vs1.Equals(vs4));
 }
 public virtual void TestEquals()
 {
     Expression expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
     bindings.Add(new SortField("popularity", SortField.Type_e.INT));
     SimpleBindings otherBindings = new SimpleBindings();
     otherBindings.Add(new SortField("_score", SortField.Type_e.LONG));
     otherBindings.Add(new SortField("popularity", SortField.Type_e.INT));
     SortField sf1 = expr.GetSortField(bindings, true);
     // different order
     SortField sf2 = expr.GetSortField(bindings, false);
     IsFalse(sf1.Equals(sf2));
     // different bindings
     sf2 = expr.GetSortField(otherBindings, true);
     IsFalse(sf1.Equals(sf2));
     // different expression
     Expression other = JavascriptCompiler.Compile("popularity/2");
     sf2 = other.GetSortField(bindings, true);
     IsFalse(sf1.Equals(sf2));
     // null
     IsFalse(sf1.Equals(null));
     // same instance:
     AreEqual(sf1, sf1);
 }
Example #7
0
        public virtual void TestEquals()
        {
            Expression     expr     = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            bindings.Add(new SortField("popularity", SortFieldType.INT32));
            SimpleBindings otherBindings = new SimpleBindings();

            otherBindings.Add(new SortField("_score", SortFieldType.INT64));
            otherBindings.Add(new SortField("popularity", SortFieldType.INT32));
            SortField sf1 = expr.GetSortField(bindings, true);
            // different order
            SortField sf2 = expr.GetSortField(bindings, false);

            Assert.IsFalse(sf1.Equals(sf2));
            // different bindings
            sf2 = expr.GetSortField(otherBindings, true);
            Assert.IsFalse(sf1.Equals(sf2));
            // different expression
            Expression other = JavascriptCompiler.Compile("popularity/2");

            sf2 = other.GetSortField(bindings, true);
            Assert.IsFalse(sf1.Equals(sf2));
            // null
            Assert.IsFalse(sf1.Equals(null));
            // same instance:
            Assert.AreEqual(sf1, sf1);
        }
Example #8
0
        public virtual void TestToString()
        {
            Expression     expr     = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            bindings.Add(new SortField("popularity", SortFieldType.INT32));
            SortField sf = expr.GetSortField(bindings, true);

            Assert.AreEqual("<expr \"sqrt(_score) + ln(popularity)\">!", sf.ToString());
        }
 public virtual void Test()
 {
     // compile an expression:
     var expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
     // we use SimpleBindings: which just maps variables to SortField instances
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
     bindings.Add(new SortField("popularity", SortField.Type_e.INT));
     // create a sort field and sort by it (reverse order)
     Sort sort = new Sort(expr.GetSortField(bindings, true));
     Query query = new TermQuery(new Term("body", "contents"));
     searcher.Search(query, null, 3, sort);
 }
 public virtual void TestCoRecursion()
 {
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add("cycle0", JavascriptCompiler.Compile("cycle1"));
     bindings.Add("cycle1", JavascriptCompiler.Compile("cycle0"));
     try
     {
         bindings.Validate();
         Fail("didn't get expected exception");
     }
     catch (ArgumentException expected)
     {
         IsTrue(expected.Message.Contains("Cycle detected"));
     }
 }
Example #11
0
        public virtual void Test()
        {
            // compile an expression:
            var expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
            // we use SimpleBindings: which just maps variables to SortField instances
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            bindings.Add(new SortField("popularity", SortFieldType.INT32));
            // create a sort field and sort by it (reverse order)
            Sort  sort  = new Sort(expr.GetSortField(bindings, true));
            Query query = new TermQuery(new Term("body", "contents"));

            searcher.Search(query, null, 3, sort);
        }
Example #12
0
        public virtual void TestSelfRecursion()
        {
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add("cycle0", JavascriptCompiler.Compile("cycle0"));
            try
            {
                bindings.Validate();
                Assert.Fail("didn't get expected exception");
            }
            catch (ArgumentException expected)
            {
                Assert.IsTrue(expected.Message.Contains("Cycle detected"));
            }
        }
Example #13
0
        public virtual void TestValidExternals()
        {
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("valid0", SortFieldType.INT32));
            bindings.Add(new SortField("valid1", SortFieldType.INT32));
            bindings.Add(new SortField("valid2", SortFieldType.INT32));
            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            bindings.Add("valide0", JavascriptCompiler.Compile("valid0 - valid1 + valid2 + _score"
                                                               ));
            bindings.Validate();
            bindings.Add("valide1", JavascriptCompiler.Compile("valide0 + valid0"));
            bindings.Validate();
            bindings.Add("valide2", JavascriptCompiler.Compile("valide0 * valide1"));
            bindings.Validate();
        }
Example #14
0
        public virtual void TestInvalidExternal2()
        {
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("valid", SortFieldType.INT32));
            bindings.Add("invalid", JavascriptCompiler.Compile("valid + badreference"));
            try
            {
                bindings.Validate();
                Assert.Fail("didn't get expected exception");
            }
            catch (ArgumentException expected)
            {
                Assert.IsTrue(expected.Message.Contains("Invalid reference"));
            }
        }
Example #15
0
        public virtual void TestNeedsScores()
        {
            SimpleBindings bindings = new SimpleBindings();
            // refers to score directly
            Expression exprA = JavascriptCompiler.Compile("_score");
            // constant
            Expression exprB = JavascriptCompiler.Compile("0");
            // field
            Expression exprC = JavascriptCompiler.Compile("intfield");

            // score + constant
            Expression exprD = JavascriptCompiler.Compile("_score + 0");
            // field + constant
            Expression exprE = JavascriptCompiler.Compile("intfield + 0");

            // expression + constant (score ref'd)
            Expression exprF = JavascriptCompiler.Compile("a + 0");
            // expression + constant
            Expression exprG = JavascriptCompiler.Compile("e + 0");

            // several variables (score ref'd)
            Expression exprH = JavascriptCompiler.Compile("b / c + e * g - sqrt(f)");
            // several variables
            Expression exprI = JavascriptCompiler.Compile("b / c + e * g");

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            bindings.Add(new SortField("intfield", SortFieldType.INT32));
            bindings.Add("a", exprA);
            bindings.Add("b", exprB);
            bindings.Add("c", exprC);
            bindings.Add("d", exprD);
            bindings.Add("e", exprE);
            bindings.Add("f", exprF);
            bindings.Add("g", exprG);
            bindings.Add("h", exprH);
            bindings.Add("i", exprI);

            Assert.IsTrue(exprA.GetSortField(bindings, true).NeedsScores);
            Assert.IsFalse(exprB.GetSortField(bindings, true).NeedsScores);
            Assert.IsFalse(exprC.GetSortField(bindings, true).NeedsScores);
            Assert.IsTrue(exprD.GetSortField(bindings, true).NeedsScores);
            Assert.IsFalse(exprE.GetSortField(bindings, true).NeedsScores);
            Assert.IsTrue(exprF.GetSortField(bindings, true).NeedsScores);
            Assert.IsFalse(exprG.GetSortField(bindings, true).NeedsScores);
            Assert.IsTrue(exprH.GetSortField(bindings, true).NeedsScores);
            Assert.IsFalse(exprI.GetSortField(bindings, false).NeedsScores);
        }
Example #16
0
        public virtual void TestDistanceSort()
        {
            var            distance = JavascriptCompiler.Compile("haversin(40.7143528,-74.0059731,latitude,longitude)");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("latitude", SortFieldType.DOUBLE));
            bindings.Add(new SortField("longitude", SortFieldType.DOUBLE));
            Sort         sort = new Sort(distance.GetSortField(bindings, false));
            TopFieldDocs td   = searcher.Search(new MatchAllDocsQuery(), null, 3, sort);
            FieldDoc     d    = (FieldDoc)td.ScoreDocs[0];

            AreEqual(0.4619D, (double)d.Fields[0], 1E-4);
            d = (FieldDoc)td.ScoreDocs[1];
            AreEqual(1.0546D, (double)d.Fields[0], 1E-4);
            d = (FieldDoc)td.ScoreDocs[2];
            AreEqual(5.2842D, (double)d.Fields[0], 1E-4);
        }
Example #17
0
        public virtual void TestSortValues()
        {
            var            expr     = JavascriptCompiler.Compile("sqrt(_score)");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            Sort         sort  = new Sort(expr.GetSortField(bindings, true));
            Query        query = new TermQuery(new Term("body", "contents"));
            TopFieldDocs td    = searcher.Search(query, null, 3, sort, true, true);

            for (int i = 0; i < 3; i++)
            {
                FieldDoc d        = (FieldDoc)td.ScoreDocs[i];
                float    expected = (float)Math.Sqrt(d.Score);
                float    actual   = (float)((double)d.Fields[0]);
                AreEqual(expected, actual, CheckHits.ExplainToleranceDelta(expected, actual));
            }
        }
        public virtual void TestCoRecursion4()
        {
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add("cycle0", JavascriptCompiler.Compile("100"));
            bindings.Add("cycle1", JavascriptCompiler.Compile("100"));
            bindings.Add("cycle2", JavascriptCompiler.Compile("cycle1 + cycle0 + cycle3"));
            bindings.Add("cycle3", JavascriptCompiler.Compile("cycle0 + cycle1 + cycle2"));
            try
            {
                bindings.Validate();
                Fail("didn't get expected exception");
            }
            catch (ArgumentException expected)
            {
                IsTrue(expected.Message.Contains("Cycle detected"));
            }
        }
Example #19
0
        public virtual void TestTwoOfSameBinding()
        {
            var            expr     = JavascriptCompiler.Compile("_score + _score");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            Sort         sort  = new Sort(expr.GetSortField(bindings, true));
            Query        query = new TermQuery(new Term("body", "contents"));
            TopFieldDocs td    = searcher.Search(query, null, 3, sort, true, true);

            for (int i = 0; i < 3; i++)
            {
                FieldDoc d        = (FieldDoc)td.ScoreDocs[i];
                float    expected = 2 * d.Score;
                float    actual   = ((J2N.Numerics.Double)d.Fields[0]).ToSingle();
                Assert.AreEqual(expected, actual, CheckHits.ExplainToleranceDelta
                                    (expected, actual));
            }
        }
Example #20
0
        public virtual void TestEquals()
        {
            Expression expr = JavascriptCompiler.Compile("sqrt(a) + ln(b)");

            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("a", SortFieldType.INT32));
            bindings.Add(new SortField("b", SortFieldType.INT32));

            ValueSource vs1 = expr.GetValueSource(bindings);

            // same instance
            Assert.AreEqual(vs1, vs1);
            // null
            Assert.IsFalse(vs1.Equals(null));
            // other object
            Assert.IsFalse(vs1.Equals("foobar"));

            // same bindings and expression instances
            ValueSource vs2 = expr.GetValueSource(bindings);

            Assert.AreEqual(vs1.GetHashCode(), vs2.GetHashCode());
            Assert.AreEqual(vs1, vs2);

            // equiv bindings (different instance)
            SimpleBindings bindings2 = new SimpleBindings();

            bindings2.Add(new SortField("a", SortFieldType.INT32));
            bindings2.Add(new SortField("b", SortFieldType.INT32));
            ValueSource vs3 = expr.GetValueSource(bindings2);

            Assert.AreEqual(vs1, vs3);

            // different bindings (same names, different types)
            SimpleBindings bindings3 = new SimpleBindings();

            bindings3.Add(new SortField("a", SortFieldType.INT64));
            bindings3.Add(new SortField("b", SortFieldType.INT32));
            ValueSource vs4 = expr.GetValueSource(bindings3);

            Assert.IsFalse(vs1.Equals(vs4));
        }
 public virtual void TestNeedsScores()
 {
     SimpleBindings bindings = new SimpleBindings();
     // refers to score directly
     Expression exprA = JavascriptCompiler.Compile("_score");
     // constant
     Expression exprB = JavascriptCompiler.Compile("0");
     // field
     Expression exprC = JavascriptCompiler.Compile("intfield");
     // score + constant
     Expression exprD = JavascriptCompiler.Compile("_score + 0");
     // field + constant
     Expression exprE = JavascriptCompiler.Compile("intfield + 0");
     // expression + constant (score ref'd)
     Expression exprF = JavascriptCompiler.Compile("a + 0");
     // expression + constant
     Expression exprG = JavascriptCompiler.Compile("e + 0");
     // several variables (score ref'd)
     Expression exprH = JavascriptCompiler.Compile("b / c + e * g - sqrt(f)");
     // several variables
     Expression exprI = JavascriptCompiler.Compile("b / c + e * g");
     bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
     bindings.Add(new SortField("intfield", SortField.Type_e.INT));
     bindings.Add("a", exprA);
     bindings.Add("b", exprB);
     bindings.Add("c", exprC);
     bindings.Add("d", exprD);
     bindings.Add("e", exprE);
     bindings.Add("f", exprF);
     bindings.Add("g", exprG);
     bindings.Add("h", exprH);
     bindings.Add("i", exprI);
     IsTrue(exprA.GetSortField(bindings, true).NeedsScores());
     IsFalse(exprB.GetSortField(bindings, true).NeedsScores());
     IsFalse(exprC.GetSortField(bindings, true).NeedsScores());
     IsTrue(exprD.GetSortField(bindings, true).NeedsScores());
     IsFalse(exprE.GetSortField(bindings, true).NeedsScores());
     IsTrue(exprF.GetSortField(bindings, true).NeedsScores());
     IsFalse(exprG.GetSortField(bindings, true).NeedsScores());
     IsTrue(exprH.GetSortField(bindings, true).NeedsScores());
     IsFalse(exprI.GetSortField(bindings, false).NeedsScores());
 }
Example #22
0
        public virtual void TestExpressionRefersToExpression()
        {
            var expr1    = JavascriptCompiler.Compile("_score");
            var expr2    = JavascriptCompiler.Compile("2*expr1");
            var bindings = new SimpleBindings();

            bindings.Add(new SortField("_score", SortFieldType.SCORE));
            bindings.Add("expr1", expr1);
            Sort         sort  = new Sort(expr2.GetSortField(bindings, true));
            Query        query = new TermQuery(new Term("body", "contents"));
            TopFieldDocs td    = searcher.Search(query, null, 3, sort, true, true);

            for (int i = 0; i < 3; i++)
            {
                FieldDoc d        = (FieldDoc)td.ScoreDocs[i];
                float    expected = 2 * d.Score;
                float    actual   = (float)((double)d.Fields[0]);
                Assert.AreEqual(expected, actual, CheckHits.ExplainToleranceDelta
                                    (expected, actual));
            }
        }
        internal virtual void AssertQuery(Query query, Filter filter, Sort sort)
        {
            int     size     = TestUtil.NextInt(Random(), 1, searcher.IndexReader.MaxDoc / 5);
            TopDocs expected = searcher.Search(query, filter, size, sort, Random().NextBoolean
                                                   (), Random().NextBoolean());

            // make our actual sort, mutating original by replacing some of the
            // sortfields with equivalent expressions
            SortField[] original = sort.GetSort();
            SortField[] mutated  = new SortField[original.Length];
            for (int i = 0; i < mutated.Length; i++)
            {
                if (Random().Next(3) > 0)
                {
                    SortField      s              = original[i];
                    Expression     expr           = JavascriptCompiler.Compile(s.Field);
                    SimpleBindings simpleBindings = new SimpleBindings();
                    simpleBindings.Add(s);
                    bool reverse = s.Type == SortFieldType.SCORE || s.IsReverse;
                    mutated[i] = expr.GetSortField(simpleBindings, reverse);
                }
                else
                {
                    mutated[i] = original[i];
                }
            }
            Sort    mutatedSort = new Sort(mutated);
            TopDocs actual      = searcher.Search(query, filter, size, mutatedSort, Random().NextBoolean
                                                      (), Random().NextBoolean());

            CheckHits.CheckEqual(query, expected.ScoreDocs, actual.ScoreDocs);
            if (size < actual.TotalHits)
            {
                expected = searcher.SearchAfter(expected.ScoreDocs[size - 1], query, filter, size
                                                , sort);
                actual = searcher.SearchAfter(actual.ScoreDocs[size - 1], query, filter, size, mutatedSort
                                              );
                CheckHits.CheckEqual(query, expected.ScoreDocs, actual.ScoreDocs);
            }
        }
Example #24
0
        public virtual void TestTypes()
        {
            Expression     expr     = JavascriptCompiler.Compile("2*popularity");
            SimpleBindings bindings = new SimpleBindings();

            bindings.Add(new SortField("popularity", SortFieldType.INT64));
            ValueSource vs = expr.GetValueSource(bindings);

            Assert.AreEqual(1, reader.Leaves.Count);
            AtomicReaderContext leaf   = reader.Leaves[0];
            FunctionValues      values = vs.GetValues(new Dictionary <string, object>(), leaf);

            Assert.AreEqual(10, values.DoubleVal(0), 0);
            Assert.AreEqual(10, values.SingleVal(0), 0);
            Assert.AreEqual(10, values.Int64Val(0));
            Assert.AreEqual(10, values.Int32Val(0));
            Assert.AreEqual(10, values.Int16Val(0));
            Assert.AreEqual((byte)10, values.ByteVal(0));
            Assert.AreEqual("10.0", values.StrVal(0));
            Assert.AreEqual(J2N.Numerics.Double.GetInstance(10), values.ObjectVal(0));

            Assert.AreEqual(40, values.DoubleVal(1), 0);
            Assert.AreEqual(40, values.SingleVal(1), 0);
            Assert.AreEqual(40, values.Int64Val(1));
            Assert.AreEqual(40, values.Int32Val(1));
            Assert.AreEqual(40, values.Int16Val(1));
            Assert.AreEqual((byte)40, values.ByteVal(1));
            Assert.AreEqual("40.0", values.StrVal(1));
            Assert.AreEqual(J2N.Numerics.Double.GetInstance(40), values.ObjectVal(1));

            Assert.AreEqual(4, values.DoubleVal(2), 0);
            Assert.AreEqual(4, values.SingleVal(2), 0);
            Assert.AreEqual(4, values.Int64Val(2));
            Assert.AreEqual(4, values.Int32Val(2));
            Assert.AreEqual(4, values.Int16Val(2));
            Assert.AreEqual((byte)4, values.ByteVal(2));
            Assert.AreEqual("4.0", values.StrVal(2));
            Assert.AreEqual(J2N.Numerics.Double.GetInstance(4), values.ObjectVal(2));
        }
 public virtual void TestToString()
 {
     Expression expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
     bindings.Add(new SortField("popularity", SortField.Type_e.INT));
     SortField sf = expr.GetSortField(bindings, true);
     AreEqual("<expr \"sqrt(_score) + ln(popularity)\">!", sf.ToString());
 }
 public virtual void TestTypes()
 {
     Expression expr = JavascriptCompiler.Compile("2*popularity");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("popularity", SortField.Type_e.LONG));
     ValueSource vs = expr.GetValueSource(bindings);
     AreEqual(1, reader.Leaves.Count);
     AtomicReaderContext leaf = reader.Leaves[0];
     FunctionValues values = vs.GetValues(new Dictionary<string, object>(), leaf);
     AreEqual(10, values.DoubleVal(0), 0);
     AreEqual(10, values.FloatVal(0), 0);
     AreEqual(10, values.LongVal(0));
     AreEqual(10, values.IntVal(0));
     AreEqual(10, values.ShortVal(0));
     AreEqual(10, values.ByteVal(0));
     AreEqual("10", values.StrVal(0));
     AreEqual(System.Convert.ToDouble(10), values.ObjectVal(0));
     AreEqual(40, values.DoubleVal(1), 0);
     AreEqual(40, values.FloatVal(1), 0);
     AreEqual(40, values.LongVal(1));
     AreEqual(40, values.IntVal(1));
     AreEqual(40, values.ShortVal(1));
     AreEqual(40, values.ByteVal(1));
     AreEqual("40", values.StrVal(1));
     AreEqual(System.Convert.ToDouble(40), values.ObjectVal(1));
     AreEqual(4, values.DoubleVal(2), 0);
     AreEqual(4, values.FloatVal(2), 0);
     AreEqual(4, values.LongVal(2));
     AreEqual(4, values.IntVal(2));
     AreEqual(4, values.ShortVal(2));
     AreEqual(4, values.ByteVal(2));
     AreEqual("4", values.StrVal(2));
     AreEqual(System.Convert.ToDouble(4), values.ObjectVal(2));
 }
 public virtual void TestValidExternals()
 {
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("valid0", SortField.Type_e.INT));
     bindings.Add(new SortField("valid1", SortField.Type_e.INT));
     bindings.Add(new SortField("valid2", SortField.Type_e.INT));
     bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
     bindings.Add("valide0", JavascriptCompiler.Compile("valid0 - valid1 + valid2 + _score"
         ));
     bindings.Validate();
     bindings.Add("valide1", JavascriptCompiler.Compile("valide0 + valid0"));
     bindings.Validate();
     bindings.Add("valide2", JavascriptCompiler.Compile("valide0 * valide1"));
     bindings.Validate();
 }
 public virtual void TestInvalidExternal()
 {
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("valid", SortField.Type_e.INT));
     bindings.Add("invalid", JavascriptCompiler.Compile("badreference"));
     try
     {
         bindings.Validate();
         Fail("didn't get expected exception");
     }
     catch (ArgumentException expected)
     {
         IsTrue(expected.Message.Contains("Invalid reference"));
     }
 }
 internal virtual void AssertQuery(Query query, Filter filter, Sort sort)
 {
     int size = TestUtil.NextInt(Random(), 1, searcher.IndexReader.MaxDoc / 5);
     TopDocs expected = searcher.Search(query, filter, size, sort, Random().NextBoolean
         (), Random().NextBoolean());
     // make our actual sort, mutating original by replacing some of the 
     // sortfields with equivalent expressions
     SortField[] original = sort.GetSort();
     SortField[] mutated = new SortField[original.Length];
     for (int i = 0; i < mutated.Length; i++)
     {
         if (Random().Next(3) > 0)
         {
             SortField s = original[i];
             Expression expr = JavascriptCompiler.Compile(s.Field);
             SimpleBindings simpleBindings = new SimpleBindings();
             simpleBindings.Add(s);
             bool reverse = s.Type == SortField.Type_e.SCORE || s.Reverse;
             mutated[i] = expr.GetSortField(simpleBindings, reverse);
         }
         else
         {
             mutated[i] = original[i];
         }
     }
     Sort mutatedSort = new Sort(mutated);
     TopDocs actual = searcher.Search(query, filter, size, mutatedSort, Random().NextBoolean
         (), Random().NextBoolean());
     CheckHits.CheckEqual(query, expected.ScoreDocs, actual.ScoreDocs);
     if (size < actual.TotalHits)
     {
         expected = searcher.SearchAfter(expected.ScoreDocs[size - 1], query, filter, size
             , sort);
         actual = searcher.SearchAfter(actual.ScoreDocs[size - 1], query, filter, size, mutatedSort
             );
         CheckHits.CheckEqual(query, expected.ScoreDocs, actual.ScoreDocs);
     }
 }
 public virtual void TestRangeScorer()
 {
     Expression expr = JavascriptCompiler.Compile("2*popularity");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("popularity", SortField.Type_e.LONG));
     ValueSource vs = expr.GetValueSource(bindings);
     AreEqual(1, reader.Leaves.Count);
     AtomicReaderContext leaf = reader.Leaves[0];
     FunctionValues values = vs.GetValues(new Dictionary<string, object>(), leaf);
     // everything
     ValueSourceScorer scorer = values.GetRangeScorer(leaf.Reader, "4"
         , "40", true, true);
     AreEqual(-1, scorer.DocID());
     AreEqual(0, scorer.NextDoc());
     AreEqual(1, scorer.NextDoc());
     AreEqual(2, scorer.NextDoc());
     AreEqual(DocIdSetIterator.NO_MORE_DOCS, scorer.NextDoc());
     // just the first doc
     scorer = values.GetRangeScorer(leaf.Reader, "4", "40", false, false);
     AreEqual(-1, scorer.DocID());
     AreEqual(0, scorer.NextDoc());
     AreEqual(DocIdSetIterator.NO_MORE_DOCS, scorer.NextDoc());
 }
 public virtual void TestTwoOfSameBinding()
 {
     var expr = JavascriptCompiler.Compile("_score + _score");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("_score", SortField.Type_e.SCORE));
     Sort sort = new Sort(expr.GetSortField(bindings, true));
     Query query = new TermQuery(new Term("body", "contents"));
     TopFieldDocs td = searcher.Search(query, null, 3, sort, true, true);
     for (int i = 0; i < 3; i++)
     {
         FieldDoc d = (FieldDoc)td.ScoreDocs[i];
         float expected = 2 * d.Score;
         float actual = (float)((double)d.Fields[0]);
         AreEqual(expected, actual, CheckHits.ExplainToleranceDelta
             (expected, actual));
     }
 }
 public virtual void TestDistanceSort()
 {
     var distance = JavascriptCompiler.Compile("haversin(40.7143528,-74.0059731,latitude,longitude)");
     SimpleBindings bindings = new SimpleBindings();
     bindings.Add(new SortField("latitude", SortField.Type_e.DOUBLE));
     bindings.Add(new SortField("longitude", SortField.Type_e.DOUBLE));
     Sort sort = new Sort(distance.GetSortField(bindings, false));
     TopFieldDocs td = searcher.Search(new MatchAllDocsQuery(), null, 3, sort);
     FieldDoc d = (FieldDoc)td.ScoreDocs[0];
     AreEqual(0.4619D, (double)d.Fields[0], 1E-4);
     d = (FieldDoc)td.ScoreDocs[1];
     AreEqual(1.0546D, (double)d.Fields[0], 1E-4);
     d = (FieldDoc)td.ScoreDocs[2];
     AreEqual(5.2842D, (double)d.Fields[0], 1E-4);
 }
 // TODO: ideally we'd test > Short.MAX_VALUE too, but compilation is currently recursive.
 // so if we want to test such huge expressions, we need to instead change parser to use an explicit Stack
 /// <exception cref="System.Exception"></exception>
 private void DoTestLotsOfBindings(int n)
 {
     SimpleBindings bindings = new SimpleBindings();
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < n; i++)
     {
         if (i > 0)
         {
             sb.Append("+");
         }
         sb.Append("x" + i);
         bindings.Add(new SortField("x" + i, SortField.Type_e.SCORE));
     }
     var expr = JavascriptCompiler.Compile(sb.ToString());
     var sort = new Sort(expr.GetSortField(bindings, true));
     Query query = new TermQuery(new Term("body", "contents"));
     TopFieldDocs td = searcher.Search(query, null, 3, sort, true, true);
     for (int i_1 = 0; i_1 < 3; i_1++)
     {
         FieldDoc d = (FieldDoc)td.ScoreDocs[i_1];
         float expected = n * d.Score;
         float actual = (float)((double)d.Fields[0]);
         AreEqual(expected, actual, CheckHits.ExplainToleranceDelta(expected, actual));
     }
 }