xiangfu0 commented on code in PR #20292: URL: https://github.com/apache/pinot/pull/20292#discussion_r4663883361
########## 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 Review Comment: **[nit] Qualify the comparison with an ASCII pattern.** `[a-zA-Z0-9]+` extracts `Caf` from `Café`. This analyzer produces `café`. Could we limit the comparison to ASCII examples and describe the Unicode behavior in terms of `Character.isLetterOrDigit`? ########## 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: **[should-address] Document the rebuild required for existing columns.** `TextIndexHandler` compares the storage format when deciding whether to rebuild an existing single-column index, and `LuceneTextIndexReader` reads the saved analyzer config from the index. After a config-only switch from `SimpleAnalyzer`, historical segments can still miss `200` while newly built segments match it. Please tell users to rebuild the existing text indexes or regenerate their segments when switching analyzers. ########## 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: **[should-address] Define how long identifiers are indexed.** The `CharTokenizer` factory silently splits a continuous ASCII identifier after 255 characters. With Lucene 9.12.0, indexing `"A".repeat(255) + "BC"` lets `BC` match the full identifier. A 256-character prefix query misses it because the parser normalizes the whole prefix as one term. Could we make the token length policy explicit and add a boundary test for both term and prefix searches? -- 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]
