Search in sources :

Example 16 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class TestCustomFunctions method testNoArgMethod.

/** tests a method with no arguments */
public void testNoArgMethod() throws Exception {
    Map<String, Method> functions = new HashMap<>();
    functions.put("foo", getClass().getMethod("zeroArgMethod"));
    Expression expr = JavascriptCompiler.compile("foo()", functions, getClass().getClassLoader());
    assertEquals(5, expr.evaluate(null), DELTA);
}
Also used : HashMap(java.util.HashMap) Expression(org.apache.lucene.expressions.Expression) Method(java.lang.reflect.Method)

Example 17 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class DocumentExpressionDictionaryFactory method fromExpression.

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);
    }
    return expression.getDoubleValuesSource(bindings).toLongValuesSource();
}
Also used : Expression(org.apache.lucene.expressions.Expression) SimpleBindings(org.apache.lucene.expressions.SimpleBindings) SortField(org.apache.lucene.search.SortField) ParseException(java.text.ParseException)

Example 18 with Expression

use of org.apache.lucene.expressions.Expression in project lucene-solr by apache.

the class ExpressionAggregationFacetsExample method search.

/** 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();
    // the score of the document
    bindings.add(new SortField("_score", SortField.Type.SCORE));
    // the value of the 'popularity' field
    bindings.add(new SortField("popularity", SortField.Type.LONG));
    // 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;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) Expression(org.apache.lucene.expressions.Expression) SimpleBindings(org.apache.lucene.expressions.SimpleBindings) SortField(org.apache.lucene.search.SortField) TaxonomyFacetSumValueSource(org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Aggregations

Expression (org.apache.lucene.expressions.Expression)18 Method (java.lang.reflect.Method)8 HashMap (java.util.HashMap)8 ParseException (java.text.ParseException)4 SimpleBindings (org.apache.lucene.expressions.SimpleBindings)4 SortField (org.apache.lucene.search.SortField)4 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 AccessControlContext (java.security.AccessControlContext)1 VariableContext (org.apache.lucene.expressions.js.VariableContext)1 FacetResult (org.apache.lucene.facet.FacetResult)1 Facets (org.apache.lucene.facet.Facets)1 FacetsCollector (org.apache.lucene.facet.FacetsCollector)1 TaxonomyFacetSumValueSource (org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource)1 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)1 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1