1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//! This library aims to bring some [Lucene](https://lucene.apache.org/core/) components into [Tantivy](https://github.com/quickwit-oss/tantivy)
//!
//! Currently it contains
//! * ICU related components :
//!     * [ICUTokenizer](crate::icu::ICUTokenizer) that is an equivalent
//! of [Lucene's ICUTokenizer](https://lucene.apache.org/core/9_0_0/analysis/icu/org/apache/lucene/analysis/icu/segmentation/ICUTokenizer.html)
//! without support of emojis.
//!     * [ICUNormalizer2TokenFilter](crate::icu::ICUNormalizer2TokenFilter) that normalize text. It is an equivalent of
//! [Lucene's ICUNormalizer2Filter](https://lucene.apache.org/core/9_0_0/analysis/icu/org/apache/lucene/analysis/icu/ICUNormalizer2Filter.html).
//!     * [ICUTransformTokenFilter](crate::icu::ICUTransformTokenFilter) which is an equivalent of
//! [Lucene's ICUTransformFilter](https://lucene.apache.org/core/9_0_0/analysis/icu/org/apache/lucene/analysis/icu/ICUNormalizer2Filter.html)
//! * Commons components :
//!     * [PathTokenizer](crate::commons::PathTokenizer) which tokenize a hierarchical path (equivalent of
//! [PathHierarchyTokenizer](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/path/PathHierarchyTokenizer.html) and
//! [ReversePathHierarchyTokenizer](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/path/ReversePathHierarchyTokenizer.html))
//!     * [LengthTokenFilter](crate::commons::LengthTokenFilter) that remove tokens which doesn't are above or below certain limits (see
//! [LengthFilter](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/miscellaneous/LengthFilter.html))
//!     * [LimitTokenCountFilter](crate::commons::LimitTokenCountFilter) that limits the number of token, see
//! [LimitTokenCountFilter](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/miscellaneous/LimitTokenCountFilter.html)
//!     * [ReverseTokenFilter](crate::commons::ReverseTokenFilter) that reverse a string see
//! [ReverseStringFilter](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/reverse/ReverseStringFilter.html)
//!     * [ElisionTokenFilter](crate::commons::ElisionTokenFilter) that remove elisions, see
//! [ElisionFilter](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/util/ElisionFilter.html)
//!     * [EdgeNgramTokenFilter](crate::commons::EdgeNgramTokenFilter) that generate ngram prefixes of tokens, see
//! [EdgeNGramTokenFilter](https://lucene.apache.org/core/9_1_0/analysis/common/org/apache/lucene/analysis/ngram/EdgeNGramTokenFilter.html)
//! * Phonetic :
//!     * [PhoneticTokenFilter](crate::phonetic::PhoneticTokenFilter) a token filter to apply phonetic algorithm on tokens.
//!
//! # Example
//!
//! Here is a full example of how tokenize using [icu::ICUTokenizer] and do transliteration and lowercase each tokens using [icu::ICUTransformTokenFilter]:
//! ```rust
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//!     use tantivy::collector::TopDocs;
//!     use tantivy::query::QueryParser;
//!     use tantivy::schema::{
//!         IndexRecordOption, SchemaBuilder, TextFieldIndexing, TextOptions, Value,
//!     };
//!     use tantivy::tokenizer::TextAnalyzer;
//!     use tantivy::{doc, Index, ReloadPolicy, TantivyDocument};
//!     use tantivy_analysis_contrib::icu::{Direction, ICUTokenizer, ICUTransformTokenFilter};
//!
//!     const ANALYSIS_NAME: &str = "test";
//!
//!     let options = TextOptions::default()
//!         .set_indexing_options(
//!             TextFieldIndexing::default()
//!                 .set_tokenizer(ANALYSIS_NAME)
//!                 .set_index_option(IndexRecordOption::WithFreqsAndPositions),
//!         )
//!         .set_stored();
//!     let mut schema = SchemaBuilder::new();
//!     schema.add_text_field("field", options);
//!     let schema = schema.build();
//!
//!     let transform = ICUTransformTokenFilter::new(
//!         "Any-Latin; NFD; [:Nonspacing Mark:] Remove; Lower;  NFC".to_string(),
//!         None,
//!         Direction::Forward,
//!     )?;
//!     let icu_analyzer = TextAnalyzer::builder(ICUTokenizer)
//!         .filter(transform)
//!         .build();
//!
//!     let field = schema.get_field("field").expect("Can't get field.");
//!
//!     let index = Index::create_in_ram(schema);
//!     index.tokenizers().register(ANALYSIS_NAME, icu_analyzer);
//!
//!     let mut index_writer = index.writer(15_000_000)?;
//!
//!     index_writer.add_document(doc!(
//!         field => "中国"
//!     ))?;
//!     index_writer.add_document(doc!(
//!         field => "Another Document"
//!     ))?;
//!
//!     index_writer.commit()?;
//!
//!     let reader = index
//!         .reader_builder()
//!         .reload_policy(ReloadPolicy::Manual)
//!         .try_into()?;
//!
//!     let searcher = reader.searcher();
//!
//!     let query_parser = QueryParser::for_index(&index, vec![field]);
//!
//!     let query = query_parser.parse_query("zhong")?;
//!     let top_docs = searcher.search(&query, &TopDocs::with_limit(10))?;
//!     let mut result: Vec<String> = Vec::new();
//!     for (_, doc_address) in top_docs {
//!         let retrieved_doc = searcher.doc::<TantivyDocument>(doc_address)?;
//!         result = retrieved_doc
//!             .get_all(field)
//!             .map(|v| v.as_str().unwrap().to_string())
//!             .collect();
//!     }
//!     let expected: Vec<String> = vec!["中国".to_string()];
//!     assert_eq!(expected, result);
//!
//!     let query = query_parser.parse_query("国")?;
//!     let top_docs = searcher.search(&query, &TopDocs::with_limit(10))?;
//!     let mut result: Vec<String> = Vec::new();
//!     for (_, doc_address) in top_docs {
//!         let retrieved_doc = searcher.doc::<TantivyDocument>(doc_address)?;
//!         result = retrieved_doc
//!             .get_all(field)
//!             .map(|v| v.as_str().unwrap().to_string())
//!             .collect();
//!     }
//!     let expected: Vec<String> = vec!["中国".to_string()];
//!     assert_eq!(expected, result);
//!
//!     let query = query_parser.parse_query("document")?;
//!     let top_docs = searcher.search(&query, &TopDocs::with_limit(10))?;
//!     let mut result: Vec<String> = Vec::new();
//!     for (_, doc_address) in top_docs {
//!         let retrieved_doc = searcher.doc::<TantivyDocument>(doc_address)?;
//!         result = retrieved_doc
//!             .get_all(field)
//!             .map(|v| v.as_str().unwrap().to_string())
//!             .collect();
//!     }
//!     let expected: Vec<String> = vec!["Another Document".to_string()];
//!     assert_eq!(expected, result);
//!
//! #    Ok(())
//! }
//! ```
//!
//! ## Feature flags
#![doc = document_features::document_features!()]
#![cfg_attr(test, deny(warnings))]
#![warn(
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs,
    trivial_numeric_casts,
    unsafe_code,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(feature = "commons")]
#[macro_use]
extern crate derive_builder;

#[cfg(feature = "commons")]
pub mod commons;
#[cfg(feature = "icu")]
pub mod icu;
#[cfg(feature = "phonetic")]
pub mod phonetic;