lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: PayloadAttribute behavior change between Lucene 2.9/3.0 and the trunk
Date Sat, 04 Dec 2010 23:19:57 GMT
All tokenfilters/tokenizers in a chain share the same attributes. When you
add an attribute to one of them, they all share the same instance. That's
the whole trick behind the analysis API. getAttribute() should only be used
in conditional code, that check with has Attribute before. But In general,
producing and consuming attributes should always use addAttribute, then you
don't need to take care if its there or not.

-----
Uwe Schindler
H.-H.-Meier-Allee 63, D-28213 Bremen
http://www.thetaphi.de
eMail: uwe@thetaphi.de

> -----Original Message-----
> From: Teruhiko Kurosaka [mailto:Kuro@basistech.com]
> Sent: Sunday, December 05, 2010 12:05 AM
> To: java-user@lucene.apache.org
> Subject: Re: PayloadAttribute behavior change between Lucene 2.9/3.0 and
> the trunk
> 
> Thank you, Robert, substituting getAttribute with addAttribute worked!
> 
> But I don't understand why.  Could you help me to understand the
> mechanics?
> 
> In my setting,
> hasAttribute(PayloadAttribute.class) returns false.
> 
> So I thought addAttribute(PayloadAttribute.class) would just create a new
> PayloadAttribute object.  It would remedy the Exception, but it wouldn't
do
> any good accessing the payload generated upstream.
> 
> But the newly generated PayloadAttribute t is actually getting the payload
> that was generated upstream (by my Tokenizer).
> How is this possible?
> 
> 
> On Dec 3, 2010, at 7:30 PM, Robert Muir wrote:
> 
> > On Fri, Dec 3, 2010 at 10:15 PM, Teruhiko Kurosaka <Kuro@basistech.com>
> wrote:
> >> Hello,
> >> I have a Tokenizer that generates a Payload, and a TokenFilter that
uses it.
> >> These work well with Solr 1.4.0 (therefore Lucene 2.9.1?), but when
> >> I switched to the trunk version (I rebuilt the Tokenizer and
TokenFilter
> >> using the Lucene jar from the trunk and ran it), I encountered with
> >> this error:
> >>
> >> java.lang.IllegalArgumentException: This AttributeSource does not have
> the attribute
'org.apache.lucene.analysis.tokenattributes.PayloadAttribute'.
> >>
> >> This exception comes from this line in the TokenFilter code:
> >>
> >>        payloadAtt = (PayloadAttribute)
getAttribute(PayloadAttribute.class);
> >>
> >
> > I recommend you use addAttribute instead. Its buggy to use
> > getAttribute in this way because you cannot rely upon the fact that a
> > previous tokenstream has added the attribute[1]. I think your code was
> > only working before because of TokenWrapperAttributeFactory (the
> > backwards compatibility layer in 2.9 for the old Token API) being
> > present.
> >
> > [1]
> http://lucene.apache.org/java/2_9_0/api/core/org/apache/lucene/util/Attri
> buteSource.html#getAttribute(java.lang.Class)
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> > For additional commands, e-mail: java-user-help@lucene.apache.org
> >
> 
> ----
> T. "Kuro" Kurosaka, 415-227-9600x122, 617-386-7122(direct)
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message