lunr.Builder.prototype.add = function()

in static/api/python/3.1.x/lunr.js [2468:2536]


    lunr.Builder.prototype.add = function (doc, attributes) {
      var docRef = doc[this._ref],
          fields = Object.keys(this._fields)
    
      this._documents[docRef] = attributes || {}
      this.documentCount += 1
    
      for (var i = 0; i < fields.length; i++) {
        var fieldName = fields[i],
            extractor = this._fields[fieldName].extractor,
            field = extractor ? extractor(doc) : doc[fieldName],
            tokens = this.tokenizer(field, {
              fields: [fieldName]
            }),
            terms = this.pipeline.run(tokens),
            fieldRef = new lunr.FieldRef (docRef, fieldName),
            fieldTerms = Object.create(null)
    
        this.fieldTermFrequencies[fieldRef] = fieldTerms
        this.fieldLengths[fieldRef] = 0
    
        // store the length of this field for this document
        this.fieldLengths[fieldRef] += terms.length
    
        // calculate term frequencies for this field
        for (var j = 0; j < terms.length; j++) {
          var term = terms[j]
    
          if (fieldTerms[term] == undefined) {
            fieldTerms[term] = 0
          }
    
          fieldTerms[term] += 1
    
          // add to inverted index
          // create an initial posting if one doesn't exist
          if (this.invertedIndex[term] == undefined) {
            var posting = Object.create(null)
            posting["_index"] = this.termIndex
            this.termIndex += 1
    
            for (var k = 0; k < fields.length; k++) {
              posting[fields[k]] = Object.create(null)
            }
    
            this.invertedIndex[term] = posting
          }
    
          // add an entry for this term/fieldName/docRef to the invertedIndex
          if (this.invertedIndex[term][fieldName][docRef] == undefined) {
            this.invertedIndex[term][fieldName][docRef] = Object.create(null)
          }
    
          // store all whitelisted metadata about this token in the
          // inverted index
          for (var l = 0; l < this.metadataWhitelist.length; l++) {
            var metadataKey = this.metadataWhitelist[l],
                metadata = term.metadata[metadataKey]
    
            if (this.invertedIndex[term][fieldName][docRef][metadataKey] == undefined) {
              this.invertedIndex[term][fieldName][docRef][metadataKey] = []
            }
    
            this.invertedIndex[term][fieldName][docRef][metadataKey].push(metadata)
          }
        }
    
      }
    }