Beispiel #1
0
    def test_facet_script(self):
        client = self._get_client()
        index = client.get_index('test')
        index.create(options=True)
        doc_type = index.get_doc_type('helloworld')

        doc_type.add_document(
            pylastica.Document(1, {
                'name': 'Tyler',
                'last_name': 'Durden'
            }))
        doc_type.add_document(
            pylastica.Document(2, {
                'name': 'Marla',
                'last_name': 'Singer'
            }))

        index.refresh()
        facet = pylastica.facet.Terms('test')
        facet.set_field('name')
        facet.set_script(
            pylastica.Script('term + " " + doc["last_name"].value'))
        query = pylastica.query.Query()
        query.add_facet(facet)
        response = doc_type.search(query)
        facets = response.get_facets()
        self.assertEqual(2, len(facets['test']['terms']))
        index.delete()
Beispiel #2
0
    def test_create_abstract_document_with_invalid_data(self):
        bad_document = object()
        # wrong class type
        self.assertRaises(
            TypeError, pylastica.bulk.abstractdocument.AbstractDocument.create,
            bad_document)

        # attempt to create a Document with a Script
        script = pylastica.Script('')
        self.assertRaises(
            TypeError, pylastica.bulk.abstractdocument.AbstractDocument.create,
            script,
            pylastica.bulk.abstractdocument.AbstractDocument.OP_TYPE_CREATE)
Beispiel #3
0
    def test_query(self):
        doc_type = self._index.get_doc_type('test')
        doc_type.add_document(
            pylastica.Document(1, {
                'firstname': 'nicolas',
                'lastname': 'ruflin'
            }))
        self._index.refresh()

        query = pylastica.query.Query()
        script = pylastica.Script('1 + 2')
        script_fields = pylastica.ScriptFields({'test': script})
        query.set_script_fields(script_fields)
        result_set = doc_type.search(query)
        first = result_set[0].get_data()

        self.assertEqual(3, first['test'][0])
Beispiel #4
0
    def test_statistical_with_set_script(self):
        client = self._get_client()
        index = client.get_index('test')
        index.create(options=True)
        doc_type = index.get_doc_type('helloworld')
        doc_type.add_document(pylastica.Document(1, {'price': 10}))
        doc_type.add_document(pylastica.Document(2, {'price': 35}))
        doc_type.add_document(pylastica.Document(3, {'price': 45}))

        index.refresh()
        facet = pylastica.facet.Statistical('stats')
        script = pylastica.Script("doc['price'].value + offset;", {'offset': 5})
        facet.set_script(script)
        query = pylastica.query.Query()
        query.add_facet(facet)
        response = doc_type.search(query)
        facets = response.get_facets()
        self.assertEqual(105.0, facets['stats']['total'])
        self.assertEqual(15.0, facets['stats']['min'])
        self.assertEqual(50.0, facets['stats']['max'])
        index.delete()
Beispiel #5
0
    def test_update(self):
        index = self._create_index()
        doc_type = index.get_doc_type('bulk_test')
        client = index.client

        doc1 = doc_type.create_document(1, {'name': 'John'})
        doc2 = pylastica.Document(2, {'name': 'Paul'})
        doc3 = doc_type.create_document(3, {'name': 'George'})
        doc4 = doc_type.create_document(data={'name': 'Ringo'})

        doc3.op_type = pylastica.Document.OP_TYPE_CREATE
        documents = [doc1, doc2, doc3, doc4]

        bulk = pylastica.bulk.Bulk(client)
        bulk.doc_type = doc_type
        bulk.add_documents(documents)
        bulk.add_document(
            pylastica.Document(2, {'name': 'The Walrus'}, doc_type, index),
            pylastica.bulk.action.Action.OP_TYPE_UPDATE)

        response = bulk.send()
        self.assertIsInstance(response, pylastica.bulk.ResponseSet)
        self.assertTrue(response.is_ok())
        self.assertFalse(response.has_error())
        for i in range(len(response)):
            self.assertIsInstance(response[i], pylastica.bulk.Response)
            self.assertTrue(response[i].is_ok())
            self.assertFalse(response[i].has_error())

        doc_type.index.refresh()

        doc2 = doc_type.get_document('2')
        self.assertEqual('The Walrus', doc2.data['name'])

        #test updating via script
        script = pylastica.Script('ctx._source.name += param1',
                                  {'param1': ' was Paul'},
                                  doc_id=2)
        doc2 = pylastica.Document()
        script.upsert = doc2
        action = pylastica.bulk.action.AbstractDocument.create(
            script, pylastica.bulk.action.AbstractDocument.OP_TYPE_UPDATE)

        bulk = pylastica.bulk.Bulk(client)
        bulk.doc_type = doc_type
        bulk.add_action(action)
        bulk.send()
        index.refresh()

        doc = doc_type.get_document('2')
        self.assertEqual('The Walrus was Paul', doc.data['name'])

        #test upsert
        script = pylastica.Script('ctx._source.counter += count', {'count': 1},
                                  doc_id=5)
        doc = pylastica.Document('', {'counter': 1})
        script.upsert = doc
        action = pylastica.bulk.action.AbstractDocument.create(
            script, pylastica.bulk.action.AbstractDocument.OP_TYPE_UPDATE)

        bulk = pylastica.bulk.Bulk(client)
        bulk.doc_type = doc_type
        bulk.add_action(action)
        bulk.send()
        index.refresh()

        doc = doc_type.get_document('5')
        self.assertEqual(1, doc.data['counter'])

        #test doc_as_upsert
        doc = pylastica.Document(6, {'test': 'test'})
        doc.doc_as_upsert = True
        update_action = pylastica.bulk.action.AbstractDocument.create(
            doc, pylastica.bulk.action.AbstractDocument.OP_TYPE_UPDATE)
        bulk = pylastica.bulk.Bulk(client)
        bulk.doc_type = doc_type
        bulk.add_action(update_action)
        response = bulk.send()

        self.assertTrue(response.is_ok())
        self.assertFalse(response.has_error())

        index.refresh()
        doc = doc_type.get_document(6)
        self.assertEqual('test', doc.data['test'])

        index.delete()