arunkumarucet commented on code in PR #20292: URL: https://github.com/apache/pinot/pull/20292#discussion_r4663987172
########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/text/AlphanumericAnalyzer.java: ########## @@ -0,0 +1,106 @@ +/** + * 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.pinot.segment.local.segment.index.text; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.LowerCaseFilter; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.analysis.Tokenizer; +import org.apache.lucene.analysis.util.CharTokenizer; + + +/// An [Analyzer] that splits on every non-alphanumeric character and, by default, lowercases the +/// result — so `email_server.rb` yields `email`, `server`, `rb`, and `HTTP2` yields `http2`. +/// +/// This fills a gap between the two analyzers Pinot already offers for log and identifier-heavy +/// text, neither of which tokenizes the way most search engines do: +/// +/// | Analyzer | `send_email` | `200` | `1YMWWN1N4O` | +/// |---|---|---|---| +/// | [org.apache.lucene.analysis.standard.StandardAnalyzer] (default) | `send_email` | `200` | `1ymwwn1n4o` | +/// | [org.apache.lucene.analysis.core.SimpleAnalyzer] | `send`, `email` | *(none)* | `ymwwn`, `n`, `o` | +/// | `AlphanumericAnalyzer` | `send`, `email` | `200` | `1ymwwn1n4o` | +/// +/// [org.apache.lucene.analysis.standard.StandardAnalyzer] follows UAX#29, which keeps +/// `send_email` and `email_server.rb` whole, so a search for `email` does not match them. +/// [org.apache.lucene.analysis.core.SimpleAnalyzer] splits on non-letters, but because it is built +/// on [org.apache.lucene.analysis.core.LetterTokenizer] every digit is a *separator* and is +/// dropped: `200` and `8080` produce no terms at all and can never be matched, `http2` is indexed +/// as `http`, and an identifier such as `1YMWWN1N4O` shatters into letter fragments. Pinot applies +/// the configured analyzer to queries as well as to documents, so the term a user searches for is +/// discarded in exactly the same way. +/// +/// This analyzer is the behaviour of Elasticsearch's `char_group` tokenizer over whitespace, +/// punctuation and symbols plus `lowercase`, of ClickHouse's `splitByNonAlpha`, and of a +/// `[a-zA-Z0-9]+` pattern tokenizer. It is a good default for logs, URLs, stack traces, and +/// anything carrying status codes, ports, versions or generated identifiers. +/// +/// Token characters are decided by [Character#isLetterOrDigit(int)], so letters and digits outside +/// ASCII — accented letters, CJK ideographs, Arabic-Indic digits — are kept rather than treated as +/// separators. +/// +/// Select it per column with `luceneAnalyzerClass`, optionally preserving case: +/// +/// ```json +/// "properties": { +/// "luceneAnalyzerClass": "org.apache.pinot.segment.local.segment.index.text.AlphanumericAnalyzer", +/// "luceneAnalyzerClassArgs": "true", +/// "luceneAnalyzerClassArgTypes": "java.lang.Boolean.TYPE" +/// } +/// ``` +/// +/// Instances are immutable and safe for concurrent use; per-thread token streams come from +/// [Analyzer]'s reuse strategy. +public class AlphanumericAnalyzer extends Analyzer { + + private final boolean _caseSensitive; + + /// Builds a case-insensitive analyzer, matching the default of + /// [org.apache.lucene.analysis.standard.StandardAnalyzer]. + public AlphanumericAnalyzer() { + this(false); + } + + /// Builds an analyzer that preserves the original case when `caseSensitive` is `true`. + /// + /// @param caseSensitive whether tokens keep their original case + public AlphanumericAnalyzer(boolean caseSensitive) { + _caseSensitive = caseSensitive; + } + + /// Returns true if this analyzer preserves the original case of its tokens. + public boolean isCaseSensitive() { + return _caseSensitive; + } + + @Override + protected TokenStreamComponents createComponents(String fieldName) { + Tokenizer tokenizer = CharTokenizer.fromTokenCharPredicate(Character::isLetterOrDigit); Review Comment: Good catch — confirmed on Lucene 9.12.0. `CharTokenizer.fromTokenCharPredicate` has no max-length overload, so it inherits `DEFAULT_MAX_WORD_LEN` = 255, and an oversized run is **split**, not truncated or dropped: `"A".repeat(255) + "BC"` indexes as `[a*255, "bc"]`, and `"a".repeat(510)` as two 255-char terms. Made the policy explicit rather than leaving it an inherited default: - Added `MAX_TOKEN_LENGTH`, documented as splitting at the boundary, and noted it is the same value and policy as `StandardAnalyzer.DEFAULT_MAX_TOKEN_LENGTH` (also 255), so this matches what Pinot tables already get today. - Documented both consequences you identified: the tail of an oversized run becomes a term of its own, so a 257-char identifier is matched by a term query for its last two characters; and a prefix/wildcard/fuzzy query longer than the limit can never match, because `normalize()` leaves it as one term and no indexed term is longer. Two tests: `testTokenLengthBoundary` for tokenization at 255/256/257/510, and `testLongIdentifierTermAndPrefixSearch`, which builds a real index and asserts term queries for the head and the `bc` tail both hit, the full identifier as a single term misses, and prefix queries at 254/255 hit while 256 misses. Both die if `MAX_TOKEN_LENGTH` is off by one. I kept 255 rather than exposing it as a constructor arg, since matching the `StandardAnalyzer` default seemed better than adding a second config dimension. Happy to make it configurable if you would rather have the knob. ########## pinot-segment-local/src/main/java/org/apache/pinot/segment/local/segment/index/text/AlphanumericAnalyzer.java: ########## @@ -0,0 +1,106 @@ +/** + * 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.pinot.segment.local.segment.index.text; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.LowerCaseFilter; +import org.apache.lucene.analysis.TokenStream; +import org.apache.lucene.analysis.Tokenizer; +import org.apache.lucene.analysis.util.CharTokenizer; + + +/// An [Analyzer] that splits on every non-alphanumeric character and, by default, lowercases the +/// result — so `email_server.rb` yields `email`, `server`, `rb`, and `HTTP2` yields `http2`. +/// +/// This fills a gap between the two analyzers Pinot already offers for log and identifier-heavy +/// text, neither of which tokenizes the way most search engines do: +/// +/// | Analyzer | `send_email` | `200` | `1YMWWN1N4O` | +/// |---|---|---|---| +/// | [org.apache.lucene.analysis.standard.StandardAnalyzer] (default) | `send_email` | `200` | `1ymwwn1n4o` | +/// | [org.apache.lucene.analysis.core.SimpleAnalyzer] | `send`, `email` | *(none)* | `ymwwn`, `n`, `o` | +/// | `AlphanumericAnalyzer` | `send`, `email` | `200` | `1ymwwn1n4o` | +/// +/// [org.apache.lucene.analysis.standard.StandardAnalyzer] follows UAX#29, which keeps +/// `send_email` and `email_server.rb` whole, so a search for `email` does not match them. +/// [org.apache.lucene.analysis.core.SimpleAnalyzer] splits on non-letters, but because it is built +/// on [org.apache.lucene.analysis.core.LetterTokenizer] every digit is a *separator* and is +/// dropped: `200` and `8080` produce no terms at all and can never be matched, `http2` is indexed +/// as `http`, and an identifier such as `1YMWWN1N4O` shatters into letter fragments. Pinot applies +/// the configured analyzer to queries as well as to documents, so the term a user searches for is +/// discarded in exactly the same way. +/// +/// This analyzer is the behaviour of Elasticsearch's `char_group` tokenizer over whitespace, +/// punctuation and symbols plus `lowercase`, of ClickHouse's `splitByNonAlpha`, and of a +/// `[a-zA-Z0-9]+` pattern tokenizer. It is a good default for logs, URLs, stack traces, and +/// anything carrying status codes, ports, versions or generated identifiers. +/// +/// Token characters are decided by [Character#isLetterOrDigit(int)], so letters and digits outside +/// ASCII — accented letters, CJK ideographs, Arabic-Indic digits — are kept rather than treated as +/// separators. +/// +/// Select it per column with `luceneAnalyzerClass`, optionally preserving case: Review Comment: Verified both halves of this and you are right. `TextIndexHandler.hasTextIndexConfigurationChanged` compares only `isStoreInSegmentFile()` against the on-disk format — it never looks at the analyzer, so a config-only switch does not trigger a rebuild on reload. And `LuceneTextIndexReader` calls `TextIndexUtils.getUpdatedConfigFromPropertiesFile(propertiesFile, config)` before `getAnalyzer(config)`, so an existing index restores the analyzer it was built with in preference to the current table config. The net effect is exactly what you described, and it is per-segment: older segments keep their old tokenization and still miss `200`, while newly built segments match it. Added a "Switching an existing column to this analyzer" section to the class doc stating that reload alone is not enough and that the text indexes must be rebuilt or the segments regenerated. -- 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]
