You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by sling <sl...@gmail.com> on 2013/11/25 11:11:43 UTC

In a functon query, I can't get the ValueSource when extend ValueSourceParser

hi,
I am working with solr4.1.
When I don't parseValueSource, my function query works well. The code is
like this:
public class DateSourceParser extends ValueSourceParser {
	@Override
	public void init(NamedList namedList) {
	}
	@Override
	*public ValueSource parse(FunctionQParser fp) throws SyntaxError {		
		return new DateFunction();
	}*
}

When I want to use the ValueSource, like this:
public class DateSourceParser extends ValueSourceParser {
	@Override
	public void init(NamedList namedList) {
	}
	@Override
	*public ValueSource parse(FunctionQParser fp) throws SyntaxError {
		ValueSource source = fp.parseValueSource();
		return new DateFunction(source);
	}*
}

fp.parseValueSource() throws an error like this:
ERROR [org.apache.solr.core.SolrCore] -
org.apache.solr.common.SolrException: org.apache.solr.search.SyntaxError:
Expected identifier at pos 12 str='dateDeboost()'
        at
org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:147)
        at
org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:187)
        at
org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:135)
        at org.apache.solr.core.SolrCore.execute(SolrCore.java:1816)
        at
org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:448)
        at
org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:269)
        at
com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
        at
com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:173)
        at
com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
        at
com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:274)
        at com.caucho.server.port.TcpConnection.run(TcpConnection.java:514)
        at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:527)
        at com.caucho.util.ThreadPool.run(ThreadPool.java:449)
        at java.lang.Thread.run(Thread.java:662)
Caused by: org.apache.solr.search.SyntaxError: Expected identifier at pos 12
str='dateDeboost()'
        at
org.apache.solr.search.QueryParsing$StrParser.getId(QueryParsing.java:747)
        at
org.apache.solr.search.QueryParsing$StrParser.getId(QueryParsing.java:726)
        at
org.apache.solr.search.FunctionQParser.parseValueSource(FunctionQParser.java:345)
        at
org.apache.solr.search.FunctionQParser.parseValueSource(FunctionQParser.java:223)
        at
org.sling.solr.custom.DateSourceParser.parse(DateSourceParser.java:24)
        at
org.apache.solr.search.FunctionQParser.parseValueSource(FunctionQParser.java:352)
        at
org.apache.solr.search.FunctionQParser.parse(FunctionQParser.java:68)
        at org.apache.solr.search.QParser.getQuery(QParser.java:142)
        at
org.apache.solr.search.BoostQParserPlugin$1.parse(BoostQParserPlugin.java:61)
        at org.apache.solr.search.QParser.getQuery(QParser.java:142)
        at
org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:117)
        ... 13 more


so, how to make fp.parseValueSource() work?

Thanks!!!

sling





--
View this message in context: http://lucene.472066.n3.nabble.com/In-a-functon-query-I-can-t-get-the-ValueSource-when-extend-ValueSourceParser-tp4103026.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: In a functon query, I can't get the ValueSource when extend ValueSourceParser

Posted by sling <sl...@gmail.com>.
Thank you, kydryavtsev andrey!
You give me the right solution.



--
View this message in context: http://lucene.472066.n3.nabble.com/In-a-functon-query-I-can-t-get-the-ValueSource-when-extend-ValueSourceParser-tp4103026p4103449.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: In a functon query, I can't get the ValueSource when extend ValueSourceParser

Posted by kydryavtsev andrey <we...@yandex.ru>.
As your DateSourceParser is put in standardValueSourceParsers map with key "dateDeboost" (right?), I think you need specify name of your source like "dateDeboost(title)". 

26.11.2013, 06:46, "sling" <sl...@gmail.com>:
> Thanks a lot for your reply, Chris.
>
> I was trying to sort the query result by the Datefunction, by passing
> q={!boost b=dateDeboost()}title:test to the /select request-handler.
>
> Before, my custom DateFunction is like this:
> public class DateFunction extends FieldCacheSource {
>         private static final long serialVersionUID = 6752223682280098130L;
>         private static long now;
>         public DateFunction(String field) {
>                 super(field);
>                 now = System.currentTimeMillis();
>         }
>         @Override
>         public FunctionValues getValues(Map context,
>                         AtomicReaderContext readerContext) throws IOException {
>                 long[] times = cache.getLongs(readerContext.reader(), field, false);
>                 final float[] weights = new float[times.length];
>                 for (int i = 0; i < times.length; i++) {
>                         weights[i] = ScoreUtils.getNewsScoreFactor(now, times[i]);
>                 }
>                 return new FunctionValues() {
>                         @Override
>                         public float floatVal(int doc) {
>                                 return weights[doc];
>                         }
>                 };
>         }
> }
> It calculate every documet's date-weight, but at the same time , it only
> need the one doc's date-weight, so it run slowly.
>
> When I see the source code of recip function in
> org.apache.solr.search.ValueSourceParser, like this:
> addParser("recip", new ValueSourceParser() {
>       @Override
>       public ValueSource parse(FunctionQParser fp) throws SyntaxError {
>         ValueSource source = fp.parseValueSource();
>         float m = fp.parseFloat();
>         float a = fp.parseFloat();
>         float b = fp.parseFloat();
>         return new ReciprocalFloatFunction(source, m, a, b);
>       }
> });
> and in the ReciprocalFloatFunction, it get the value like this:
> @Override
>   public FunctionValues getValues(Map context, AtomicReaderContext
> readerContext) throws IOException {
>     final FunctionValues vals = source.getValues(context, readerContext);
>     return new FloatDocValues(this) {
>       @Override
>       public float floatVal(int doc) {
>         return a/(m*vals.floatVal(doc) + b);
>       }
>       @Override
>       public String toString(int doc) {
>         return Float.toString(a) + "/("
>                 + m + "*float(" + vals.toString(doc) + ')'
>                 + '+' + b + ')';
>       }
>     };
>   }
>
> So I think this is what I want.
> When calculate a doc's date-weight, I needn't "cache.getLongs(xxxxx)",
> instead, I should "source.getValues(xxx)"
>
> Therefore I change my code, but when fp.parseValueSource(), it throws an
> error like this:
> org.apache.solr.search.SyntaxError: Expected identifier at pos 12
> str='dateDeboost()'
>
> Do I describe clearly this time?
>
> Thanks again!
>
> sling
>
> --
> View this message in context: http://lucene.472066.n3.nabble.com/In-a-functon-query-I-can-t-get-the-ValueSource-when-extend-ValueSourceParser-tp4103026p4103207.html
> Sent from the Solr - User mailing list archive at Nabble.com.

Re: In a functon query, I can't get the ValueSource when extend ValueSourceParser

Posted by sling <sl...@gmail.com>.
Thanks a lot for your reply, Chris.

I was trying to sort the query result by the Datefunction, by passing
q={!boost b=dateDeboost()}title:test to the /select request-handler.

Before, my custom DateFunction is like this:
public class DateFunction extends FieldCacheSource {
	private static final long serialVersionUID = 6752223682280098130L;
	private static long now;
	public DateFunction(String field) {
		super(field);
		now = System.currentTimeMillis();
	}
	@Override
	public FunctionValues getValues(Map context,
			AtomicReaderContext readerContext) throws IOException {
		long[] times = cache.getLongs(readerContext.reader(), field, false);		
		final float[] weights = new float[times.length];
		for (int i = 0; i < times.length; i++) {
			weights[i] = ScoreUtils.getNewsScoreFactor(now, times[i]);
		}
		return new FunctionValues() {
			@Override
			public float floatVal(int doc) {
				return weights[doc];
			}
		};
	}
}
It calculate every documet's date-weight, but at the same time , it only
need the one doc's date-weight, so it run slowly. 

When I see the source code of recip function in
org.apache.solr.search.ValueSourceParser, like this:
addParser("recip", new ValueSourceParser() {
      @Override
      public ValueSource parse(FunctionQParser fp) throws SyntaxError {
        ValueSource source = fp.parseValueSource();
        float m = fp.parseFloat();
        float a = fp.parseFloat();
        float b = fp.parseFloat();
        return new ReciprocalFloatFunction(source, m, a, b);
      }
});
and in the ReciprocalFloatFunction, it get the value like this:
@Override
  public FunctionValues getValues(Map context, AtomicReaderContext
readerContext) throws IOException {
    final FunctionValues vals = source.getValues(context, readerContext);
    return new FloatDocValues(this) {
      @Override
      public float floatVal(int doc) {
        return a/(m*vals.floatVal(doc) + b);
      }
      @Override
      public String toString(int doc) {
        return Float.toString(a) + "/("
                + m + "*float(" + vals.toString(doc) + ')'
                + '+' + b + ')';
      }
    };
  }

So I think this is what I want. 
When calculate a doc's date-weight, I needn't "cache.getLongs(xxxxx)",
instead, I should "source.getValues(xxx)"

Therefore I change my code, but when fp.parseValueSource(), it throws an
error like this:
org.apache.solr.search.SyntaxError: Expected identifier at pos 12
str='dateDeboost()' 

Do I describe clearly this time?

Thanks again!

sling




--
View this message in context: http://lucene.472066.n3.nabble.com/In-a-functon-query-I-can-t-get-the-ValueSource-when-extend-ValueSourceParser-tp4103026p4103207.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: In a functon query, I can't get the ValueSource when extend ValueSourceParser

Posted by Chris Hostetter <ho...@fucit.org>.
I'm not sure i understand your question - largely because you've only 
provided a small sample of information aboutwhat you are doing, and not 
giving a full picture.

what are you actually trying to accomplish?  
With your custom ValueSourceParser, what input are you sending to solr 
that generates that error?  
what does your "DateFunction" do?

Best i can tell from the information provided, you've registered your 
DateSourceParser using the name 'dateDeboost' (just a guess, you never 
actaully said) and then you tried using it in a request in 
some way (boost function?) as 'dateDeboost()' (just guessing based on the 
error message)

In which case this error is entirely expected, because your parse 
implementation says that you expect your function to be passed as input 
another vlaue source -- but when you called your function (in the input 
string 'dateDeboost()') you didn't specify any arguments at all - let 
alone an input argument that could be evaluated as a nested ValueSource.




: Date: Mon, 25 Nov 2013 02:11:43 -0800 (PST)
: From: sling <sl...@gmail.com>
: Reply-To: solr-user@lucene.apache.org
: To: solr-user@lucene.apache.org
: Subject: In a functon query,
:     I can't get the ValueSource when extend ValueSourceParser
: 
: hi,
: I am working with solr4.1.
: When I don't parseValueSource, my function query works well. The code is
: like this:
: public class DateSourceParser extends ValueSourceParser {
: 	@Override
: 	public void init(NamedList namedList) {
: 	}
: 	@Override
: 	*public ValueSource parse(FunctionQParser fp) throws SyntaxError {		
: 		return new DateFunction();
: 	}*
: }
: 
: When I want to use the ValueSource, like this:
: public class DateSourceParser extends ValueSourceParser {
: 	@Override
: 	public void init(NamedList namedList) {
: 	}
: 	@Override
: 	*public ValueSource parse(FunctionQParser fp) throws SyntaxError {
: 		ValueSource source = fp.parseValueSource();
: 		return new DateFunction(source);
: 	}*
: }
: 
: fp.parseValueSource() throws an error like this:
: ERROR [org.apache.solr.core.SolrCore] -
: org.apache.solr.common.SolrException: org.apache.solr.search.SyntaxError:
: Expected identifier at pos 12 str='dateDeboost()'
:         at
: org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:147)
:         at
: org.apache.solr.handler.component.SearchHandler.handleRequestBody(SearchHandler.java:187)
:         at
: org.apache.solr.handler.RequestHandlerBase.handleRequest(RequestHandlerBase.java:135)
:         at org.apache.solr.core.SolrCore.execute(SolrCore.java:1816)
:         at
: org.apache.solr.servlet.SolrDispatchFilter.execute(SolrDispatchFilter.java:448)
:         at
: org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:269)
:         at
: com.caucho.server.dispatch.FilterFilterChain.doFilter(FilterFilterChain.java:70)
:         at
: com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:173)
:         at
: com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:229)
:         at
: com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:274)
:         at com.caucho.server.port.TcpConnection.run(TcpConnection.java:514)
:         at com.caucho.util.ThreadPool.runTasks(ThreadPool.java:527)
:         at com.caucho.util.ThreadPool.run(ThreadPool.java:449)
:         at java.lang.Thread.run(Thread.java:662)
: Caused by: org.apache.solr.search.SyntaxError: Expected identifier at pos 12
: str='dateDeboost()'
:         at
: org.apache.solr.search.QueryParsing$StrParser.getId(QueryParsing.java:747)
:         at
: org.apache.solr.search.QueryParsing$StrParser.getId(QueryParsing.java:726)
:         at
: org.apache.solr.search.FunctionQParser.parseValueSource(FunctionQParser.java:345)
:         at
: org.apache.solr.search.FunctionQParser.parseValueSource(FunctionQParser.java:223)
:         at
: org.sling.solr.custom.DateSourceParser.parse(DateSourceParser.java:24)
:         at
: org.apache.solr.search.FunctionQParser.parseValueSource(FunctionQParser.java:352)
:         at
: org.apache.solr.search.FunctionQParser.parse(FunctionQParser.java:68)
:         at org.apache.solr.search.QParser.getQuery(QParser.java:142)
:         at
: org.apache.solr.search.BoostQParserPlugin$1.parse(BoostQParserPlugin.java:61)
:         at org.apache.solr.search.QParser.getQuery(QParser.java:142)
:         at
: org.apache.solr.handler.component.QueryComponent.prepare(QueryComponent.java:117)
:         ... 13 more
: 
: 
: so, how to make fp.parseValueSource() work?
: 
: Thanks!!!
: 
: sling
: 
: 
: 
: 
: 
: --
: View this message in context: http://lucene.472066.n3.nabble.com/In-a-functon-query-I-can-t-get-the-ValueSource-when-extend-ValueSourceParser-tp4103026.html
: Sent from the Solr - User mailing list archive at Nabble.com.
: 

-Hoss