[ 
https://issues.apache.org/jira/browse/SOLR-5707?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14028887#comment-14028887
 ] 

Uwe Schindler edited comment on SOLR-5707 at 6/12/14 7:15 AM:
--------------------------------------------------------------

bq. ...i'm not really a fan of that syntax though – I haven't played around 
with expressions enough yet to know what kinds of "variable" names are allowed, 
but if we can use variables like $1 in expressions, then the Bindings could 
look for the $ prefix and map those directly to the positional function args 
w/o needing the explicit positional-bindings configuration which would be much 
cleaner.

{{$}} is not a special character in Javascript, so it can be part of a variable 
or function name. One famous example is the $-Function in JQuery: 
{{$('.class')}}. If the Lucene Expressions parser is not "special" it should 
accept {{$1}} as variable name in the binding and the expression parser itsself.

EDIT: I checked it: 

{code}
  public void testDollarVariable() throws Exception {
    Expression expr = JavascriptCompiler.compile("sqrt($0)");
    
    SimpleBindings bindings = new SimpleBindings();    
    bindings.add(new SortField("$0", SortField.Type.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 = ((Double)d.fields[0]).floatValue();
      assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, 
actual));
    }
  }
{code}

This fails to parse, although its technically correct Javascript: 
java.text.ParseException:  unexpected character '$' at position (5).

What works: {{_1}} as variable/binding name. But on the other hand, disallowing 
$ is strange to me.

Another good thing (maybe as extension to expressions): It would be cool, if 
the JavaScript compiler could "report" back all variable used in the expression 
after compiling. We could then somehow automatically map them (but would not 
help with positional args).


was (Author: thetaphi):
bq. ...i'm not really a fan of that syntax though – I haven't played around 
with expressions enough yet to know what kinds of "variable" names are allowed, 
but if we can use variables like $1 in expressions, then the Bindings could 
look for the $ prefix and map those directly to the positional function args 
w/o needing the explicit positional-bindings configuration which would be much 
cleaner.

{{$}} is not a special character in Javascript, so it can be part of a variable 
or function name. One famous example is the $-Function in JQuery: 
{{$('.class')}}. If the Lucene Expressions parser is not "special" it should 
accept {{$1}} as variable name in the binding and the expression parser itsself.

EDIT: I checked it: 

{code}
  public void testDollarVariable() throws Exception {
    Expression expr = JavascriptCompiler.compile("sqrt($0)");
    
    SimpleBindings bindings = new SimpleBindings();    
    bindings.add(new SortField("$0", SortField.Type.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 = ((Double)d.fields[0]).floatValue();
      assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, 
actual));
    }
  }
{code}

This fails to parse, although its technically correct Javascript: 
java.text.ParseException:  unexpected character '$' at position (5).

Another good thing (maybe as extension to expressions): It would be cool, if 
the JavaScript compiler could "report" back all variable used in the expression 
after compiling. We could then somehow automatically map them (but would not 
help with positional args).

> Lucene Expressions in Solr
> --------------------------
>
>                 Key: SOLR-5707
>                 URL: https://issues.apache.org/jira/browse/SOLR-5707
>             Project: Solr
>          Issue Type: New Feature
>            Reporter: Ryan Ernst
>         Attachments: SOLR-5707.patch, SOLR-5707_vsp.patch
>
>
> Expressions should be available for use in Solr.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org
For additional commands, e-mail: dev-h...@lucene.apache.org

Reply via email to