On Tue, 11 Dec 2007, Timo Nentwig wrote:

Date: Tue, 11 Dec 2007 13:27:59 +0100
From: Timo Nentwig <[EMAIL PROTECTED]>
Reply-To: java-dev@lucene.apache.org
To: java-dev@lucene.apache.org
Subject: Caching FuzzyQuery

Hi!

Actually FuzzyQuery.rewrite() is pretty expensive so why not introduce a
caching decorator? A WeakHashMap with key==IndexReader and value==LRU of
BooleanQueries.

So, didn't become a popular approach/topic. Anyway, I wrote an aspect and it does the job for a couple of months now, so I'd like to share it.

HTH
Timo


In order to enable runtime AspectJ weaving start your JVM with

   -javaagent:./foo/bar/lib/aspectjweaver.jar

and place the following file called "aop.xml" in META-INF.

<aspectj>
        <aspects>
                <aspect name="foo.bar.FuzzyQueryCachingAspect" />
        </aspects>

        <weaver options="-verbose -showWeaveInfo">
                <include within="org.apache.lucene.search.*" />
                <include within="org.apache.lucene.index.*" />
        </weaver>
</aspectj>


@Aspect
public class FuzzyQueryCachingAspect
{
private static final Log LOG = LogFactory.getLog( FuzzyQueryCachingAspect.class );
        // your cache impl goes here
private static final Caching CACHE = Caching.instance( Caching.FUZZY );

@Before( "execution(public void org.apache.lucene.index.IndexReader+.close())" )
        public void flush( final JoinPoint jp ) throws Throwable
        {
                assert jp.getThis() == jp.getTarget();

                final IndexReader r = (IndexReader)jp.getThis();
                final Directory d = r.directory();

                final String group = name( d );

                CACHE.remove( group );
                LOG.info( "Removed from " + CACHE + ": " + group );
        }

        @Around( "execution(public org.apache.lucene.search.Query
org.apache.lucene.search.FuzzyQuery.rewrite(org.apache.lucene.index.IndexReader)) && args(reader)" ) public Object cache( final IndexReader reader, final ProceedingJoinPoint jp ) throws Throwable
        {
                assert jp.getThis() == jp.getTarget();

                final FuzzyQuery q = (FuzzyQuery)jp.getThis();
                final String term = q.getTerm().text();

                final String group = name( reader.directory() );
                final String key = term + "~" + q.getMinSimilarity();

                final Object e = CACHE.get( group, key );
                if( e != null )
                {
                        LOG.debug( term + " found in cache" );
                        return e;
                }

                final long t0 = System.currentTimeMillis();
                final Query fuzzy = (Query)jp.proceed();
                CACHE.put( group, key, fuzzy );
LOG.info( term + " rewritten in " + (System.currentTimeMillis() - t0) + "ms (" + group + ")" );

                return fuzzy;
        }

        private static final String name( final Directory d )
        {
if( d instanceof FSDirectory ) return ((FSDirectory)d).getFile().getAbsolutePath();

                return String.valueOf( d.hashCode() );
        }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to