anil884 opened a new pull request, #17025:
URL: https://github.com/apache/lucene/pull/17025

   ### Description
   
   `FuzzyQuery` rebuilds its Levenshtein automata every time it is rewritten. 
They are shared across the segments of one rewrite, but the same token searched 
on another field, in another clause or in the next request builds them again. 
For fuzzy autocomplete over several fields this is a large part of the rewrite 
cost.
   
   This PR adds an opt-in `FuzzyAutomatonCache` that an application can pass to 
`FuzzyQuery`:
   
   - **Key:** (term text, maxEdits, prefixLength, transpositions). The field is 
not part of the key because the automata do not depend on it, so one build 
serves every field.
   - **Bounds:** LRU, limited by entry count and by estimated RAM 
(`CompiledAutomaton#ramBytesUsed`). Hit, miss and eviction counters are exposed.
   - **Opt-in, no static state:** the application owns the instance. Queries 
built without a cache behave exactly as before.
   - **Queries stay small:** a `FuzzyQuery` only holds a reference to the 
cache, and the cache is not part of `equals`/`hashCode`, so query-keyed caches 
are unaffected.
   - **Index independent:** nothing to invalidate when readers change.
   
   `FuzzyQuery#getAutomata` and `visit` (used by highlighting) also go through 
the cache when one is set.
   
   #### Thread-safety fix in `CompiledAutomaton#floor`
   
   `floor` used a shared scratch `Transition` field. That was harmless while an 
instance was only used by one rewrite, but a cached instance is shared between 
threads, and `FuzzyTermsEnum` calls `floor` when it lowers its edit distance. 
The scratch object is now local to the call.
   
   #### Benchmark
   
   `FuzzyQueryBenchmark` (JMH, included): 1M unique terms, one segment, average 
microseconds per `IndexSearcher#rewrite`. `rewriteWithCache` is the best case, 
where every lookup hits.
   
   | maxEdits | term length | build one automaton | rewrite | rewriteWithCache 
| saved |
   |---|---|---|---|---|---|
   | 1 | 4 | 21 | 97 | 72 | 26% |
   | 1 | 8 | 51 | 116 | 59 | 49% |
   | 1 | 12 | 74 | 146 | 60 | 59% |
   | 2 | 4 | 194 | 368 | 126 | 66% |
   | 2 | 8 | 600 | 1829 | 1116 | 39% |
   | 2 | 12 | 975 | 2214 | 1114 | 50% |
   
   These come from a short run (1 fork, 3 iterations) on an Apple Silicon 
laptop with JDK 25. With a query stream where half of the lookups hit the 
cache, the saving measured locally was 13% to 30%.
   
   #### Tests
   
   `TestFuzzyAutomatonCache` covers LRU and RAM eviction, key normalization, 
sharing across fields, identical results with and without a cache on random 
queries, and concurrent searches sharing automata.
   
   #### Not in this PR
   
   Query parser support, the other `FuzzyTermsEnum` callers 
(`DirectSpellChecker`, `FuzzyLikeThisQuery`, `NearestFuzzyQuery`) and the 
suggesters are left for follow-ups.
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to