-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Support configurable Lucene analyzer with args and configurable query parser #13003
base: master
Are you sure you want to change the base?
Changes from 16 commits
5ad8b05
09d53bb
23d8de3
bc84aee
610bfff
559548e
e72ff6e
7796f0e
0e62b97
fafdaa4
d97a383
0b99d64
e4023bf
5b43fff
2ba9812
8df04e6
3129017
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ | |||||
package org.apache.pinot.segment.local.segment.store; | ||||||
|
||||||
import java.io.File; | ||||||
import java.util.ArrayList; | ||||||
import java.util.Arrays; | ||||||
import java.util.Collections; | ||||||
import java.util.HashSet; | ||||||
|
@@ -32,12 +33,16 @@ | |||||
import org.apache.lucene.analysis.standard.StandardAnalyzer; | ||||||
import org.apache.pinot.segment.local.segment.creator.impl.text.LuceneTextIndexCreator; | ||||||
import org.apache.pinot.segment.spi.V1Constants.Indexes; | ||||||
import org.apache.pinot.segment.spi.index.TextIndexConfig; | ||||||
import org.apache.pinot.segment.spi.store.SegmentDirectoryPaths; | ||||||
import org.apache.pinot.spi.config.table.FSTType; | ||||||
import org.apache.pinot.spi.config.table.FieldConfig; | ||||||
import org.slf4j.Logger; | ||||||
import org.slf4j.LoggerFactory; | ||||||
|
||||||
|
||||||
public class TextIndexUtils { | ||||||
private static final Logger LOGGER = LoggerFactory.getLogger(TextIndexUtils.class); | ||||||
private TextIndexUtils() { | ||||||
} | ||||||
|
||||||
|
@@ -108,10 +113,140 @@ private static List<String> parseEntryAsString(@Nullable Map<String, String> col | |||||
.collect(Collectors.toList()); | ||||||
} | ||||||
|
||||||
public static Analyzer getAnalyzerFromClassName(String luceneAnalyzerClass) | ||||||
throws ReflectiveOperationException { | ||||||
// Support instantiation with default constructor for now unless customized | ||||||
return (Analyzer) Class.forName(luceneAnalyzerClass).getConstructor().newInstance(); | ||||||
public static Analyzer getAnalyzer(TextIndexConfig config) throws ReflectiveOperationException { | ||||||
String luceneAnalyzerClassName = config.getLuceneAnalyzerClass(); | ||||||
List<String> luceneAnalyzerClassArgs = config.getLuceneAnalyzerClassArgs(); | ||||||
List<String> luceneAnalyzerClassArgsTypes = config.getLuceneAnalyzerClassArgTypes(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
if (null == luceneAnalyzerClassName || luceneAnalyzerClassName.isEmpty() | ||||||
|| (luceneAnalyzerClassName.equals(StandardAnalyzer.class.getName()) | ||||||
&& luceneAnalyzerClassArgs.isEmpty() && luceneAnalyzerClassArgsTypes.isEmpty())) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// When there is no analyzer defined, or when StandardAnalyzer (default) is used without arguments, | ||||||
// use existing logic to obtain an instance of StandardAnalyzer with customized stop words | ||||||
return TextIndexUtils.getStandardAnalyzerWithCustomizedStopWords( | ||||||
config.getStopWordsInclude(), config.getStopWordsExclude()); | ||||||
} else { | ||||||
// Custom analyzer + custom configs via reflection | ||||||
if (luceneAnalyzerClassArgs.size() != luceneAnalyzerClassArgsTypes.size()) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
throw new ReflectiveOperationException("Mismatch of the number of analyzer arguments and arguments types."); | ||||||
} | ||||||
|
||||||
// Generate args type list | ||||||
List<Class<?>> argClasses = new ArrayList<>(); | ||||||
for (String argType : luceneAnalyzerClassArgsTypes) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
argClasses.add(parseSupportedTypes(argType)); | ||||||
} | ||||||
|
||||||
// Best effort coercion to the analyzer argument type | ||||||
// Note only a subset of class types is supported, unsupported ones can be added in the future | ||||||
List<Object> argValues = new ArrayList<>(); | ||||||
for (int i = 0; i < luceneAnalyzerClassArgs.size(); i++) { | ||||||
argValues.add(parseSupportedTypeValues(luceneAnalyzerClassArgs.get(i), argClasses.get(i))); | ||||||
} | ||||||
|
||||||
// Initialize the custom analyzer class with custom analyzer args | ||||||
Class<?> luceneAnalyzerClass = Class.forName(luceneAnalyzerClassName); | ||||||
if (!Analyzer.class.isAssignableFrom(luceneAnalyzerClass)) { | ||||||
String exceptionMessage = "Custom analyzer must be a child of " + Analyzer.class.getCanonicalName(); | ||||||
LOGGER.error(exceptionMessage); | ||||||
throw new ReflectiveOperationException(exceptionMessage); | ||||||
} | ||||||
|
||||||
// Return a new instance of custom lucene analyzer class | ||||||
return (Analyzer) luceneAnalyzerClass.getConstructor(argClasses.toArray(new Class<?>[0])) | ||||||
.newInstance(argValues.toArray(new Object[0])); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Parse the Java value type specified in the type string | ||||||
* @param valueTypeString FQCN of the value type class or the name of the primitive value type | ||||||
* @return Class object of the value type | ||||||
* @throws ClassNotFoundException when the value type is not supported | ||||||
*/ | ||||||
public static Class<?> parseSupportedTypes(String valueTypeString) throws ClassNotFoundException { | ||||||
try { | ||||||
// Support both primitive types + class | ||||||
switch (valueTypeString) { | ||||||
case "java.lang.Byte.TYPE": | ||||||
return Byte.TYPE; | ||||||
case "java.lang.Short.TYPE": | ||||||
return Short.TYPE; | ||||||
case "java.lang.Integer.TYPE": | ||||||
return Integer.TYPE; | ||||||
case "java.lang.Long.TYPE": | ||||||
return Long.TYPE; | ||||||
case "java.lang.Float.TYPE": | ||||||
return Float.TYPE; | ||||||
case "java.lang.Double.TYPE": | ||||||
return Double.TYPE; | ||||||
case "java.lang.Boolean.TYPE": | ||||||
return Boolean.TYPE; | ||||||
case "java.lang.Character.TYPE": | ||||||
return Character.TYPE; | ||||||
default: | ||||||
return Class.forName(valueTypeString); | ||||||
} | ||||||
} catch (ClassNotFoundException ex) { | ||||||
LOGGER.error("Analyzer argument class type not found: " + valueTypeString); | ||||||
throw ex; | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Attempt to coerce string into supported value type | ||||||
* @param stringValue string representation of the value | ||||||
* @param clazz of the value | ||||||
* @return class object of the value, auto-boxed if it is a primitive type | ||||||
* @throws ReflectiveOperationException if value cannot be coerced without ambiguity or encountered unsupported type | ||||||
*/ | ||||||
public static Object parseSupportedTypeValues(String stringValue, Class<?> clazz) | ||||||
throws ReflectiveOperationException { | ||||||
try { | ||||||
if (clazz.equals(String.class)) { | ||||||
return stringValue; | ||||||
} else if (clazz.equals(Byte.class) || clazz.equals(Byte.TYPE)) { | ||||||
return Byte.parseByte(stringValue); | ||||||
} else if (clazz.equals(Short.class) || clazz.equals(Short.TYPE)) { | ||||||
return Short.parseShort(stringValue); | ||||||
} else if (clazz.equals(Integer.class) || clazz.equals(Integer.TYPE)) { | ||||||
return Integer.parseInt(stringValue); | ||||||
} else if (clazz.equals(Long.class) || clazz.equals(Long.TYPE)) { | ||||||
return Long.parseLong(stringValue); | ||||||
} else if (clazz.equals(Float.class) || clazz.equals(Float.TYPE)) { | ||||||
return Float.parseFloat(stringValue); | ||||||
} else if (clazz.equals(Double.class) || clazz.equals(Double.TYPE)) { | ||||||
return Double.parseDouble(stringValue); | ||||||
} else if (clazz.equals(Boolean.class) || clazz.equals(Boolean.TYPE)) { | ||||||
// Note we cannot use Boolean.parseBoolean here because it treats "abc" as false which | ||||||
// introduces unexpected parsing results. We should validate the input by accepting only | ||||||
// true|false in a case-insensitive manner, for all other values, return an exception. | ||||||
String lowerCaseStringValue = stringValue.toLowerCase(); | ||||||
if (lowerCaseStringValue.equals("true")) { | ||||||
return true; | ||||||
} else if (lowerCaseStringValue.equals("false")) { | ||||||
return false; | ||||||
} | ||||||
throw new ReflectiveOperationException(); | ||||||
jackluo923 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} else if (clazz.equals(Character.class) || clazz.equals(Character.TYPE)) { | ||||||
if (stringValue.length() == 1) { | ||||||
return stringValue.charAt(0); | ||||||
} | ||||||
throw new ReflectiveOperationException(); | ||||||
jackluo923 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} else { | ||||||
throw new UnsupportedOperationException(); | ||||||
} | ||||||
} catch (NumberFormatException | ReflectiveOperationException ex) { | ||||||
jackluo923 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
String exceptionMessage = "Custom analyzer argument cannot be coerced from " | ||||||
+ stringValue + " to " + clazz.getName() + " type"; | ||||||
LOGGER.error(exceptionMessage); | ||||||
throw new ReflectiveOperationException(exceptionMessage); | ||||||
} catch (UnsupportedOperationException ex) { | ||||||
// In the future, consider adding more common serdes for common complex types used within Lucene | ||||||
String exceptionMessage = "Custom analyzer argument does not support " + clazz.getName() + " type"; | ||||||
LOGGER.error(exceptionMessage); | ||||||
throw new ReflectiveOperationException(exceptionMessage); | ||||||
} | ||||||
} | ||||||
|
||||||
public static StandardAnalyzer getStandardAnalyzerWithCustomizedStopWords(@Nullable List<String> stopWordsInclude, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a default constructor that doesn't take in any argument(s)