lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Uwe Schindler" <...@thetaphi.de>
Subject RE: Having a default constructor in Analyzers
Date Sun, 07 Feb 2010 18:15:12 GMT
Hi Sanne,

This is example code used e.g. in my applications. This method allows you to create an analyzer
via reflection for both cases: default ctor or with version:

  final Analyzer createAnalyzer(String className, Version matchVersion) throws Exception {
    final Class<? extends Analyzer> clazz = Class.forName(className).asSubclass(Analyzer.class);
    try {
      // first try to use a ctor with version parameter (needed for many new Analyzers that
have no default one anymore
      return clazz.getConstructor(Version.class).newInstance(matchVersion);
    } catch (NoSuchMethodException nsme) {
      // otherwise use default ctor
      return clazz.newInstance();
    }
  }

The method takes a Version parameter, that you should really use (and not LUCENE_CURRENT!!!).
You should add an extra configuration option in your software for a default version that is
used everywhere when you instantiate lucene objects (like query parser and so on). By this
you preserve the compatibility after upgrades.

Just add a option somewhere in your config files, and assign this to a final Version constant
in your application using Version.valueOf(StringFromFile).

Hope that helps,
Uwe

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


> -----Original Message-----
> From: Uwe Schindler [mailto:uwe@thetaphi.de]
> Sent: Sunday, February 07, 2010 6:54 PM
> To: java-dev@lucene.apache.org
> Subject: RE: Having a default constructor in Analyzers
> 
> Hi Sanne,
> 
> Exactly that usage we want to prevent. Using Version.LUCENE_CURRENT is
> the badest thing you can do if you want to later update your Lucene
> version and do not want to reindex all your indexes (see javadocs).
> 
> It is easy to modify your application to create analyzers even from
> config files using the reflection way. Just find a constructor taking
> Version and call newInstance() on it, not directly on the Class. It's
> just one line of code more.
> 
> Uwe
> 
> -----
> Uwe Schindler
> H.-H.-Meier-Allee 63, D-28213 Bremen
> http://www.thetaphi.de
> eMail: uwe@thetaphi.de
> 
> > -----Original Message-----
> > From: Sanne Grinovero [mailto:sanne.grinovero@gmail.com]
> > Sent: Sunday, February 07, 2010 6:33 PM
> > To: java-dev@lucene.apache.org
> > Subject: Having a default constructor in Analyzers
> >
> > Hello,
> > I've seen that some core Analyzers are now missing a default
> > constructor; this is preventing many applications to configure/load
> > Analyzers by reflection, which is a common use case to have Analyzers
> > chosen in configuration files.
> >
> > Would it be possible to add, for example, a constructor like
> >
> > public StandardAnalyzer() {
> >    this(Version.LUCENE_CURRENT);
> > }
> >
> > ?
> >
> > Of course more advanced use cases would need to pass parameters but
> > please make the advanced usage optional; I have now seen more than a
> > single project break because of this (and revert to older Lucene).
> >
> > Regards,
> > Sanne
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
> > For additional commands, e-mail: java-dev-help@lucene.apache.org
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-dev-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-dev-help@lucene.apache.org



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


Mime
View raw message