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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
* 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.
*/
use std::borrow::Cow;
use std::fmt;
use std::vec::IntoIter;
use crate::core::document::fields::Fields;
use crate::core::index::BinaryValueEnum;
use crate::core::index::indexable_field::IndexableField;
use crate::core::util::error::lucene_error::Result;
/// Documents are the unit of indexing and search.
///
/// A Document is a set of fields. Each field has a name and a textual value. A
/// field may be
/// [`IndexableFieldType::stored`](crate::core::index::indexable_field_type::IndexableFieldType::stored) with the document, in which case it is returned with search hits
/// on the document. Thus each document should typically contain one or more
/// stored fields which uniquely identify it.
///
/// Note that fields which are *not*
/// [`IndexableFieldType::stored`](crate::core::index::indexable_field_type::IndexableFieldType::stored)
/// are *not* available in documents retrieved from the index, e.g. with
/// [`ScoreDoc::doc`](crate::core::search::score_doc::ScoreDoc) or
/// [`StoredFields::document(i32)`](crate::core::index::stored_fields::StoredFields::document).
pub struct Document {
fields: Vec<Fields>,
}
impl Default for Document {
fn default() -> Self {
Self::new()
}
}
impl Document {
/// Constructs a new document with no fields.
pub fn new() -> Self {
Document { fields: Vec::new() }
}
/// Adds a field to a document. Several fields may be added with the same
/// name. In this case, if the fields are indexed, their text is treated
/// as though appended for the purposes of search.
///
/// Note that `add` like the `removeField(s)` methods only makes sense prior
/// to adding a document to an index. These methods cannot be used to
/// change the content oR an existing index! In order to achieve this, a
/// document has to be deleted from an index and a new changed version of
/// that document has to be added.
#[inline(always)]
pub fn add<T>(&mut self, field: T)
where
T: Into<Fields>,
{
let field = field.into();
if self.fields.capacity() == 0 {
self.fields.reserve_exact(10);
}
self.fields.push(field);
}
/// Removes the field with the specified name from the document. If multiple
/// fields exist with this name, this method removes the first field
/// that has been added. If there is no field with the specified name,
/// the document remains unchanged.
///
/// Note that the `removeField(s)` methods, like the `add` method, only make
/// sense prior to adding a document to an index. These methods cannot
/// be used to change the content of an existing index! In order to
/// achieve this, a document has to be deleted from an index and a new
/// changed version of that document has to be added.
pub fn remove_field(&mut self, name: &str) {
if let Some(index) = self.fields.iter().position(|field| field.name() == name) {
self.fields.remove(index);
}
}
/// Removes all fields with the given name from the document. If there is no
/// field with the specified name, the document remains unchanged.
///
/// Note that the `removeField(s)` methods, like the `add` method, only make
/// sense prior to adding a document to an index. These methods cannot
/// be used to change the content of an existing index! In order to
/// achieve this, a document has to be deleted from an index and a new
/// changed version of that document has to be added.
pub fn remove_fields(&mut self, name: &str) {
self.fields.retain(|field| field.name() != name);
}
/// Returns an array of byte arrays for the fields that have the name
/// specified as the method parameter. This method returns an empty
/// array when there are no matching fields. It never returns `None`.
///
/// # Parameters
/// - `name`: the name of the field
///
/// # Returns
/// A vector of binary field values.
pub fn get_binary_values(&self, name: &str) -> Result<Vec<BinaryValueEnum<'_>>> {
let mut result = Vec::new();
for field in &self.fields {
if field.name() == name {
match field.binary_value() {
Ok(Some(bytes)) => result.push(bytes),
Ok(None) => continue,
Err(e) => return Err(e),
}
}
}
Ok(result)
}
/// Returns an array of bytes for the first (or only) field that has the
/// name specified as the method parameter. This method will return
/// `None` if no binary fields with the specified name are available.
/// There may be non-binary fields with the same name.
///
/// # Parameters
/// - `name`: the name of the field.
///
/// # Returns
/// The binary field value, or `None` if no matching field is found.
pub fn get_binary_value(&self, name: &str) -> Result<Option<BinaryValueEnum<'_>>> {
for field in &self.fields {
if field.name() == name {
return match field.binary_value() {
Ok(Some(bytes)) => Ok(Some(bytes)),
Ok(None) => Ok(None),
Err(e) => Err(e),
};
}
}
Ok(None)
}
/// Returns a field with the given name if any exist in this document, or
/// `None`. If multiple fields exist with this name, this method returns
/// the first value added.
///
/// # Parameters
/// - `name`: the name of the field.
///
/// # Returns
/// An `Option<Arc>`, `None` means no matching field is found.
pub fn get_field(&self, name: &str) -> Option<&Fields> {
self.fields.iter().find(|field| field.name() == name)
}
/// Returns a mutable field with the given name if any exist in this document, or
/// `None`. If multiple fields exist with this name, this method returns
/// the first value added.
pub fn get_field_mut(&mut self, name: &str) -> Option<&mut Fields> {
self.fields.iter_mut().find(|field| field.name() == name)
}
/// Returns an array of [`IndexableField`]s with the given name. This method
/// returns an empty array when there are no matching fields. It never
/// returns `None`.
///
/// # Parameters
/// - `name`: the name of the field.
///
/// # Returns
/// A `Vec<Arc>` array containing the matching fields.
pub fn get_fields_with_name(&self, name: &str) -> Vec<&Fields> {
self
.fields
.iter()
.filter(|field| field.name() == name)
.collect()
}
/// Returns a `Vec<Arc>` containing all the fields in a document.
///
/// # Note
/// Fields that are not stored are not available in documents retrieved from
/// the index, for example when using
/// [`StoredFields::document`](crate::core::index::stored_fields::StoredFields::document).
///
/// # Returns
/// An immutable `Vec<Arc>` containing all fields in the document.
pub fn get_fields(&self) -> &[Fields] {
&self.fields
}
/// Returns an array of values of the field specified by the `name`. This
/// method returns an empty array when there are no matching fields. It
/// never returns `None`. For a numeric [`StoredField`](crate::core::document::stored_field::StoredField), it returns the
/// string representation of the number. To get the actual numeric field
/// instances, use `getFields`.
///
/// # Parameters
/// - `name`: the name of the field.
///
/// # Returns
/// A `Vec<Arc<String>`, which is an empty vector if no matching fields are
/// found.
pub fn get_values(&self, name: &str) -> Result<Vec<Cow<'_, str>>> {
let mut result = Vec::new();
for field in &self.fields {
if field.name() == name {
match field.string_value() {
Ok(Some(value)) => result.push(value),
Ok(None) => continue,
Err(e) => return Err(e),
}
}
}
Ok(result)
}
/// Returns the string value of the field with the given name if any exist
/// in this document, or `None`. If multiple fields exist with this
/// name, this method returns the first value added. If only binary
/// fields with this name exist, returns `None`. For a numeric
/// [`StoredField`](crate::core::document::stored_field::StoredField), it returns the string value of the number. To get the
/// actual numeric field instance, use `getField`.
///
/// # Parameters
/// - `name`: the name of the field.
///
/// # Returns
/// An `Option<Cow<str>>`, `None` means no string value is found (e.g.,
/// for binary fields).
pub fn get(&self, name: &str) -> Result<Option<Cow<'_, str>>> {
for field in &self.fields {
if field.name() == name {
return match field.string_value() {
Ok(Some(value)) => Ok(Some(value)),
Ok(None) => Ok(None),
Err(e) => Err(e),
};
}
}
Ok(None)
}
/// Removes all the fields from document.
pub fn clear(&mut self) {
self.fields.clear();
}
}
impl fmt::Display for Document {
/// Prints the fields of a document for human consumption.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}<", std::any::type_name::<Self>())?;
for (i, field) in self.fields.iter().enumerate() {
write!(f, "{field}")?;
if i != self.fields.len() - 1 {
write!(f, " ")?;
}
}
write!(f, ">")
}
}
impl IntoIterator for Document {
type Item = Fields;
type IntoIter = IntoIter<Fields>;
fn into_iter(self) -> Self::IntoIter {
self.fields.into_iter()
}
}
impl<'a> IntoIterator for &'a Document {
type Item = &'a Fields;
type IntoIter = std::slice::Iter<'a, Fields>;
fn into_iter(self) -> Self::IntoIter {
self.fields.iter()
}
}
impl<'a> IntoIterator for &'a mut Document {
type Item = &'a mut Fields;
type IntoIter = std::slice::IterMut<'a, Fields>;
fn into_iter(self) -> Self::IntoIter {
self.fields.iter_mut()
}
}