madrob commented on a change in pull request #1467:
URL: https://github.com/apache/lucene-solr/pull/1467#discussion_r417658589



##########
File path: lucene/core/src/java/org/apache/lucene/search/FuzzyQuery.java
##########
@@ -237,22 +216,9 @@ public boolean equals(Object obj) {
     if (getClass() != obj.getClass())
       return false;
     FuzzyQuery other = (FuzzyQuery) obj;
-    // Note that we don't need to compare termLength or automata because they
-    // are entirely determined by the other fields
-    if (maxEdits != other.maxEdits)
-      return false;
-    if (prefixLength != other.prefixLength)
-      return false;
-    if (maxExpansions != other.maxExpansions)
-      return false;
-    if (transpositions != other.transpositions)
-      return false;
-    if (term == null) {
-      if (other.term != null)
-        return false;
-    } else if (!term.equals(other.term))
-      return false;
-    return true;
+    return Objects.equals(maxEdits, other.maxEdits) && 
Objects.equals(prefixLength, other.prefixLength)

Review comment:
       Is there a school of thought that we want to compare most likely to 
differ objects first, competing with the school that advocates comparing 
primitive types first because they are faster?

##########
File path: 
lucene/core/src/java/org/apache/lucene/search/FuzzyAutomatonBuilder.java
##########
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import org.apache.lucene.util.UnicodeUtil;
+import org.apache.lucene.util.automaton.CompiledAutomaton;
+import org.apache.lucene.util.automaton.LevenshteinAutomata;
+import org.apache.lucene.util.automaton.TooComplexToDeterminizeException;
+
+/**
+ * Builds a set of CompiledAutomaton for fuzzy matching on a given term,
+ * with specified maximum edit distance, fixed prefix and whether or not
+ * to allow transpositions.
+ */
+class FuzzyAutomatonBuilder {
+
+  private final String term;
+  private final int maxEdits;
+  private final LevenshteinAutomata levBuilder;
+  private final String prefix;
+  private final int termLength;
+
+  FuzzyAutomatonBuilder(String term, int maxEdits, int prefixLength, boolean 
transpositions) {
+    if (maxEdits < 0 || maxEdits > 
LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE) {
+      throw new IllegalArgumentException("max edits must be 0.." + 
LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + ", inclusive; got: " + 
maxEdits);
+    }
+    if (prefixLength < 0) {
+      throw new IllegalArgumentException("prefixLength cannot be less than 0");
+    }
+    this.term = term;
+    this.maxEdits = maxEdits;
+    int[] codePoints = stringToUTF32(term);
+    this.termLength = codePoints.length;
+    prefixLength = Math.min(prefixLength, codePoints.length);
+    int[] suffix = new int[codePoints.length - prefixLength];
+    System.arraycopy(codePoints, prefixLength, suffix, 0, suffix.length);
+    this.levBuilder = new LevenshteinAutomata(suffix, 
Character.MAX_CODE_POINT, transpositions);
+    this.prefix = UnicodeUtil.newString(codePoints, 0, prefixLength);
+  }
+
+  CompiledAutomaton[] buildAutomatonSet() {
+    CompiledAutomaton[] compiled = new CompiledAutomaton[maxEdits + 1];
+    for (int i = 0; i <= maxEdits; i++) {
+      try {
+        compiled[i] = new CompiledAutomaton(levBuilder.toAutomaton(i, prefix), 
true, false);
+      }
+      catch (TooComplexToDeterminizeException e) {
+        throw new FuzzyTermsEnum.FuzzyTermsException(term, e);
+      }
+    }
+    return compiled;
+  }
+
+  CompiledAutomaton buildMaxEditAutomaton() {

Review comment:
       I'm confused about the difference between when we would want the full 
automaton set and when we want the max edit automaton. When is one useful but 
not the other? Is this a simple optimization to skip building the relatively 
inexpensive (exponentially less expensive, even) fewer edit automata?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to