Example #1
0
    def test_index_upgrade(self):
        ParentIndexable(foo="Fighters").save()
        ChildIndexable(foo="Fighters", bar=69).save()
        GrandchildIndexable(foo="Fighters",
                            bar=69,
                            baz=datetime.datetime.now() -
                            datetime.timedelta(hours=1)).save()
        SeparateIndexable(junk="Testing").save()
        # make sure we have some indexed stuff
        ParentIndexable.search_objects.refresh()
        SeparateIndexable.search_objects.refresh()
        self.assertEqual(ParentIndexable.search_objects.s().count(), 3)
        self.assertEqual(SeparateIndexable.search_objects.s().count(), 1)

        call_command("synces", "vtest123", drop_existing_indexes=True)
        call_command("bulk_index", index_suffix="vtest123")
        call_command("es_swap_aliases", "vtest123")
        ParentIndexable.search_objects.refresh()
        SeparateIndexable.search_objects.refresh()
        # Let's make sure that everything has the right counts
        self.assertEqual(ParentIndexable.search_objects.s().count(), 3)
        self.assertEqual(SeparateIndexable.search_objects.s().count(), 1)
        self.delete_indexes_with_suffix("vtest123")  # clean up
Example #2
0
    def test_bulk_index(self):
        ParentIndexable(foo="Fighters").save(index=False)
        ChildIndexable(foo="Fighters", bar=69).save(index=False)

        GrandchildIndexable(foo="Fighters",
                            bar=69,
                            baz=datetime.datetime.now() -
                            datetime.timedelta(hours=1)).save(index=False)

        SeparateIndexable(junk="Testing").save(index=False)

        # Let's make sure that nothing is indexed yet.
        self.assertEqual(ParentIndexable.search_objects.s().count(), 0)
        self.assertEqual(SeparateIndexable.search_objects.s().count(), 0)

        # Now that everything has been made, let's try a bulk_index.
        call_command("bulk_index")
        ParentIndexable.search_objects.refresh()
        SeparateIndexable.search_objects.refresh()

        # Let's make sure that everything has the right counts
        self.assertEqual(ParentIndexable.search_objects.s().count(), 3)
        self.assertEqual(SeparateIndexable.search_objects.s().count(), 1)

        # Let's add another one, make sure the counts are right.
        ParentIndexable(foo="Mr. T").save(index=False)
        self.assertEqual(ParentIndexable.search_objects.s().count(), 3)
        call_command("bulk_index")
        ParentIndexable.search_objects.refresh()
        self.assertEqual(ParentIndexable.search_objects.s().count(), 4)

        # Let's f**k up some data in ES.
        obj = ParentIndexable.objects.all()[0]
        es = get_es(urls=settings.ES_URLS)
        doc = obj.extract_document()
        doc["foo"] = "DATA LOVERS"
        es.update(index=obj.get_index_name(),
                  doc_type=obj.get_mapping_type_name(),
                  id=obj.id,
                  body=dict(doc=doc, doc_as_upsert=True),
                  refresh=True)

        # Make sure the bad data works
        self.assertEqual(
            ParentIndexable.search_objects.query(
                foo__match="DATA LOVERS").count(), 1)
        call_command("bulk_index")
        ParentIndexable.search_objects.refresh()
        self.assertEqual(
            ParentIndexable.search_objects.query(
                foo__match="DATA LOVERS").count(), 0)

        # Let's delete an item from the db.
        obj = ParentIndexable.objects.all()[0]
        obj.delete()

        # Make sure the count is the same
        self.assertEqual(ParentIndexable.search_objects.s().count(), 4)

        # This shoulnd't remove the item
        call_command("bulk_index")
        ParentIndexable.search_objects.refresh()
        self.assertEqual(ParentIndexable.search_objects.s().count(), 4)

        # This should
        call_command("synces", self.index_suffix, drop_existing_indexes=True)
        call_command("es_swap_aliases", self.index_suffix)
        call_command("bulk_index")
        ParentIndexable.search_objects.refresh()
        self.assertEqual(ParentIndexable.search_objects.s().count(), 3)