Пример #1
0
    def test_multi_index(self):
        """
        Test that documents can be stored in multiple indexes.
        """
        self.index.delete_instance()

        indexes = [Index.create(name='idx-%s' % i) for i in range(3)]
        document = Document.create(content='hueybear')
        for index in indexes:
            index.index(document.content, document)

        self.assertEqual(Document.select().count(), 1)
        self.assertEqual(Index.select().count(), 3)
        self.assertEqual(IndexDocument.select().count(), 3)
        query = (IndexDocument.select(
            Index.name,
            IndexDocument.document).join(Index).order_by(Index.name).dicts())
        idx_doc_data = [idx_doc for idx_doc in query]
        self.assertEqual(idx_doc_data, [
            {
                'document': document.get_id(),
                'name': 'idx-0'
            },
            {
                'document': document.get_id(),
                'name': 'idx-1'
            },
            {
                'document': document.get_id(),
                'name': 'idx-2'
            },
        ])
Пример #2
0
    def create(self):
        data = validator.parse_post(
            ['content'], ['identifier', 'index', 'indexes', 'metadata'])

        indexes = validator.validate_indexes(data)
        if indexes is None:
            error('You must specify either an "index" or "indexes".')

        if data.get('identifier'):
            try:
                document = self._get_document(data['identifier'])
            except NotFound:
                pass
            else:
                return self.update(data['identifier'])

        document = Document.create(content=data['content'],
                                   identifier=data.get('identifier'))

        if data.get('metadata'):
            document.metadata = data['metadata']

        logger.info('Created document with id=%s', document.get_id())

        for index in indexes:
            index.add_to_index(document)
            logger.info('Added document %s to index %s', document.get_id(),
                        index.name)

        if len(request.files):
            self.attach_files(document)

        return self.detail(document.get_id())