Exemplo n.º 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'
            },
        ])
Exemplo n.º 2
0
    def list_view(self):
        query = (Index.select(
            Index,
            fn.COUNT(IndexDocument.id).alias('document_count')).join(
                IndexDocument, JOIN.LEFT_OUTER).group_by(Index))

        ordering = request.args.getlist('ordering')
        query = engine.apply_sorting(
            query, ordering, {
                'name': Index.name,
                'document_count': SQL('document_count'),
                'id': Index.id
            }, 'name')

        pq = self.paginated_query(query)
        return jsonify({
            'indexes': [
                index_serializer.serialize(index)
                for index in pq.get_object_list()
            ],
            'ordering':
            ordering,
            'page':
            pq.get_page(),
            'pages':
            pq.get_page_count()
        })
Exemplo n.º 3
0
    def list_view(self):
        # Allow filtering by index.
        idx_list = request.args.getlist('index')
        if idx_list:
            indexes = Index.select(Index.id).where(Index.name << idx_list)
        else:
            indexes = None

        document_count = Document.select().count()
        return jsonify(self._search_response(indexes, True, document_count))
Exemplo n.º 4
0
    def test_index_update_delete(self):
        idx = Index.create(name='idx')
        alt_idx = Index.create(name='alt-idx')
        doc = idx.index(content='foo')
        alt_idx.index(doc.content, doc)
        idx.index('idx only')
        alt_idx.index('alt only')

        response = self.post_json('/idx/', {'name': 'idx-updated'})
        self.assertEqual(response['id'], idx.id)
        self.assertEqual(response['name'], 'idx-updated')
        self.assertEqual([doc['content'] for doc in response['documents']],
                         ['foo', 'idx only'])

        response = self.app.delete('/idx-updated/')
        data = json_load(response.data)
        self.assertEqual(data, {'success': True})

        self.assertEqual(Document.select().count(), 3)
        self.assertEqual(IndexDocument.select().count(), 2)
        self.assertEqual(Index.select().count(), 1)
Exemplo n.º 5
0
    def serialize(self, document, prefetched=False, include_score=False):
        data = {
            'id': document.docid,
            'identifier': document.identifier,
            'content': document.content,
        }

        _filename = operator.attrgetter('filename')
        data['attachments'] = [{
            'filename':
            attachment.filename,
            'mimetype':
            attachment.mimetype,
            'timestamp':
            str(attachment.timestamp),
            'data_length':
            attachment.length,
            'data':
            url_for('attachment_download',
                    document_id=document.docid,
                    pk=attachment.filename)
        } for attachment in sorted(document.attachments, key=_filename)]

        if prefetched:
            data['metadata'] = dict((metadata.key, metadata.value)
                                    for metadata in document.metadata_set)
            data['indexes'] = [
                idx_doc.index.name for idx_doc in document.indexdocument_set
            ]
        else:
            data['metadata'] = document.metadata
            indexes = (Index.select(Index.name).join(IndexDocument).where(
                IndexDocument.document == document.docid).order_by(
                    Index.name).tuples())
            data['indexes'] = [name for name, in indexes]

        if include_score:
            data['score'] = document.score

        return data
Exemplo n.º 6
0
    def validate_indexes(self, data, required=True):
        if data.get('index'):
            index_names = (data['index'], )
        elif data.get('indexes'):
            index_names = data['indexes']
        elif ('index' in data or 'indexes' in data) and not required:
            return ()
        else:
            return None

        indexes = list(Index.select().where(Index.name << index_names))

        # Validate that all the index names exist.
        observed_names = set(index.name for index in indexes)
        invalid_names = []
        for index_name in index_names:
            if index_name not in observed_names:
                invalid_names.append(index_name)

        if invalid_names:
            error('The following indexes were not found: %s.' %
                  ', '.join(invalid_names))

        return indexes
Exemplo n.º 7
0
 def test_create_index(self):
     data = self.post_json('/', {'name': 'TestIndex'})
     self.assertEqual(data['name'], 'TestIndex')
     self.assertEqual(data['documents'], [])
     self.assertEqual(Index.select().count(), 1)