Class Analyzer
- Namespace
- TantivyDotNet
- Assembly
- TantivyDotNet.dll
A custom text analyzer: a tokenizer that splits text into tokens, followed by filters that transform or drop tokens. Register it with AddAnalyzer(string, Analyzer) and use its name as a text field's tokenizer.
public sealed class Analyzer : IEquatable<Analyzer>
- Inheritance
-
Analyzer
- Implements
- Inherited Members
Examples
var schema = new SchemaBuilder()
.AddAnalyzer("en_text", Analyzer.Simple().LowerCase().AsciiFolding().StopWords(AnalyzerLanguage.English).Stem(AnalyzerLanguage.English))
.AddAnalyzer("prefix", Analyzer.NGram(1, 10, prefixOnly: true).LowerCase())
.AddTextField("body", tokenizer: "en_text")
.AddTextField("title_prefix", tokenizer: "prefix")
.Build();
Remarks
Analyzers are immutable: each filter method returns a new analyzer. Filters run in the order they are added, so add LowerCase() before StopWords(AnalyzerLanguage) and Stem(AnalyzerLanguage), which expect lowercase input. An index created in a directory saves its analyzers there, and Open(string) restores them.
Methods
- AsciiFolding()
Converts accented and other non-ASCII letters to their closest ASCII form (
café→cafe).
- Equals(object?)
Determines whether the specified object is equal to the current object.
- Equals(Analyzer?)
Indicates whether the current object is equal to another object of the same type.
- GetHashCode()
Serves as the default hash function.
- LowerCase()
Lowercases tokens.
- NGram(int, int, bool)
Emits every substring of
minGramtomaxGramcharacters (n-grams), for substring and search-as-you-type matching. Whitespace isn't special: n-grams span it.
- Raw()
Keeps the whole value as one token. Combine with LowerCase() for case-insensitive exact matching.
- Regex(string)
Emits every match of
patternas a token (Rustregexsyntax, which is close to .NET's but has no lookaround or backreferences). For example\w+or[A-Za-z0-9_-]+.
- RemoveLong(int)
Drops tokens longer than
maxBytesUTF-8 bytes (thedefaulttokenizer uses 40).
- Simple()
Splits on every character that isn't a letter or digit (like the
defaulttokenizer, without its filters).
- Stem(AnalyzerLanguage)
Reduces tokens to their stem with the Snowball stemmer for
language(running→run). Expects lowercase tokens.
- StopWords(IEnumerable<string>)
Drops tokens equal to any of
words(exact, case-sensitive match).
- StopWords(AnalyzerLanguage)
Drops the common words of
language(Tantivy's built-in lists). Expects lowercase tokens. Not every language has a list (see AnalyzerLanguage); a missing one fails when the schema is used to create an index.
- ToString()
A readable description, for example
simple | lowercase | stem(english).
- Whitespace()
Splits on whitespace only, so punctuation stays part of tokens.