Java Code Examples for org.apache.lucene.expressions.js.JavascriptCompiler#compile()
The following examples show how to use
org.apache.lucene.expressions.js.JavascriptCompiler#compile() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestFunctionScoreQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testBoostsAreAppliedLast() throws Exception { SimpleBindings bindings = new SimpleBindings(); bindings.add("score", DoubleValuesSource.SCORES); Expression expr = JavascriptCompiler.compile("ln(score + 4)"); Query q1 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "text")), expr.getDoubleValuesSource(bindings)); TopDocs plain = searcher.search(q1, 5); Query boosted = new BoostQuery(q1, 2); TopDocs afterboost = searcher.search(boosted, 5); assertEquals(plain.totalHits.value, afterboost.totalHits.value); for (int i = 0; i < 5; i++) { assertEquals(plain.scoreDocs[i].doc, afterboost.scoreDocs[i].doc); assertEquals(plain.scoreDocs[i].score, afterboost.scoreDocs[i].score / 2, 0.0001); } }
Example 2
Source File: TestFunctionScoreQuery.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testAccessToValueSource() throws Exception { FunctionScoreQuery q1 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "a")), DoubleValuesSource.constant(31)); Query q2 = new FunctionScoreQuery(q1.getWrappedQuery(), q1.getSource()); QueryUtils.check(q2); QueryUtils.checkEqual(q2, q1); FunctionScoreQuery q3 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "first")), DoubleValuesSource.fromIntField(INT_FIELD)); Query q4 = new FunctionScoreQuery(q3.getWrappedQuery(), q3.getSource()); QueryUtils.checkEqual(q3, q4); SimpleBindings bindings = new SimpleBindings(); bindings.add("score", DoubleValuesSource.SCORES); Expression expr = JavascriptCompiler.compile("ln(score + 4)"); FunctionScoreQuery q5 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "text")), expr.getDoubleValuesSource(bindings)); Query q6 = new FunctionScoreQuery(q5.getWrappedQuery(), q5.getSource()); QueryUtils.checkEqual(q5, q6); }
Example 3
Source File: TestExpressionValueSource.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testDoubleValuesSourceTypes() throws Exception { Expression expr = JavascriptCompiler.compile("2*popularity + count"); SimpleBindings bindings = new SimpleBindings(); bindings.add("popularity", DoubleValuesSource.fromLongField("popularity")); bindings.add("count", DoubleValuesSource.fromLongField("count")); DoubleValuesSource vs = expr.getDoubleValuesSource(bindings); assertEquals(1, reader.leaves().size()); LeafReaderContext leaf = reader.leaves().get(0); DoubleValues values = vs.getValues(leaf, null); assertTrue(values.advanceExact(0)); assertEquals(10, values.doubleValue(), 0); assertTrue(values.advanceExact(1)); assertEquals(41, values.doubleValue(), 0); assertTrue(values.advanceExact(2)); assertEquals(4, values.doubleValue(), 0); }
Example 4
Source File: TestDemoExpressions.java From lucene-solr with Apache License 2.0 | 6 votes |
/** tests the returned sort values are correct */ public void testSortValues() throws Exception { Expression expr = JavascriptCompiler.compile("sqrt(_score)"); SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); Sort sort = new Sort(expr.getSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.search(query, 3, sort, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc) td.scoreDocs[i]; float expected = (float) Math.sqrt(d.score); float actual = ((Double)d.fields[0]).floatValue(); assertEquals(expected, actual, 0d); } }
Example 5
Source File: TestDemoExpressions.java From lucene-solr with Apache License 2.0 | 6 votes |
/** tests same binding used more than once in an expression */ public void testTwoOfSameBinding() throws Exception { Expression expr = JavascriptCompiler.compile("_score + _score"); SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); Sort sort = new Sort(expr.getSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.search(query, 3, sort, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc) td.scoreDocs[i]; float expected = 2*d.score; float actual = ((Double)d.fields[0]).floatValue(); assertEquals(expected, actual, 0d); } }
Example 6
Source File: TestDemoExpressions.java From lucene-solr with Apache License 2.0 | 6 votes |
/** Uses variables with $ */ public void testDollarVariable() throws Exception { Expression expr = JavascriptCompiler.compile("$0+$score"); SimpleBindings bindings = new SimpleBindings(); bindings.add("$0", DoubleValuesSource.SCORES); bindings.add("$score", DoubleValuesSource.SCORES); Sort sort = new Sort(expr.getSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.search(query, 3, sort, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc) td.scoreDocs[i]; float expected = 2*d.score; float actual = ((Double)d.fields[0]).floatValue(); assertEquals(expected, actual, 0d); } }
Example 7
Source File: TestDemoExpressions.java From lucene-solr with Apache License 2.0 | 6 votes |
/** tests expression referring to another expression */ public void testExpressionRefersToExpression() throws Exception { Expression expr1 = JavascriptCompiler.compile("_score"); Expression expr2 = JavascriptCompiler.compile("2*expr1"); SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); 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, 3, sort, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc) td.scoreDocs[i]; float expected = 2*d.score; float actual = ((Double)d.fields[0]).floatValue(); assertEquals(expected, actual, 0d); } }
Example 8
Source File: TestDemoExpressions.java From lucene-solr with Apache License 2.0 | 6 votes |
private void doTestLotsOfBindings(int n) throws Exception { 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("x" + i, DoubleValuesSource.SCORES); } Expression expr = JavascriptCompiler.compile(sb.toString()); Sort sort = new Sort(expr.getSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.search(query, 3, sort, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc) td.scoreDocs[i]; float expected = n*d.score; float actual = ((Double)d.fields[0]).floatValue(); assertEquals(expected, actual, 0d); } }
Example 9
Source File: WrappedIntPointField.java From lucene-solr with Apache License 2.0 | 5 votes |
/** static helper for re-use in sibling trie class */ public static SortField getSortField(final SortField superSort, final SchemaField field) { field.checkSortability(); Expression expr = null; try { expr = JavascriptCompiler.compile(field.getName() + " % 3"); } catch (Exception e) { throw new RuntimeException("impossible?", e); } SimpleBindings bindings = new SimpleBindings(); bindings.add(superSort.getField(), fromSortField(superSort)); return expr.getSortField(bindings, superSort.getReverse()); }
Example 10
Source File: ExpressionAggregationFacetsExample.java From lucene-solr with Apache License 2.0 | 5 votes |
/** User runs a query and aggregates facets. */ private FacetResult search() throws IOException, ParseException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // 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(); bindings.add("_score", DoubleValuesSource.SCORES); // the score of the document bindings.add("popularity", DoubleValuesSource.fromLongField("popularity")); // 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.getDoubleValuesSource(bindings)); FacetResult result = facets.getTopChildren(10, "A"); indexReader.close(); taxoReader.close(); return result; }
Example 11
Source File: DocumentExpressionDictionaryFactory.java From lucene-solr with Apache License 2.0 | 5 votes |
public LongValuesSource fromExpression(String weightExpression, Set<SortField> sortFields) { Expression expression = null; try { expression = JavascriptCompiler.compile(weightExpression); } catch (ParseException e) { throw new RuntimeException(e); } SimpleBindings bindings = new SimpleBindings(); for (SortField sortField : sortFields) { bindings.add(sortField.getField(), fromSortField(sortField)); } return expression.getDoubleValuesSource(bindings).toLongValuesSource(); }
Example 12
Source File: TestExpressionSorts.java From lucene-solr with Apache License 2.0 | 5 votes |
void assertQuery(Query query, Sort sort) throws Exception { int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5); TopDocs expected = searcher.search(query, size, sort, 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().nextInt(3) > 0) { SortField s = original[i]; Expression expr = JavascriptCompiler.compile(s.getField()); SimpleBindings simpleBindings = new SimpleBindings(); simpleBindings.add(s.getField(), fromSortField(s)); boolean reverse = s.getType() == SortField.Type.SCORE || s.getReverse(); mutated[i] = expr.getSortField(simpleBindings, reverse); } else { mutated[i] = original[i]; } } Sort mutatedSort = new Sort(mutated); TopDocs actual = searcher.search(query, size, mutatedSort, random().nextBoolean()); CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs); if (size < actual.totalHits.value) { expected = searcher.searchAfter(expected.scoreDocs[size-1], query, size, sort); actual = searcher.searchAfter(actual.scoreDocs[size-1], query, size, mutatedSort); CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs); } }
Example 13
Source File: TestExpressionValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testRewrite() throws Exception { Expression expr = JavascriptCompiler.compile("a"); ExpressionValueSource rewritingExpressionSource = new ExpressionValueSource( new DoubleValuesSource[]{createDoubleValuesSourceMock(true)}, expr, false); ExpressionValueSource notRewritingExpressionSource = new ExpressionValueSource( new DoubleValuesSource[]{createDoubleValuesSourceMock(false)}, expr, false); assertNotSame(rewritingExpressionSource, rewritingExpressionSource.rewrite(null)); assertSame(notRewritingExpressionSource, notRewritingExpressionSource.rewrite(null)); }
Example 14
Source File: TestExpressionValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testDoubleValuesSourceEquals() throws Exception { Expression expr = JavascriptCompiler.compile("sqrt(a) + ln(b)"); SimpleBindings bindings = new SimpleBindings(); bindings.add("a", DoubleValuesSource.fromIntField("a")); bindings.add("b", DoubleValuesSource.fromIntField("b")); DoubleValuesSource vs1 = expr.getDoubleValuesSource(bindings); // same instance assertEquals(vs1, vs1); // null assertFalse(vs1.equals(null)); // other object assertFalse(vs1.equals("foobar")); // same bindings and expression instances DoubleValuesSource vs2 = expr.getDoubleValuesSource(bindings); assertEquals(vs1.hashCode(), vs2.hashCode()); assertEquals(vs1, vs2); // equiv bindings (different instance) SimpleBindings bindings2 = new SimpleBindings(); bindings2.add("a", DoubleValuesSource.fromIntField("a")); bindings2.add("b", DoubleValuesSource.fromIntField("b")); DoubleValuesSource vs3 = expr.getDoubleValuesSource(bindings2); assertEquals(vs1, vs3); // different bindings (same names, different types) SimpleBindings bindings3 = new SimpleBindings(); bindings3.add("a", DoubleValuesSource.fromLongField("a")); bindings3.add("b", DoubleValuesSource.fromFloatField("b")); DoubleValuesSource vs4 = expr.getDoubleValuesSource(bindings3); assertFalse(vs1.equals(vs4)); }
Example 15
Source File: TestDemoExpressions.java From lucene-solr with Apache License 2.0 | 5 votes |
/** an example of how to rank by an expression */ public void test() throws Exception { // compile an expression: Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)"); // we use SimpleBindings: which just maps variables to SortField instances SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); bindings.add("popularity", DoubleValuesSource.fromIntField("popularity")); // 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, 3, sort); }
Example 16
Source File: TestExpressionRescorer.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testBasic() throws Exception { // create a sort field and sort by it (reverse order) Query query = new TermQuery(new Term("body", "contents")); IndexReader r = searcher.getIndexReader(); // Just first pass query TopDocs hits = searcher.search(query, 10); assertEquals(3, hits.totalHits.value); assertEquals("3", r.document(hits.scoreDocs[0].doc).get("id")); assertEquals("1", r.document(hits.scoreDocs[1].doc).get("id")); assertEquals("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("popularity", DoubleValuesSource.fromIntField("popularity")); bindings.add("_score", DoubleValuesSource.SCORES); Rescorer rescorer = e.getRescorer(bindings); hits = rescorer.rescore(searcher, hits, 10); assertEquals(3, hits.totalHits.value); assertEquals("2", r.document(hits.scoreDocs[0].doc).get("id")); assertEquals("1", r.document(hits.scoreDocs[1].doc).get("id")); assertEquals("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: assertTrue(expl.contains("= double(popularity)")); // Confirm the explanation includes first pass details: assertTrue(expl.contains("= first pass score")); assertTrue(expl.contains("body:contents in")); }
Example 17
Source File: TestExpressionSortField.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testEquals() throws Exception { Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)"); SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); bindings.add("popularity", DoubleValuesSource.fromIntField("popularity")); SimpleBindings otherBindings = new SimpleBindings(); otherBindings.add("_score", DoubleValuesSource.fromLongField("_score")); otherBindings.add("popularity", DoubleValuesSource.fromIntField("popularity")); SortField sf1 = expr.getSortField(bindings, true); // different order SortField sf2 = expr.getSortField(bindings, false); assertFalse(sf1.equals(sf2)); // different bindings sf2 = expr.getSortField(otherBindings, true); assertFalse(sf1.equals(sf2)); // different expression Expression other = JavascriptCompiler.compile("popularity/2"); sf2 = other.getSortField(bindings, true); assertFalse(sf1.equals(sf2)); // null assertFalse(sf1.equals(null)); // same instance: assertEquals(sf1, sf1); }
Example 18
Source File: TestExpressionSortField.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testToString() throws Exception { Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)"); SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); bindings.add("popularity", DoubleValuesSource.fromIntField("popularity")); SortField sf = expr.getSortField(bindings, true); assertEquals("<expr(sqrt(_score) + ln(popularity))>!", sf.toString()); }
Example 19
Source File: DistanceFacetsExample.java From lucene-solr with Apache License 2.0 | 5 votes |
private DoubleValuesSource getDistanceValueSource() { Expression distance; try { distance = JavascriptCompiler.compile( "haversin(" + ORIGIN_LATITUDE + "," + ORIGIN_LONGITUDE + ",latitude,longitude)"); } catch (ParseException pe) { // Should not happen throw new RuntimeException(pe); } SimpleBindings bindings = new SimpleBindings(); bindings.add("latitude", DoubleValuesSource.fromDoubleField("latitude")); bindings.add("longitude", DoubleValuesSource.fromDoubleField("longitude")); return distance.getDoubleValuesSource(bindings); }
Example 20
Source File: TestExpressionSortField.java From lucene-solr with Apache License 2.0 | 4 votes |
public void testNeedsScores() throws Exception { 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("_score", DoubleValuesSource.SCORES); bindings.add("intfield", DoubleValuesSource.fromIntField("intfield")); 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); assertTrue(exprA.getSortField(bindings, true).needsScores()); assertFalse(exprB.getSortField(bindings, true).needsScores()); assertFalse(exprC.getSortField(bindings, true).needsScores()); assertTrue(exprD.getSortField(bindings, true).needsScores()); assertFalse(exprE.getSortField(bindings, true).needsScores()); assertTrue(exprF.getSortField(bindings, true).needsScores()); assertFalse(exprG.getSortField(bindings, true).needsScores()); assertTrue(exprH.getSortField(bindings, true).needsScores()); assertFalse(exprI.getSortField(bindings, false).needsScores()); }