/// Indexes a new document
///
/// As a user, you should rather use `IndexWriter`'s add_document.
Indexes a new document
As a user, you should rather use `IndexWriter`'s add_document.
Indexes a new document As a user you should rather use ` IndexWriter ` ' s add_document .
pub fn add_document(&mut self, add_operation: AddOperation, schema: &Schema) -> io::Result<()> {
        let doc_id = self.max_doc;
        let mut doc = add_operation.document;
        self.doc_opstamps.push(add_operation.opstamp);
        self.fast_field_writers.add_document(&doc);
        for (field, field_values) in doc.get_sorted_field_values() {
            let field_options = schema.get_field_entry(field);
            if !field_options.is_indexed() {
                continue;
            }
            match *field_options.field_type() {
                FieldType::HierarchicalFacet => {
                    let facets: Vec<&str> = field_values
                        .iter()
                        .flat_map(|field_value| match *field_value.value() {
                            Value::Facet(ref facet) => Some(facet.encoded_str()),
                            _ => {
                                panic!("Expected hierarchical facet");
                            }
                        })
                        .collect();
                    let mut term = Term::for_field(field); // we set the Term
                    for fake_str in facets {
                        let mut unordered_term_id_opt = None;
                        FacetTokenizer.token_stream(fake_str).process(&mut |token| {
                            term.set_text(&token.text);
                            let unordered_term_id =
                                self.multifield_postings.subscribe(doc_id, &term);
                            unordered_term_id_opt = Some(unordered_term_id);
                        });
                        if let Some(unordered_term_id) = unordered_term_id_opt {
                            self.fast_field_writers
                                .get_multivalue_writer(field)
                                .expect("writer for facet missing")
                                .add_val(unordered_term_id);
                        }
                    }
                }
                FieldType::Str(_) => {
                    let mut token_streams: Vec<BoxTokenStream> = vec![];
                    let mut offsets = vec![];
                    let mut total_offset = 0;
                    for field_value in field_values {
                        match field_value.value() {
                            Value::PreTokStr(tok_str) => {
                                offsets.push(total_offset);
                                if let Some(last_token) = tok_str.tokens.last() {
                                    total_offset += last_token.offset_to;
                                }
                                token_streams
                                    .push(PreTokenizedStream::from(tok_str.clone()).into());
                            }
                            Value::Str(ref text) => {
                                if let Some(ref mut tokenizer) =
                                    self.tokenizers[field.field_id() as usize]
                                {
                                    offsets.push(total_offset);
                                    total_offset += text.len();
                                    token_streams.push(tokenizer.token_stream(text));
                                }
                            }
                            _ => (),
                        }
                    }
                    let num_tokens = if token_streams.is_empty() {
                        0
                    } else {
                        let mut token_stream = TokenStreamChain::new(offsets, token_streams);
                        self.multifield_postings
                            .index_text(doc_id, field, &mut token_stream)
                    };
                    self.fieldnorms_writer.record(doc_id, field, num_tokens);
                }
                FieldType::U64(ref int_option) => {
                    if int_option.is_indexed() {
                        for field_value in field_values {
                            let term = Term::from_field_u64(
                                field_value.field(),
                                field_value.value().u64_value(),
                            );
                            self.multifield_postings.subscribe(doc_id, &term);
                        }
                    }
                }
                FieldType::Date(ref int_option) => {
                    if int_option.is_indexed() {
                        for field_value in field_values {
                            let term = Term::from_field_i64(
                                field_value.field(),
                                field_value.value().date_value().timestamp(),
                            );
                            self.multifield_postings.subscribe(doc_id, &term);
                        }
                    }
                }
                FieldType::I64(ref int_option) => {
                    if int_option.is_indexed() {
                        for field_value in field_values {
                            let term = Term::from_field_i64(
                                field_value.field(),
                                field_value.value().i64_value(),
                            );
                            self.multifield_postings.subscribe(doc_id, &term);
                        }
                    }
                }
                FieldType::F64(ref int_option) => {
                    if int_option.is_indexed() {
                        for field_value in field_values {
                            let term = Term::from_field_f64(
                                field_value.field(),
                                field_value.value().f64_value(),
                            );
                            self.multifield_postings.subscribe(doc_id, &term);
                        }
                    }
                }
                FieldType::Bytes => {
                    // Do nothing. Bytes only supports fast fields.
                }
            }
        }
        doc.filter_fields(|field| schema.get_field_entry(field).is_stored());
        doc.prepare_for_store();
        let doc_writer = self.segment_serializer.get_store_writer();
        doc_writer.store(&doc)?;
        self.max_doc += 1;
        Ok(())
    }
pub fn add_document ( & mut self , add_operation : AddOperation , schema : & Schema ) -> io :: Result < ( ) > { let doc_id = self . max_doc ; let mut doc = add_operation . document ; self . doc_opstamps . push ( add_operation . opstamp ) ; self . fast_field_writers . add_document ( & doc ) ; for ( field , field_values ) in doc . get_sorted_field_values ( ) { let field_options = schema . get_field_entry ( field ) ; if ! field_options . is_indexed ( ) { continue ; } match * field_options . field_type ( ) { FieldType :: HierarchicalFacet => { let facets : Vec < & str > = field_values . iter ( ) . flat_map ( | field_value | match * field_value . value ( ) { Value :: Facet ( ref facet ) => Some ( facet . encoded_str ( ) ) , _ => { panic ! ( " " ) ; } } ) . collect ( ) ; let mut term = Term :: for_field ( field ) ; // we set the Term for fake_str in facets { let mut unordered_term_id_opt = None ; FacetTokenizer . token_stream ( fake_str ) . process ( & mut | token | { term . set_text ( & token . text ) ; let unordered_term_id = self . multifield_postings . subscribe ( doc_id , & term ) ; unordered_term_id_opt = Some ( unordered_term_id ) ; } ) ; if let Some ( unordered_term_id ) = unordered_term_id_opt { self . fast_field_writers . get_multivalue_writer ( field ) . expect ( " " ) . add_val ( unordered_term_id ) ; } } } FieldType :: Str ( _ ) => { let mut token_streams : Vec < BoxTokenStream > = vec ! [ ] ; let mut offsets = vec ! [ ] ; let mut total_offset = 0 ; for field_value in field_values { match field_value . value ( ) { Value :: PreTokStr ( tok_str ) => { offsets . push ( total_offset ) ; if let Some ( last_token ) = tok_str . tokens . last ( ) { total_offset += last_token . offset_to ; } token_streams . push ( PreTokenizedStream :: from ( tok_str . clone ( ) ) . into ( ) ) ; } Value :: Str ( ref text ) => { if let Some ( ref mut tokenizer ) = self . tokenizers [ field . field_id ( ) as usize ] { offsets . push ( total_offset ) ; total_offset += text . len ( ) ; token_streams . push ( tokenizer . token_stream ( text ) ) ; } } _ => ( ) , } } let num_tokens = if token_streams . is_empty ( ) { 0 } else { let mut token_stream = TokenStreamChain :: new ( offsets , token_streams ) ; self . multifield_postings . index_text ( doc_id , field , & mut token_stream ) } ; self . fieldnorms_writer . record ( doc_id , field , num_tokens ) ; } FieldType :: U64 ( ref int_option ) => { if int_option . is_indexed ( ) { for field_value in field_values { let term = Term :: from_field_u64 ( field_value . field ( ) , field_value . value ( ) . u64_value ( ) , ) ; self . multifield_postings . subscribe ( doc_id , & term ) ; } } } FieldType :: Date ( ref int_option ) => { if int_option . is_indexed ( ) { for field_value in field_values { let term = Term :: from_field_i64 ( field_value . field ( ) , field_value . value ( ) . date_value ( ) . timestamp ( ) , ) ; self . multifield_postings . subscribe ( doc_id , & term ) ; } } } FieldType :: I64 ( ref int_option ) => { if int_option . is_indexed ( ) { for field_value in field_values { let term = Term :: from_field_i64 ( field_value . field ( ) , field_value . value ( ) . i64_value ( ) , ) ; self . multifield_postings . subscribe ( doc_id , & term ) ; } } } FieldType :: F64 ( ref int_option ) => { if int_option . is_indexed ( ) { for field_value in field_values { let term = Term :: from_field_f64 ( field_value . field ( ) , field_value . value ( ) . f64_value ( ) , ) ; self . multifield_postings . subscribe ( doc_id , & term ) ; } } } FieldType :: Bytes => { // Do nothing. Bytes only supports fast fields. } } } doc . filter_fields ( | field | schema . get_field_entry ( field ) . is_stored ( ) ) ; doc . prepare_for_store ( ) ; let doc_writer = self . segment_serializer . get_store_writer ( ) ; doc_writer . store ( & doc ) ? ; self . max_doc += 1 ; Ok ( ( ) ) }
Indexes a new document
As a user, you should rather use `IndexWriter`'s add_document.
Indexes a new document As a user you should rather use ` IndexWriter ` ' s add_document .
&mut self
add_operation
AddOperation
schema
&Schema