def index_document(self, data): """Add the given data to the database. :param data: the data to be indexed. A dictionary will be treated as ``fieldname:value`` combinations. If the fieldname is None then the value will be interpreted as a plain term or as a list of plain terms. Lists of terms are indexed separately. Lists of strings are treated as plain terms. :type data: dict | list of str """ doc = self._create_empty_document() if isinstance(data, dict): data = data.items() # add all data for dataset in data: if isinstance(dataset, tuple): # the dataset tuple consists of '(key, value)' key, value = dataset if key is None: if isinstance(value, list): terms = value[:] elif isinstance(value, basestring): terms = [value] else: raise ValueError( "Invalid data type to be indexed: %s" % str(type(data))) for one_term in terms: self._add_plain_term(doc, self._decode(one_term), (self.ANALYZER_DEFAULT & self.ANALYZER_TOKENIZE > 0)) else: analyze_settings = self.get_field_analyzers(key) # handle multiple terms if not isinstance(value, list): value = [value] for one_term in value: self._add_field_term( doc, key, self._decode(one_term), (analyze_settings & self.ANALYZER_TOKENIZE > 0)) elif isinstance(dataset, basestring): self._add_plain_term( doc, self._decode(dataset), (self.ANALYZER_DEFAULT & self.ANALYZER_TOKENIZE > 0)) else: raise ValueError("Invalid data type to be indexed: %s" % str(type(data))) self._add_document_to_index(doc)
def index_document(self, data): """Add the given data to the database. :param data: the data to be indexed. A dictionary will be treated as ``fieldname:value`` combinations. If the fieldname is None then the value will be interpreted as a plain term or as a list of plain terms. Lists of terms are indexed separately. Lists of strings are treated as plain terms. :type data: dict | list of str """ doc = self._create_empty_document() if isinstance(data, dict): data = data.items() # add all data for dataset in data: if isinstance(dataset, tuple): # the dataset tuple consists of '(key, value)' key, value = dataset if key is None: if isinstance(value, list): terms = value[:] elif isinstance(value, six.string_types): terms = [value] else: raise ValueError("Invalid data type to be indexed: %s" % str(type(data))) for one_term in terms: self._add_plain_term( doc, self._decode(one_term), (self.ANALYZER_DEFAULT & self.ANALYZER_TOKENIZE > 0)) else: analyze_settings = self.get_field_analyzers(key) # handle multiple terms if not isinstance(value, list): value = [value] for one_term in value: self._add_field_term( doc, key, self._decode(one_term), (analyze_settings & self.ANALYZER_TOKENIZE > 0)) elif isinstance(dataset, six.string_types): self._add_plain_term( doc, self._decode(dataset), (self.ANALYZER_DEFAULT & self.ANALYZER_TOKENIZE > 0)) else: raise ValueError("Invalid data type to be indexed: %s" % str(type(data))) self._add_document_to_index(doc)