List of usage examples for org.apache.lucene.expressions.js JavascriptCompiler compile
public static Expression compile(String sourceText) throws ParseException
From source file:com.czw.search.lucene.example.facet.DistanceFacetsExample.java
License:Apache License
private DoubleValuesSource getDistanceValueSource() { Expression distance;//from w ww. ja va2s . c om 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(new SortField("latitude", SortField.Type.DOUBLE)); bindings.add(new SortField("longitude", SortField.Type.DOUBLE)); return distance.getDoubleValuesSource(bindings); }
From source file:com.czw.search.lucene.example.facet.ExpressionAggregationFacetsExample.java
License:Apache License
/** 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(new SortField("_score", SortField.Type.SCORE)); // the score of the document bindings.add(new SortField("popularity", SortField.Type.LONG)); // 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();//from w ww .j a v a2 s. co m taxoReader.close(); return result; }
From source file:com.search.lucene.demo.facet.DistanceFacetsExample.java
License:Apache License
private ValueSource getDistanceValueSource() { Expression distance;/* w w w .j a v a 2 s . co m*/ 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(new SortField("latitude", SortField.Type.DOUBLE)); bindings.add(new SortField("longitude", SortField.Type.DOUBLE)); return distance.getValueSource(bindings); }
From source file:com.search.lucene.demo.facet.ExpressionAggregationFacetsExample.java
License:Apache License
/** 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(new SortField("_score", SortField.Type.SCORE)); // the score of the document bindings.add(new SortField("popularity", SortField.Type.LONG)); // 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"); indexReader.close();/*from ww w. ja v a 2 s.c o m*/ taxoReader.close(); return result; }
From source file:com.yida.framework.lucene5.facet.DistanceFacetsExample.java
License:Creative Commons License
private ValueSource getDistanceValueSource() { Expression distance;//from w w w.jav a 2 s . com try { //haversin?????Google ?haversin? distance = JavascriptCompiler .compile("haversin(" + ORIGIN_LATITUDE + "," + ORIGIN_LONGITUDE + ",latitude,longitude)"); } catch (ParseException pe) { throw new RuntimeException(pe); } SimpleBindings bindings = new SimpleBindings(); //?????????????? bindings.add(new SortField("latitude", SortField.Type.DOUBLE)); //?????? bindings.add(new SortField("longitude", SortField.Type.DOUBLE)); return distance.getValueSource(bindings); }
From source file:org.apache.solr.schema.WrappedIntField.java
License:Apache License
public WrappedIntField() { try {/*w w w . ja v a2 s . com*/ expr = JavascriptCompiler.compile("payload % 3"); } catch (Exception e) { throw new RuntimeException("impossible?", e); } }
From source file:org.apache.solr.schema.WrappedIntPointField.java
License:Apache License
/** static helper for re-use in sibling trie class */ public static SortField getSortField(final SortField superSort, final SchemaField field) { field.checkSortability();//from www . j av a 2 s.c o m 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); return expr.getSortField(bindings, superSort.getReverse()); }
From source file:org.apache.solr.spelling.suggest.DocumentExpressionDictionaryFactory.java
License:Apache License
public ValueSource fromExpression(String weightExpression, Set<SortField> sortFields) { Expression expression = null; try {//w w w . ja v a2s. c o m expression = JavascriptCompiler.compile(weightExpression); } catch (ParseException e) { throw new RuntimeException(); } SimpleBindings bindings = new SimpleBindings(); for (SortField sortField : sortFields) { bindings.add(sortField); } return expression.getValueSource(bindings); }
From source file:org.elasticsearch.script.expression.ExpressionScriptIT.java
License:Apache License
public void testExecutableScripts() throws Exception { Map<String, Object> vars = new HashMap<>(); vars.put("a", 2.5); vars.put("b", 3); vars.put("xyz", -1); Expression expr = JavascriptCompiler.compile("a+b+xyz"); CompiledScript compiledScript = new CompiledScript(ScriptType.INLINE, "", "expression", expr); ExpressionExecutableScript ees = new ExpressionExecutableScript(compiledScript, vars); assertEquals((Double) ees.run(), 4.5, 0.001); ees.setNextVar("b", -2.5); assertEquals((Double) ees.run(), -1, 0.001); ees.setNextVar("a", -2.5); ees.setNextVar("b", -2.5); ees.setNextVar("xyz", -2.5); assertEquals((Double) ees.run(), -7.5, 0.001); String message;/* w w w. j av a 2s .c o m*/ try { vars = new HashMap<>(); vars.put("a", 1); ees = new ExpressionExecutableScript(compiledScript, vars); ees.run(); fail("An incorrect number of variables were allowed to be used in an expression."); } catch (ScriptException se) { message = se.getMessage(); assertThat(message + " should have contained number of variables", message.contains("number of variables"), equalTo(true)); } try { vars = new HashMap<>(); vars.put("a", 1); vars.put("b", 3); vars.put("c", -1); ees = new ExpressionExecutableScript(compiledScript, vars); ees.run(); fail("A variable was allowed to be set that does not exist in the expression."); } catch (ScriptException se) { message = se.getMessage(); assertThat(message + " should have contained does not exist in", message.contains("does not exist in"), equalTo(true)); } try { vars = new HashMap<>(); vars.put("a", 1); vars.put("b", 3); vars.put("xyz", "hello"); ees = new ExpressionExecutableScript(compiledScript, vars); ees.run(); fail("A non-number was allowed to be use in the expression."); } catch (ScriptException se) { message = se.getMessage(); assertThat(message + " should have contained process numbers", message.contains("process numbers"), equalTo(true)); } }
From source file:org.elasticsearch.script.expression.ExpressionScriptTests.java
License:Apache License
public void testExecutableScripts() throws Exception { Map<String, Object> vars = new HashMap<>(); vars.put("a", 2.5); vars.put("b", 3); vars.put("xyz", -1); Expression expr = JavascriptCompiler.compile("a+b+xyz"); ExpressionExecutableScript ees = new ExpressionExecutableScript(expr, vars); assertEquals((Double) ees.run(), 4.5, 0.001); ees.setNextVar("b", -2.5); assertEquals((Double) ees.run(), -1, 0.001); ees.setNextVar("a", -2.5); ees.setNextVar("b", -2.5); ees.setNextVar("xyz", -2.5); assertEquals((Double) ees.run(), -7.5, 0.001); String message;/*w w w . j av a2s . c o m*/ try { vars = new HashMap<>(); vars.put("a", 1); ees = new ExpressionExecutableScript(expr, vars); ees.run(); fail("An incorrect number of variables were allowed to be used in an expression."); } catch (ScriptException se) { message = se.getMessage(); assertThat(message + " should have contained number of variables", message.contains("number of variables"), equalTo(true)); } try { vars = new HashMap<>(); vars.put("a", 1); vars.put("b", 3); vars.put("c", -1); ees = new ExpressionExecutableScript(expr, vars); ees.run(); fail("A variable was allowed to be set that does not exist in the expression."); } catch (ScriptException se) { message = se.getMessage(); assertThat(message + " should have contained does not exist in", message.contains("does not exist in"), equalTo(true)); } try { vars = new HashMap<>(); vars.put("a", 1); vars.put("b", 3); vars.put("xyz", "hello"); ees = new ExpressionExecutableScript(expr, vars); ees.run(); fail("A non-number was allowed to be use in the expression."); } catch (ScriptException se) { message = se.getMessage(); assertThat(message + " should have contained process numbers", message.contains("process numbers"), equalTo(true)); } }