-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Backport] Added files for implementing index_prefix (#823)
Added files for implementing index_prefix
- Loading branch information
Showing
14 changed files
with
2,536 additions
and
1,644 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/yelp/nrtsearch/server/luceneserver/analysis/PrefixWrappedAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2025 Yelp Inc. | ||
* | ||
* Licensed 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 com.yelp.nrtsearch.server.luceneserver.analysis; | ||
|
||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.analysis.AnalyzerWrapper; | ||
import org.apache.lucene.analysis.TokenFilter; | ||
import org.apache.lucene.analysis.ngram.EdgeNGramTokenFilter; | ||
|
||
/** | ||
* An {@link AnalyzerWrapper} that wraps another analyzer and applies an Edge N-Gram token filter to | ||
* the token stream. | ||
*/ | ||
public class PrefixWrappedAnalyzer extends AnalyzerWrapper { | ||
private final int minChars; | ||
private final int maxChars; | ||
private final Analyzer delegate; | ||
|
||
/** | ||
* Create a new {@link PrefixWrappedAnalyzer} that wraps the given {@link Analyzer} and sets | ||
* applies an Edge N-Gram token filter to the token stream. | ||
* | ||
* @param delegate the analyzer to wrap | ||
* @param minChars the minimum number of characters for the edge n-grams | ||
* @param maxChars the maximum number of characters for the edge n-grams | ||
*/ | ||
public PrefixWrappedAnalyzer(Analyzer delegate, int minChars, int maxChars) { | ||
super(delegate.getReuseStrategy()); | ||
this.delegate = delegate; | ||
this.minChars = minChars; | ||
this.maxChars = maxChars; | ||
} | ||
|
||
@Override | ||
protected Analyzer getWrappedAnalyzer(String fieldName) { | ||
return delegate; | ||
} | ||
|
||
@Override | ||
protected TokenStreamComponents wrapComponents( | ||
String fieldName, TokenStreamComponents components) { | ||
TokenFilter filter = | ||
new EdgeNGramTokenFilter(components.getTokenStream(), minChars, maxChars, false); | ||
return new TokenStreamComponents(components.getSource(), filter); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "PrefixWrappedAnalyzer(" + delegate.toString() + ")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/main/java/com/yelp/nrtsearch/server/luceneserver/field/PrefixFieldDef.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2025 Yelp Inc. | ||
* | ||
* Licensed 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 com.yelp.nrtsearch.server.luceneserver.field; | ||
|
||
import com.yelp.nrtsearch.server.grpc.Field; | ||
import com.yelp.nrtsearch.server.grpc.PrefixQuery; | ||
import com.yelp.nrtsearch.server.luceneserver.analysis.PrefixWrappedAnalyzer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import org.apache.lucene.analysis.Analyzer; | ||
import org.apache.lucene.document.FieldType; | ||
import org.apache.lucene.index.IndexOptions; | ||
import org.apache.lucene.index.Term; | ||
import org.apache.lucene.search.*; | ||
import org.apache.lucene.util.automaton.Automata; | ||
import org.apache.lucene.util.automaton.Automaton; | ||
import org.apache.lucene.util.automaton.Operations; | ||
|
||
public class PrefixFieldDef extends TextBaseFieldDef { | ||
private final int minChars; | ||
private final int maxChars; | ||
private final String parentField; | ||
private static final String INDEX_PREFIX = "._index_prefix"; | ||
|
||
public PrefixFieldDef(String parentName, Field requestField) { | ||
super(parentName + INDEX_PREFIX, requestField); | ||
this.minChars = requestField.getIndexPrefixes().getMinChars(); | ||
this.maxChars = requestField.getIndexPrefixes().getMaxChars(); | ||
this.parentField = parentName; | ||
} | ||
|
||
@Override | ||
protected void setSearchProperties(FieldType fieldType, Field requestField) { | ||
fieldType.setOmitNorms(true); | ||
fieldType.setTokenized(true); | ||
fieldType.setIndexOptions(IndexOptions.DOCS); | ||
} | ||
|
||
@Override | ||
protected Analyzer parseIndexAnalyzer(Field requestField) { | ||
Analyzer baseAnalyzer = super.parseIndexAnalyzer(requestField); | ||
if (baseAnalyzer == null) { | ||
throw new IllegalArgumentException("Could not determine analyzer"); | ||
} | ||
return new PrefixWrappedAnalyzer( | ||
baseAnalyzer, | ||
requestField.getIndexPrefixes().getMinChars(), | ||
requestField.getIndexPrefixes().getMaxChars()); | ||
} | ||
|
||
boolean accept(int length) { | ||
return length >= minChars - 1 && length <= maxChars; | ||
} | ||
|
||
public Query getPrefixQuery(PrefixQuery prefixQuery, MultiTermQuery.RewriteMethod rewriteMethod) { | ||
String textValue = prefixQuery.getPrefix(); | ||
if (textValue.length() >= minChars) { | ||
return super.getTermQueryFromTextValue(textValue); | ||
} | ||
List<Automaton> automata = new ArrayList<>(); | ||
automata.add(Automata.makeString(textValue)); | ||
for (int i = textValue.length(); i < minChars; i++) { | ||
automata.add(Automata.makeAnyChar()); | ||
} | ||
Automaton automaton = Operations.concatenate(automata); | ||
AutomatonQuery query = new AutomatonQuery(new Term(getName(), textValue + "*"), automaton); | ||
query.setRewriteMethod(rewriteMethod); | ||
return new BooleanQuery.Builder() | ||
.add(query, BooleanClause.Occur.SHOULD) | ||
.add(new TermQuery(new Term(parentField, textValue)), BooleanClause.Occur.SHOULD) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String getType() { | ||
return "PREFIX"; | ||
} | ||
|
||
public int getMinChars() { | ||
return minChars; | ||
} | ||
|
||
public int getMaxChars() { | ||
return maxChars; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), minChars, maxChars, parentField); | ||
} | ||
} |
Oops, something went wrong.