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")); }
/// <summary>User runs a query and aggregates facets.</summary> private FacetResult Search() { using DirectoryReader indexReader = DirectoryReader.Open(indexDir); using TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); IndexSearcher searcher = new IndexSearcher(indexReader); // Aggregate categories by an expression that combines the document's score // and its popularity field Expression expr = JavascriptCompiler.Compile("_score * sqrt(popularity)"); SimpleBindings bindings = new SimpleBindings { new SortField("_score", SortFieldType.SCORE), // the score of the document new SortField("popularity", SortFieldType.INT64), // the value of the 'popularity' field }; // Aggregates the facet values FacetsCollector fc = new FacetsCollector(true); // MatchAllDocsQuery is for "browsing" (counts facets // for all non-deleted docs in the index); normally // you'd use a "normal" query: FacetsCollector.Search(searcher, new MatchAllDocsQuery(), 10, fc); // Retrieve results Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, expr.GetValueSource(bindings)); FacetResult result = facets.GetTopChildren(10, "A"); return(result); }
private void AssertEvaluatesTo(string expression, long expected) { Expression evaluator = JavascriptCompiler.Compile(expression); long actual = (long)evaluator.Evaluate(0, null); AreEqual(expected, actual); }
private void AssertEvaluatesTo(string expression, double expected) { Expression evaluator = JavascriptCompiler.Compile(expression); double actual = evaluator.Evaluate(0, null); AreEqual(expected, actual, DELTA); }
public virtual void TestEmpty() { try { JavascriptCompiler.Compile(string.Empty); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("()"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile(" \r\n \n \t"); Fail(); } catch (Exception) { } }
public virtual void TestDefaultList() { IDictionary <string, MethodInfo> functions = JavascriptCompiler.DEFAULT_FUNCTIONS; var expr = JavascriptCompiler.Compile("sqrt(20)", functions); AreEqual(Math.Sqrt(20), expr.Evaluate(0, null), DELTA); }
public virtual void TestValidCompiles() { IsNotNull(JavascriptCompiler.Compile("100")); IsNotNull(JavascriptCompiler.Compile("valid0+100")); IsNotNull(JavascriptCompiler.Compile("valid0+\n100")); IsNotNull(JavascriptCompiler.Compile("logn(2, 20+10-5.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()); }
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); }
// 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)); } }
public virtual void TestOneArgMethod() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("OneArgMethod", new [] { typeof(double) }); var expr = JavascriptCompiler.Compile("foo(3)", functions); AreEqual(6, expr.Evaluate(0, null), DELTA); }
public virtual void TestNoArgMethod() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("ZeroArgMethod"); var expr = JavascriptCompiler.Compile("foo()", functions); AreEqual(5, expr.Evaluate(0, null), DELTA); }
public virtual void TestNamespaces() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo.bar"] = GetType().GetMethod("ZeroArgMethod"); string source = "foo.bar()"; var expr = JavascriptCompiler.Compile(source, functions); AreEqual(5, expr.Evaluate(0, null), DELTA); }
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 TestNull() { try { JavascriptCompiler.Compile(null); Fail(); } catch (ArgumentNullException) { } }
private ValueSource GetDistanceValueSource() { Expression distance = JavascriptCompiler.Compile( string.Format(CultureInfo.InvariantCulture, "haversin({0:R},{1:R},latitude,longitude)", ORIGIN_LATITUDE, ORIGIN_LONGITUDE)); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("latitude", SortFieldType.DOUBLE)); bindings.Add(new SortField("longitude", SortFieldType.DOUBLE)); return(distance.GetValueSource(bindings)); }
public virtual void TestEmpty() { IDictionary <string, MethodInfo> functions = new HashMap <string, MethodInfo>(); try { JavascriptCompiler.Compile("sqrt(20)", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("Unrecognized method")); } }
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); }
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")); } }
public virtual void TestWrongReturnType() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("BogusReturnType"); try { JavascriptCompiler.Compile("foo()", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("does not return a double")); } }
public virtual void TestWrongNestedNotPublic() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = typeof(NestedNotPublic).GetMethod("Method"); try { JavascriptCompiler.Compile("foo()", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("is not public")); } }
public virtual void TestWrongParameterType() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("BogusParameterType", new [] { typeof(string) }); try { JavascriptCompiler.Compile("foo(2)", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("must take only double parameters" )); } }
public virtual void TestWrongNotPublic() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("NonPublicMethod", BindingFlags.NonPublic | BindingFlags.Static); try { JavascriptCompiler.Compile("foo()", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("is not public")); } }
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")); } }
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(); }
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); }
public virtual void TestInvalidCompiles() { try { JavascriptCompiler.Compile("100 100"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("7*/-8"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("0y1234"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("500EE"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("500.5EE"); Fail(); } catch (Exception) { } }
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); }
static int StreamInput(TextReader input, Encoding encoding, bool errorsWithStackTrace) { var builtinTypes = LoadBuiltinTypes(); var charReader = new TextReaderCharReader(input); var tokenizer = new Tokenizer(charReader); var interpreter = new DocumentProcessor(tokenizer, builtinTypes, new InstructionInterpreterFactory()); try { var namespaces = interpreter.Read(); var compiler = new JavascriptCompiler("example", namespaces, builtinTypes); foreach (var f in LoadBuiltinFunctions()) { compiler.AddBuiltinFunction(f); } using (MemoryStream mem = new MemoryStream()) using (StreamWriter memW = new StreamWriter(mem, encoding)) { compiler.Compile(memW); memW.Flush(); Console.Out.Write(encoding.GetString(mem.ToArray())); } return(0); } catch (SyntaxException ex) { Console.Error.WriteLine("Syntax error at " + ex.Position); Console.Error.WriteLine(ex.Message); if (errorsWithStackTrace) { Console.Error.WriteLine(ex.StackTrace); } return(0x101); } catch (CompileException ex) { Console.Error.WriteLine("Compile error at " + ex.Position); Console.Error.WriteLine(ex.Message); if (errorsWithStackTrace) { Console.Error.WriteLine(ex.StackTrace); } return(0x102); } }
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)); } }