Exemplo n.º 1
0
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel,
                                                      backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
Exemplo n.º 2
0
    def test_log_query(self):
        from django.conf import settings
        from haystack import backends
        backends.reset_search_queries()
        self.assertEqual(len(backends.queries), 0)

        # Stow.
        old_debug = settings.DEBUG
        settings.DEBUG = False

        len(self.sq.get_results())
        self.assertEqual(len(backends.queries), 0)

        settings.DEBUG = True
        # Redefine it to clear out the cached results.
        self.sq = SearchQuery(backend=SearchBackend())
        self.sq.add_filter(SQ(name='bar'))
        len(self.sq.get_results())
        self.assertEqual(len(backends.queries), 1)
        self.assertEqual(backends.queries[0]['query_string'], 'name:bar')

        # And again, for good measure.
        self.sq = SearchQuery(backend=SearchBackend())
        self.sq.add_filter(SQ(name='bar'))
        self.sq.add_filter(SQ(text='moof'))
        len(self.sq.get_results())
        self.assertEqual(len(backends.queries), 2)
        self.assertEqual(backends.queries[0]['query_string'], 'name:bar')
        self.assertEqual(backends.queries[1]['query_string'],
                         u'(name:bar AND text:moof)')

        # Restore.
        settings.DEBUG = old_debug
Exemplo n.º 3
0
    def setUp(self):
        super(SolrBoostBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i

            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'

            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
class SolrBoostBackendTestCase(TestCase):
    def setUp(self):
        super(SolrBoostBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site
        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i

            if i % 2:
                mock.author = "daniel"
                mock.editor = "david"
            else:
                mock.author = "david"
                mock.editor = "daniel"

            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack

        haystack.site = self.old_site
        super(SolrBoostBackendTestCase, self).tearDown()

    def test_boost(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 4)

        results = SearchQuerySet().filter(SQ(author="daniel") | SQ(editor="daniel"))

        self.assertEqual(
            [result.id for result in results],
            [
                "core.afourthmockmodel.1",
                "core.afourthmockmodel.3",
                "core.afourthmockmodel.2",
                "core.afourthmockmodel.4",
            ],
        )
Exemplo n.º 5
0
class SolrBoostBackendTestCase(TestCase):
    def setUp(self):
        super(SolrBoostBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
        self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
        self.sample_objs = []

        for i in xrange(1, 5):
            mock = AFourthMockModel()
            mock.id = i

            if i % 2:
                mock.author = 'daniel'
                mock.editor = 'david'
            else:
                mock.author = 'david'
                mock.editor = 'daniel'

            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(SolrBoostBackendTestCase, self).tearDown()

    def test_boost(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 4)

        results = SearchQuerySet().filter(
            SQ(author='daniel') | SQ(editor='daniel'))

        self.assertEqual([result.id for result in results], [
            'core.afourthmockmodel.1', 'core.afourthmockmodel.3',
            'core.afourthmockmodel.2', 'core.afourthmockmodel.4'
        ])
Exemplo n.º 6
0
 def setUp(self):
     super(SolrSearchBackendTestCase, self).setUp()
     
     # Wipe it clean.
     self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
     clear_solr_index()
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
     self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
     self.site.register(MockModel, SolrMockSearchIndex)
     
     # Stow.
     import haystack
     self.old_site = haystack.site
     haystack.site = self.site
     
     self.sample_objs = []
     
     for i in xrange(1, 4):
         mock = MockModel()
         mock.id = i
         mock.author = 'daniel%s' % i
         mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
         self.sample_objs.append(mock)
Exemplo n.º 7
0
 def setUp(self):
     super(SolrBoostBackendTestCase, self).setUp()
     
     # Wipe it clean.
     self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
     clear_solr_index()
     
     self.site = SearchSite()
     self.sb = SearchBackend(site=self.site)
     self.smmi = SolrBoostMockSearchIndex(AFourthMockModel, backend=self.sb)
     self.site.register(AFourthMockModel, SolrBoostMockSearchIndex)
     
     # Stow.
     import haystack
     self.old_site = haystack.site
     haystack.site = self.site
     self.sample_objs = []
     
     for i in xrange(1, 5):
         mock = AFourthMockModel()
         mock.id = i
         
         if i % 2:
             mock.author = 'daniel'
             mock.editor = 'david'
         else:
             mock.author = 'david'
             mock.editor = 'daniel'
         
         mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
         self.sample_objs.append(mock)
Exemplo n.º 8
0
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Stow.
        self.old_solr_url = getattr(settings, "HAYSTACK_SOLR_URL", "http://localhost:9001/solr/test_default")
        settings.HAYSTACK_SOLR_URL = "http://localhost:9001/solr/test_default"

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q="*:*")

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = "daniel%s" % i
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
Exemplo n.º 9
0
 def setUp(self):
     super(SolrSearchBackendTestCase, self).setUp()
     
     # Stow.
     self.old_solr_url = getattr(settings, 'HAYSTACK_SOLR_URL', 'http://localhost:9001/solr/test_default')
     settings.HAYSTACK_SOLR_URL = 'http://localhost:9001/solr/test_default'
     
     self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
     self.raw_solr.delete(q='*:*')
     
     self.sb = SearchBackend()
     self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
     self.site = SolrSearchSite()
     self.site.register(MockModel, SolrMockSearchIndex)
     
     # Stow.
     self.old_site = sites.site
     sites.site = self.site
     
     self.sample_objs = []
     
     for i in xrange(1, 4):
         mock = MockModel()
         mock.id = i
         mock.author = 'daniel%s' % i
         mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
         self.sample_objs.append(mock)
Exemplo n.º 10
0
    def test_altered_names(self):
        sq = SearchQuery(backend=SearchBackend())

        sq.add_filter(SQ(content='hello'))
        sq.add_model(MockModel)
        self.assertEqual(sq.build_query(),
                         u'(hello) AND (my_django_ct:core.mockmodel)')

        sq.add_model(AnotherMockModel)
        self.assertEqual(
            sq.build_query(),
            u'(hello) AND (my_django_ct:core.anothermockmodel OR my_django_ct:core.mockmodel)'
        )
Exemplo n.º 11
0
 def setUp(self):
     super(LiveSolrRoundTripTestCase, self).setUp()
     
     # Wipe it clean.
     clear_solr_index()
     
     # With the models registered, you get the proper bits.
     import haystack
     from haystack.sites import SearchSite
     
     # Stow.
     self.old_site = haystack.site
     test_site = SearchSite()
     test_site.register(MockModel, SolrRoundTripSearchIndex)
     haystack.site = test_site
     
     self.sqs = SearchQuerySet()
     
     # Fake indexing.
     sb = SearchBackend(site=test_site)
     srtsi = SolrRoundTripSearchIndex(MockModel)
     mock = MockModel()
     mock.id = 1
     sb.update(srtsi, [mock])
Exemplo n.º 12
0
    def setUp(self):
        super(LiveSolrRoundTripTestCase, self).setUp()

        # Wipe it clean.
        clear_solr_index()

        # With the models registered, you get the proper bits.
        import haystack
        from haystack.sites import SearchSite

        # Stow.
        self.old_site = haystack.site
        test_site = SearchSite()
        test_site.register(MockModel, SolrRoundTripSearchIndex)
        haystack.site = test_site

        self.sqs = SearchQuerySet()

        # Fake indexing.
        sb = SearchBackend(site=test_site)
        srtsi = SolrRoundTripSearchIndex(MockModel)
        mock = MockModel()
        mock.id = 1
        sb.update(srtsi, [mock])
Exemplo n.º 13
0
    def setUp(self):
        super(LiveSolrSearchQueryTestCase, self).setUp()

        # Wipe it clean.
        clear_solr_index()

        site = SearchSite()
        site.register(MockModel, SolrMockSearchIndex)
        sb = SearchBackend(site=site)
        smmi = SolrMockSearchIndex(MockModel, backend=sb)

        self.sq = SearchQuery(backend=sb)

        # Force indexing of the content.
        mockmodel_index = site.get_index(MockModel)
        mockmodel_index.update()
Exemplo n.º 14
0
    def setUp(self):
        super(MoreLikeThisTagTestCase, self).setUp()

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q='*:*')

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = MLTSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, MLTSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        # Force indexing of the content.
        for mock in MockModel.objects.all():
            mock.save()
Exemplo n.º 15
0
class SolrSearchBackendTestCase(TestCase):
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = "daniel%s" % i
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack

        haystack.site = self.old_site
        super(SolrSearchBackendTestCase, self).tearDown()

    def test_update(self):
        self.sb.update(self.smmi, self.sample_objs)

        # Check what Solr thinks is there.
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)
        self.assertEqual(
            self.raw_solr.search("*:*").docs,
            [
                {
                    "django_id": "1",
                    "django_ct": "core.mockmodel",
                    "name": "daniel1",
                    "name_exact": "daniel1",
                    "text": "Indexed!\n1",
                    "pub_date": "2009-02-24T00:00:00Z",
                    "id": "core.mockmodel.1",
                },
                {
                    "django_id": "2",
                    "django_ct": "core.mockmodel",
                    "name": "daniel2",
                    "name_exact": "daniel2",
                    "text": "Indexed!\n2",
                    "pub_date": "2009-02-23T00:00:00Z",
                    "id": "core.mockmodel.2",
                },
                {
                    "django_id": "3",
                    "django_ct": "core.mockmodel",
                    "name": "daniel3",
                    "name_exact": "daniel3",
                    "text": "Indexed!\n3",
                    "pub_date": "2009-02-22T00:00:00Z",
                    "id": "core.mockmodel.3",
                },
            ],
        )

    def test_remove(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.remove(self.sample_objs[0])
        self.assertEqual(self.raw_solr.search("*:*").hits, 2)
        self.assertEqual(
            self.raw_solr.search("*:*").docs,
            [
                {
                    "django_id": "2",
                    "django_ct": "core.mockmodel",
                    "name": "daniel2",
                    "name_exact": "daniel2",
                    "text": "Indexed!\n2",
                    "pub_date": "2009-02-23T00:00:00Z",
                    "id": "core.mockmodel.2",
                },
                {
                    "django_id": "3",
                    "django_ct": "core.mockmodel",
                    "name": "daniel3",
                    "name_exact": "daniel3",
                    "text": "Indexed!\n3",
                    "pub_date": "2009-02-22T00:00:00Z",
                    "id": "core.mockmodel.3",
                },
            ],
        )

    def test_clear(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear()
        self.assertEqual(self.raw_solr.search("*:*").hits, 0)

        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear([AnotherMockModel])
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear([MockModel])
        self.assertEqual(self.raw_solr.search("*:*").hits, 0)

        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear([AnotherMockModel, MockModel])
        self.assertEqual(self.raw_solr.search("*:*").hits, 0)

    def test_search(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.assertEqual(self.sb.search(""), {"hits": 0, "results": []})
        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.pk for result in self.sb.search("*:*")["results"]], ["1", "2", "3"])

        self.assertEqual(self.sb.search("", highlight=True), {"hits": 0, "results": []})
        self.assertEqual(self.sb.search("Index", highlight=True)["hits"], 3)
        self.assertEqual(
            [result.highlighted["text"][0] for result in self.sb.search("Index", highlight=True)["results"]],
            ["<em>Indexed</em>!\n1", "<em>Indexed</em>!\n2", "<em>Indexed</em>!\n3"],
        )

        self.assertEqual(self.sb.search("Indx")["hits"], 0)
        self.assertEqual(self.sb.search("Indx")["spelling_suggestion"], "index")
        self.assertEqual(self.sb.search("Indx", spelling_query="indexy")["spelling_suggestion"], "index")

        self.assertEqual(self.sb.search("", facets=["name"]), {"hits": 0, "results": []})
        results = self.sb.search("Index", facets=["name"])
        self.assertEqual(results["hits"], 3)
        self.assertEqual(results["facets"]["fields"]["name"], [("daniel1", 1), ("daniel2", 1), ("daniel3", 1)])

        self.assertEqual(
            self.sb.search(
                "",
                date_facets={
                    "pub_date": {
                        "start_date": datetime.date(2008, 2, 26),
                        "end_date": datetime.date(2008, 3, 26),
                        "gap_by": "month",
                        "gap_amount": 1,
                    }
                },
            ),
            {"hits": 0, "results": []},
        )
        results = self.sb.search(
            "Index",
            date_facets={
                "pub_date": {
                    "start_date": datetime.date(2008, 2, 26),
                    "end_date": datetime.date(2008, 3, 26),
                    "gap_by": "month",
                    "gap_amount": 1,
                }
            },
        )
        self.assertEqual(results["hits"], 3)
        # DRL_TODO: Correct output but no counts. Another case of needing better test data?
        # self.assertEqual(results['facets']['dates']['pub_date'], {'end': '2008-02-26T00:00:00Z', 'gap': '/MONTH'})

        self.assertEqual(self.sb.search("", query_facets=[("name", "[* TO e]")]), {"hits": 0, "results": []})
        results = self.sb.search("Index", query_facets=[("name", "[* TO e]")])
        self.assertEqual(results["hits"], 3)
        self.assertEqual(results["facets"]["queries"], {"name:[* TO e]": 3})

        self.assertEqual(self.sb.search("", narrow_queries=set(["name:daniel1"])), {"hits": 0, "results": []})
        results = self.sb.search("Index", narrow_queries=set(["name:daniel1"]))
        self.assertEqual(results["hits"], 1)

        # Ensure that swapping the ``result_class`` works.
        self.assertTrue(
            isinstance(self.sb.search(u"index document", result_class=MockSearchResult)["results"][0], MockSearchResult)
        )

        # Check the use of ``limit_to_registered_models``.
        self.assertEqual(self.sb.search("", limit_to_registered_models=False), {"hits": 0, "results": []})
        self.assertEqual(self.sb.search("*:*", limit_to_registered_models=False)["hits"], 3)
        self.assertEqual(
            [result.pk for result in self.sb.search("*:*", limit_to_registered_models=False)["results"]],
            ["1", "2", "3"],
        )

        # Stow.
        old_limit_to_registered_models = getattr(settings, "HAYSTACK_LIMIT_TO_REGISTERED_MODELS", True)
        settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = False

        self.assertEqual(self.sb.search(""), {"hits": 0, "results": []})
        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.pk for result in self.sb.search("*:*")["results"]], ["1", "2", "3"])

        # Restore.
        settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = old_limit_to_registered_models

    def test_use_correct_site(self):
        import haystack

        test_site = SearchSite()
        test_site.register(MockModel, SolrMockSearchIndex)
        self.sb.update(self.smmi, self.sample_objs)

        # Make sure that ``_process_results`` uses the right ``site``.
        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.pk for result in self.sb.search("*:*")["results"]], ["1", "2", "3"])

        haystack.site.unregister(MockModel)
        self.assertEqual(len(haystack.site.get_indexed_models()), 0)
        self.sb.site = test_site
        self.assertTrue(len(self.sb.site.get_indexed_models()) > 0)

        # Should still be there, despite the main ``site`` not having that model
        # registered any longer.
        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.pk for result in self.sb.search("*:*")["results"]], ["1", "2", "3"])

        # Unregister it on the backend & make sure it takes effect.
        self.sb.site.unregister(MockModel)
        self.assertEqual(len(self.sb.site.get_indexed_models()), 0)
        self.assertEqual(self.sb.search("*:*")["hits"], 0)

        # Nuke it & fallback on the main ``site``.
        self.sb.site = haystack.site
        self.assertEqual(self.sb.search("*:*")["hits"], 0)
        haystack.site.register(MockModel, SolrMockSearchIndex)
        self.assertEqual(self.sb.search("*:*")["hits"], 3)

    def test_more_like_this(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        # A functional MLT example with enough data to work is below. Rely on
        # this to ensure the API is correct enough.
        self.assertEqual(self.sb.more_like_this(self.sample_objs[0])["hits"], 0)
        self.assertEqual([result.pk for result in self.sb.more_like_this(self.sample_objs[0])["results"]], [])

    def test_build_schema(self):
        (content_field_name, fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, "text")
        self.assertEqual(len(fields), 4)
        self.assertEqual(
            fields,
            [
                {"indexed": "true", "type": "text", "stored": "true", "field_name": "text", "multi_valued": "false"},
                {
                    "indexed": "true",
                    "type": "date",
                    "stored": "true",
                    "field_name": "pub_date",
                    "multi_valued": "false",
                },
                {"indexed": "true", "type": "text", "stored": "true", "field_name": "name", "multi_valued": "false"},
                {
                    "indexed": "true",
                    "field_name": "name_exact",
                    "stored": "true",
                    "type": "string",
                    "multi_valued": "false",
                },
            ],
        )

        self.site.unregister(MockModel)
        self.site.register(MockModel, SolrComplexFacetsMockSearchIndex)
        (content_field_name, fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, "text")
        self.assertEqual(len(fields), 15)
        self.assertEqual(
            fields,
            [
                {"indexed": "true", "type": "text", "stored": "true", "field_name": "name", "multi_valued": "false"},
                {
                    "indexed": "true",
                    "type": "boolean",
                    "stored": "true",
                    "field_name": "is_active_exact",
                    "multi_valued": "false",
                },
                {"indexed": "true", "type": "date", "stored": "true", "field_name": "created", "multi_valued": "false"},
                {
                    "indexed": "true",
                    "type": "slong",
                    "stored": "true",
                    "field_name": "post_count",
                    "multi_valued": "false",
                },
                {
                    "indexed": "true",
                    "type": "date",
                    "stored": "true",
                    "field_name": "created_exact",
                    "multi_valued": "false",
                },
                {
                    "indexed": "true",
                    "type": "string",
                    "stored": "true",
                    "field_name": "sites_exact",
                    "multi_valued": "true",
                },
                {
                    "indexed": "true",
                    "type": "boolean",
                    "stored": "true",
                    "field_name": "is_active",
                    "multi_valued": "false",
                },
                {"indexed": "true", "type": "text", "stored": "true", "field_name": "sites", "multi_valued": "true"},
                {
                    "indexed": "true",
                    "type": "slong",
                    "stored": "true",
                    "field_name": "post_count_i",
                    "multi_valued": "false",
                },
                {
                    "indexed": "true",
                    "type": "sfloat",
                    "stored": "true",
                    "field_name": "average_rating",
                    "multi_valued": "false",
                },
                {"indexed": "true", "type": "text", "stored": "true", "field_name": "text", "multi_valued": "false"},
                {
                    "indexed": "true",
                    "type": "date",
                    "stored": "true",
                    "field_name": "pub_date_exact",
                    "multi_valued": "false",
                },
                {
                    "indexed": "true",
                    "type": "string",
                    "stored": "true",
                    "field_name": "name_exact",
                    "multi_valued": "false",
                },
                {
                    "indexed": "true",
                    "type": "date",
                    "stored": "true",
                    "field_name": "pub_date",
                    "multi_valued": "false",
                },
                {
                    "indexed": "true",
                    "type": "sfloat",
                    "stored": "true",
                    "field_name": "average_rating_exact",
                    "multi_valued": "false",
                },
            ],
        )

    def test_verify_type(self):
        import haystack

        haystack.site.unregister(MockModel)
        haystack.site.register(MockModel, SolrMaintainTypeMockSearchIndex)
        self.sb.update(self.smtmmi, self.sample_objs)

        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.month for result in self.sb.search("*:*")["results"]], [u"02", u"02", u"02"])
Exemplo n.º 16
0
class SolrSearchBackendTestCase(TestCase):
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Stow.
        self.old_solr_url = getattr(settings, "HAYSTACK_SOLR_URL", "http://localhost:9001/solr/test_default")
        settings.HAYSTACK_SOLR_URL = "http://localhost:9001/solr/test_default"

        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q="*:*")

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack

        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = "daniel%s" % i
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack

        haystack.site = self.old_site
        settings.HAYSTACK_SOLR_URL = self.old_solr_url
        super(SolrSearchBackendTestCase, self).tearDown()

    def test_update(self):
        self.sb.update(self.smmi, self.sample_objs)

        # Check what Solr thinks is there.
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)
        self.assertEqual(
            self.raw_solr.search("*:*").docs,
            [
                {
                    "django_id": "1",
                    "django_ct": "core.mockmodel",
                    "name": "daniel1",
                    "text": "Indexed!\n1",
                    "pub_date": "2009-02-24T00:00:00Z",
                    "id": "core.mockmodel.1",
                },
                {
                    "django_id": "2",
                    "django_ct": "core.mockmodel",
                    "name": "daniel2",
                    "text": "Indexed!\n2",
                    "pub_date": "2009-02-23T00:00:00Z",
                    "id": "core.mockmodel.2",
                },
                {
                    "django_id": "3",
                    "django_ct": "core.mockmodel",
                    "name": "daniel3",
                    "text": "Indexed!\n3",
                    "pub_date": "2009-02-22T00:00:00Z",
                    "id": "core.mockmodel.3",
                },
            ],
        )

    def test_remove(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.remove(self.sample_objs[0])
        self.assertEqual(self.raw_solr.search("*:*").hits, 2)
        self.assertEqual(
            self.raw_solr.search("*:*").docs,
            [
                {
                    "django_id": "2",
                    "django_ct": "core.mockmodel",
                    "name": "daniel2",
                    "text": "Indexed!\n2",
                    "pub_date": "2009-02-23T00:00:00Z",
                    "id": "core.mockmodel.2",
                },
                {
                    "django_id": "3",
                    "django_ct": "core.mockmodel",
                    "name": "daniel3",
                    "text": "Indexed!\n3",
                    "pub_date": "2009-02-22T00:00:00Z",
                    "id": "core.mockmodel.3",
                },
            ],
        )

    def test_clear(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear()
        self.assertEqual(self.raw_solr.search("*:*").hits, 0)

        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear([AnotherMockModel])
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear([MockModel])
        self.assertEqual(self.raw_solr.search("*:*").hits, 0)

        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.sb.clear([AnotherMockModel, MockModel])
        self.assertEqual(self.raw_solr.search("*:*").hits, 0)

    def test_search(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        self.assertEqual(self.sb.search(""), {"hits": 0, "results": []})
        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.pk for result in self.sb.search("*:*")["results"]], ["1", "2", "3"])

        self.assertEqual(self.sb.search("", highlight=True), {"hits": 0, "results": []})
        self.assertEqual(self.sb.search("Index", highlight=True)["hits"], 3)
        self.assertEqual(
            [result.highlighted["text"][0] for result in self.sb.search("Index", highlight=True)["results"]],
            ["<em>Indexed</em>!\n1", "<em>Indexed</em>!\n2", "<em>Indexed</em>!\n3"],
        )

        self.assertEqual(self.sb.search("Indx")["hits"], 0)
        self.assertEqual(self.sb.search("Indx")["spelling_suggestion"], "index")
        self.assertEqual(self.sb.search("Indx", spelling_query="indexy")["spelling_suggestion"], "index")

        self.assertEqual(self.sb.search("", facets=["name"]), {"hits": 0, "results": []})
        results = self.sb.search("Index", facets=["name"])
        self.assertEqual(results["hits"], 3)
        self.assertEqual(results["facets"]["fields"]["name"], [("daniel1", 1), ("daniel2", 1), ("daniel3", 1)])

        self.assertEqual(
            self.sb.search(
                "",
                date_facets={
                    "pub_date": {
                        "start_date": datetime.date(2008, 2, 26),
                        "end_date": datetime.date(2008, 3, 26),
                        "gap_by": "month",
                        "gap_amount": 1,
                    }
                },
            ),
            {"hits": 0, "results": []},
        )
        results = self.sb.search(
            "Index",
            date_facets={
                "pub_date": {
                    "start_date": datetime.date(2008, 2, 26),
                    "end_date": datetime.date(2008, 3, 26),
                    "gap_by": "month",
                    "gap_amount": 1,
                }
            },
        )
        self.assertEqual(results["hits"], 3)
        # DRL_TODO: Correct output but no counts. Another case of needing better test data?
        # self.assertEqual(results['facets']['dates']['pub_date'], {'end': '2008-02-26T00:00:00Z', 'gap': '/MONTH'})

        self.assertEqual(self.sb.search("", query_facets={"name": "[* TO e]"}), {"hits": 0, "results": []})
        results = self.sb.search("Index", query_facets={"name": "[* TO e]"})
        self.assertEqual(results["hits"], 3)
        self.assertEqual(results["facets"]["queries"], {"name:[* TO e]": 3})

        self.assertEqual(self.sb.search("", narrow_queries=set(["name:daniel1"])), {"hits": 0, "results": []})
        results = self.sb.search("Index", narrow_queries=set(["name:daniel1"]))
        self.assertEqual(results["hits"], 1)

    def test_more_like_this(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search("*:*").hits, 3)

        # A functional MLT example with enough data to work is below. Rely on
        # this to ensure the API is correct enough.
        self.assertEqual(self.sb.more_like_this(self.sample_objs[0])["hits"], 0)
        self.assertEqual([result.pk for result in self.sb.more_like_this(self.sample_objs[0])["results"]], [])

    def test_build_schema(self):
        (content_field_name, fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, "text")
        self.assertEqual(len(fields), 3)
        self.assertEqual(
            fields,
            [
                {"indexed": "true", "type": "text", "field_name": "text", "multi_valued": "false"},
                {"indexed": "true", "type": "date", "field_name": "pub_date", "multi_valued": "false"},
                {"indexed": "true", "type": "text", "field_name": "name", "multi_valued": "false"},
            ],
        )

    def test_verify_type(self):
        import haystack

        haystack.site.unregister(MockModel)
        haystack.site.register(MockModel, SolrMaintainTypeMockSearchIndex)
        self.sb.update(self.smtmmi, self.sample_objs)

        self.assertEqual(self.sb.search("*:*")["hits"], 3)
        self.assertEqual([result.month for result in self.sb.search("*:*")["results"]], [u"02", u"02", u"02"])
Exemplo n.º 17
0
 def test_all_cases(self):
     self.sample_objs = []
     
     for i in xrange(1, 4):
         mock = MockModel()
         mock.id = i
         mock.author = 'daniel%s' % i
         mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
         self.sample_objs.append(mock)
     
     # Stow.
     # Point the backend at a URL that doesn't exist so we can watch the
     # sparks fly.
     old_solr_url = settings.HAYSTACK_SOLR_URL
     settings.HAYSTACK_SOLR_URL = "%s/foo/" % settings.HAYSTACK_SOLR_URL
     cap = CaptureHandler()
     logging.getLogger('haystack').addHandler(cap)
     import haystack
     logging.getLogger('haystack').removeHandler(haystack.stream)
     
     # Setup the rest of the bits.
     site = SearchSite()
     site.register(MockModel, SolrMockSearchIndex)
     sb = SearchBackend(site=site)
     smmi = SolrMockSearchIndex(MockModel, backend=sb)
     
     # Prior to the addition of the try/except bits, these would all fail miserably.
     self.assertEqual(len(CaptureHandler.logs_seen), 0)
     sb.update(smmi, self.sample_objs)
     self.assertEqual(len(CaptureHandler.logs_seen), 1)
     sb.remove(self.sample_objs[0])
     self.assertEqual(len(CaptureHandler.logs_seen), 2)
     sb.search('search')
     self.assertEqual(len(CaptureHandler.logs_seen), 3)
     sb.more_like_this(self.sample_objs[0])
     self.assertEqual(len(CaptureHandler.logs_seen), 4)
     sb.clear([MockModel])
     self.assertEqual(len(CaptureHandler.logs_seen), 5)
     sb.clear()
     self.assertEqual(len(CaptureHandler.logs_seen), 6)
     
     # Restore.
     settings.HAYSTACK_SOLR_URL = old_solr_url
     logging.getLogger('haystack').removeHandler(cap)
     logging.getLogger('haystack').addHandler(haystack.stream)
Exemplo n.º 18
0
class SolrSearchBackendTestCase(TestCase):
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()

        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()

        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel,
                                                      backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)

        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site

        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(SolrSearchBackendTestCase, self).tearDown()

    def test_update(self):
        self.sb.update(self.smmi, self.sample_objs)

        # Check what Solr thinks is there.
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        self.assertEqual(
            self.raw_solr.search('*:*').docs, [{
                'django_id': '1',
                'django_ct': 'core.mockmodel',
                'name': 'daniel1',
                'name_exact': 'daniel1',
                'text': 'Indexed!\n1',
                'pub_date': '2009-02-24T00:00:00Z',
                'id': 'core.mockmodel.1'
            }, {
                'django_id': '2',
                'django_ct': 'core.mockmodel',
                'name': 'daniel2',
                'name_exact': 'daniel2',
                'text': 'Indexed!\n2',
                'pub_date': '2009-02-23T00:00:00Z',
                'id': 'core.mockmodel.2'
            }, {
                'django_id': '3',
                'django_ct': 'core.mockmodel',
                'name': 'daniel3',
                'name_exact': 'daniel3',
                'text': 'Indexed!\n3',
                'pub_date': '2009-02-22T00:00:00Z',
                'id': 'core.mockmodel.3'
            }])

    def test_remove(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        self.sb.remove(self.sample_objs[0])
        self.assertEqual(self.raw_solr.search('*:*').hits, 2)
        self.assertEqual(
            self.raw_solr.search('*:*').docs, [{
                'django_id': '2',
                'django_ct': 'core.mockmodel',
                'name': 'daniel2',
                'name_exact': 'daniel2',
                'text': 'Indexed!\n2',
                'pub_date': '2009-02-23T00:00:00Z',
                'id': 'core.mockmodel.2'
            }, {
                'django_id': '3',
                'django_ct': 'core.mockmodel',
                'name': 'daniel3',
                'name_exact': 'daniel3',
                'text': 'Indexed!\n3',
                'pub_date': '2009-02-22T00:00:00Z',
                'id': 'core.mockmodel.3'
            }])

    def test_clear(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        self.sb.clear()
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)

        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        self.sb.clear([AnotherMockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        self.sb.clear([MockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)

        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        self.sb.clear([AnotherMockModel, MockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)

    def test_search(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        self.assertEqual(self.sb.search(''), {'hits': 0, 'results': []})
        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual(
            [result.pk for result in self.sb.search('*:*')['results']],
            ['1', '2', '3'])

        self.assertEqual(self.sb.search('', highlight=True), {
            'hits': 0,
            'results': []
        })
        self.assertEqual(self.sb.search('Index', highlight=True)['hits'], 3)
        self.assertEqual([
            result.highlighted['text'][0]
            for result in self.sb.search('Index', highlight=True)['results']
        ], [
            '<em>Indexed</em>!\n1', '<em>Indexed</em>!\n2',
            '<em>Indexed</em>!\n3'
        ])

        self.assertEqual(self.sb.search('Indx')['hits'], 0)
        self.assertEqual(
            self.sb.search('Indx')['spelling_suggestion'], 'index')
        self.assertEqual(
            self.sb.search('Indx',
                           spelling_query='indexy')['spelling_suggestion'],
            'index')

        self.assertEqual(self.sb.search('', facets=['name']), {
            'hits': 0,
            'results': []
        })
        results = self.sb.search('Index', facets=['name'])
        self.assertEqual(results['hits'], 3)
        self.assertEqual(results['facets']['fields']['name'], [('daniel1', 1),
                                                               ('daniel2', 1),
                                                               ('daniel3', 1)])

        self.assertEqual(
            self.sb.search('',
                           date_facets={
                               'pub_date': {
                                   'start_date': datetime.date(2008, 2, 26),
                                   'end_date': datetime.date(2008, 3, 26),
                                   'gap_by': 'month',
                                   'gap_amount': 1
                               }
                           }), {
                               'hits': 0,
                               'results': []
                           })
        results = self.sb.search('Index',
                                 date_facets={
                                     'pub_date': {
                                         'start_date':
                                         datetime.date(2008, 2, 26),
                                         'end_date':
                                         datetime.date(2008, 3, 26),
                                         'gap_by': 'month',
                                         'gap_amount': 1
                                     }
                                 })
        self.assertEqual(results['hits'], 3)
        # DRL_TODO: Correct output but no counts. Another case of needing better test data?
        # self.assertEqual(results['facets']['dates']['pub_date'], {'end': '2008-02-26T00:00:00Z', 'gap': '/MONTH'})

        self.assertEqual(
            self.sb.search('', query_facets=[('name', '[* TO e]')]), {
                'hits': 0,
                'results': []
            })
        results = self.sb.search('Index', query_facets=[('name', '[* TO e]')])
        self.assertEqual(results['hits'], 3)
        self.assertEqual(results['facets']['queries'], {'name:[* TO e]': 3})

        self.assertEqual(
            self.sb.search('', narrow_queries=set(['name:daniel1'])), {
                'hits': 0,
                'results': []
            })
        results = self.sb.search('Index', narrow_queries=set(['name:daniel1']))
        self.assertEqual(results['hits'], 1)

        # Ensure that swapping the ``result_class`` works.
        self.assertTrue(
            isinstance(
                self.sb.search(u'index document',
                               result_class=MockSearchResult)['results'][0],
                MockSearchResult))

        # Check the use of ``limit_to_registered_models``.
        self.assertEqual(self.sb.search('', limit_to_registered_models=False),
                         {
                             'hits': 0,
                             'results': []
                         })
        self.assertEqual(
            self.sb.search('*:*', limit_to_registered_models=False)['hits'], 3)
        self.assertEqual([
            result.pk for result in self.sb.search(
                '*:*', limit_to_registered_models=False)['results']
        ], ['1', '2', '3'])

        # Stow.
        old_limit_to_registered_models = getattr(
            settings, 'HAYSTACK_LIMIT_TO_REGISTERED_MODELS', True)
        settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = False

        self.assertEqual(self.sb.search(''), {'hits': 0, 'results': []})
        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual(
            [result.pk for result in self.sb.search('*:*')['results']],
            ['1', '2', '3'])

        # Restore.
        settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = old_limit_to_registered_models

    def test_more_like_this(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)

        # A functional MLT example with enough data to work is below. Rely on
        # this to ensure the API is correct enough.
        self.assertEqual(
            self.sb.more_like_this(self.sample_objs[0])['hits'], 0)
        self.assertEqual([
            result.pk for result in self.sb.more_like_this(self.sample_objs[0])
            ['results']
        ], [])

    def test_build_schema(self):
        (content_field_name,
         fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, 'text')
        self.assertEqual(len(fields), 4)
        self.assertEqual(fields, [{
            'indexed': 'true',
            'type': 'text',
            'stored': 'true',
            'field_name': 'text',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'date',
            'stored': 'true',
            'field_name': 'pub_date',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'text',
            'stored': 'true',
            'field_name': 'name',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'field_name': 'name_exact',
            'stored': 'true',
            'type': 'string',
            'multi_valued': 'false'
        }])

        self.site.unregister(MockModel)
        self.site.register(MockModel, SolrComplexFacetsMockSearchIndex)
        (content_field_name,
         fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, 'text')
        self.assertEqual(len(fields), 15)
        self.assertEqual(fields, [{
            'indexed': 'true',
            'type': 'text',
            'stored': 'true',
            'field_name': 'name',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'boolean',
            'stored': 'true',
            'field_name': 'is_active_exact',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'date',
            'stored': 'true',
            'field_name': 'created',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'slong',
            'stored': 'true',
            'field_name': 'post_count',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'date',
            'stored': 'true',
            'field_name': 'created_exact',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'string',
            'stored': 'true',
            'field_name': 'sites_exact',
            'multi_valued': 'true'
        }, {
            'indexed': 'true',
            'type': 'boolean',
            'stored': 'true',
            'field_name': 'is_active',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'text',
            'stored': 'true',
            'field_name': 'sites',
            'multi_valued': 'true'
        }, {
            'indexed': 'true',
            'type': 'slong',
            'stored': 'true',
            'field_name': 'post_count_i',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'sfloat',
            'stored': 'true',
            'field_name': 'average_rating',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'text',
            'stored': 'true',
            'field_name': 'text',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'date',
            'stored': 'true',
            'field_name': 'pub_date_exact',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'string',
            'stored': 'true',
            'field_name': 'name_exact',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'date',
            'stored': 'true',
            'field_name': 'pub_date',
            'multi_valued': 'false'
        }, {
            'indexed': 'true',
            'type': 'sfloat',
            'stored': 'true',
            'field_name': 'average_rating_exact',
            'multi_valued': 'false'
        }])

    def test_verify_type(self):
        import haystack
        haystack.site.unregister(MockModel)
        haystack.site.register(MockModel, SolrMaintainTypeMockSearchIndex)
        self.sb.update(self.smtmmi, self.sample_objs)

        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual(
            [result.month for result in self.sb.search('*:*')['results']],
            [u'02', u'02', u'02'])
Exemplo n.º 19
0
class SolrSearchBackendTestCase(TestCase):
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()
        
        # Wipe it clean.
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        clear_solr_index()
        
        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.smtmmi = SolrMaintainTypeMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)
        
        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
        
        self.sample_objs = []
        
        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
    
    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        super(SolrSearchBackendTestCase, self).tearDown()
    
    def test_update(self):
        self.sb.update(self.smmi, self.sample_objs)
        
        # Check what Solr thinks is there.
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        self.assertEqual(self.raw_solr.search('*:*').docs, [
            {
                'django_id': '1',
                'django_ct': 'core.mockmodel',
                'name': 'daniel1',
                'name_exact': 'daniel1',
                'text': 'Indexed!\n1',
                'pub_date': '2009-02-24T00:00:00Z',
                'id': 'core.mockmodel.1'
            },
            {
                'django_id': '2',
                'django_ct': 'core.mockmodel',
                'name': 'daniel2',
                'name_exact': 'daniel2',
                'text': 'Indexed!\n2',
                'pub_date': '2009-02-23T00:00:00Z',
                'id': 'core.mockmodel.2'
            },
            {
                'django_id': '3',
                'django_ct': 'core.mockmodel',
                'name': 'daniel3',
                'name_exact': 'daniel3',
                'text': 'Indexed!\n3',
                'pub_date': '2009-02-22T00:00:00Z',
                'id': 'core.mockmodel.3'
            }
        ])
    
    def test_remove(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.remove(self.sample_objs[0])
        self.assertEqual(self.raw_solr.search('*:*').hits, 2)
        self.assertEqual(self.raw_solr.search('*:*').docs, [
            {
                'django_id': '2',
                'django_ct': 'core.mockmodel',
                'name': 'daniel2',
                'name_exact': 'daniel2',
                'text': 'Indexed!\n2',
                'pub_date': '2009-02-23T00:00:00Z',
                'id': 'core.mockmodel.2'
            },
            {
                'django_id': '3',
                'django_ct': 'core.mockmodel',
                'name': 'daniel3',
                'name_exact': 'daniel3',
                'text': 'Indexed!\n3',
                'pub_date': '2009-02-22T00:00:00Z',
                'id': 'core.mockmodel.3'
            }
        ])
    
    def test_clear(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear()
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)
        
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear([AnotherMockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear([MockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)
        
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear([AnotherMockModel, MockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)
    
    def test_search(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.assertEqual(self.sb.search(''), {'hits': 0, 'results': []})
        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual([result.pk for result in self.sb.search('*:*')['results']], ['1', '2', '3'])
        
        self.assertEqual(self.sb.search('', highlight=True), {'hits': 0, 'results': []})
        self.assertEqual(self.sb.search('Index', highlight=True)['hits'], 3)
        self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index', highlight=True)['results']], ['<em>Indexed</em>!\n1', '<em>Indexed</em>!\n2', '<em>Indexed</em>!\n3'])
        
        self.assertEqual(self.sb.search('Indx')['hits'], 0)
        self.assertEqual(self.sb.search('Indx')['spelling_suggestion'], 'index')
        self.assertEqual(self.sb.search('Indx', spelling_query='indexy')['spelling_suggestion'], 'index')
        
        self.assertEqual(self.sb.search('', facets=['name']), {'hits': 0, 'results': []})
        results = self.sb.search('Index', facets=['name'])
        self.assertEqual(results['hits'], 3)
        self.assertEqual(results['facets']['fields']['name'], [('daniel1', 1), ('daniel2', 1), ('daniel3', 1)])
        
        self.assertEqual(self.sb.search('', date_facets={'pub_date': {'start_date': datetime.date(2008, 2, 26), 'end_date': datetime.date(2008, 3, 26), 'gap_by': 'month', 'gap_amount': 1}}), {'hits': 0, 'results': []})
        results = self.sb.search('Index', date_facets={'pub_date': {'start_date': datetime.date(2008, 2, 26), 'end_date': datetime.date(2008, 3, 26), 'gap_by': 'month', 'gap_amount': 1}})
        self.assertEqual(results['hits'], 3)
        # DRL_TODO: Correct output but no counts. Another case of needing better test data?
        # self.assertEqual(results['facets']['dates']['pub_date'], {'end': '2008-02-26T00:00:00Z', 'gap': '/MONTH'})
        
        self.assertEqual(self.sb.search('', query_facets=[('name', '[* TO e]')]), {'hits': 0, 'results': []})
        results = self.sb.search('Index', query_facets=[('name', '[* TO e]')])
        self.assertEqual(results['hits'], 3)
        self.assertEqual(results['facets']['queries'], {'name:[* TO e]': 3})
        
        self.assertEqual(self.sb.search('', narrow_queries=set(['name:daniel1'])), {'hits': 0, 'results': []})
        results = self.sb.search('Index', narrow_queries=set(['name:daniel1']))
        self.assertEqual(results['hits'], 1)
        
        # Check the use of ``limit_to_registered_models``.
        self.assertEqual(self.sb.search('', limit_to_registered_models=False), {'hits': 0, 'results': []})
        self.assertEqual(self.sb.search('*:*', limit_to_registered_models=False)['hits'], 3)
        self.assertEqual([result.pk for result in self.sb.search('*:*', limit_to_registered_models=False)['results']], ['1', '2', '3'])
        
        # Stow.
        old_limit_to_registered_models = getattr(settings, 'HAYSTACK_LIMIT_TO_REGISTERED_MODELS', True)
        settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = False
        
        self.assertEqual(self.sb.search(''), {'hits': 0, 'results': []})
        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual([result.pk for result in self.sb.search('*:*')['results']], ['1', '2', '3'])
        
        # Restore.
        settings.HAYSTACK_LIMIT_TO_REGISTERED_MODELS = old_limit_to_registered_models
    
    def test_more_like_this(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        # A functional MLT example with enough data to work is below. Rely on
        # this to ensure the API is correct enough.
        self.assertEqual(self.sb.more_like_this(self.sample_objs[0])['hits'], 0)
        self.assertEqual([result.pk for result in self.sb.more_like_this(self.sample_objs[0])['results']], [])
    
    def test_build_schema(self):
        (content_field_name, fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, 'text')
        self.assertEqual(len(fields), 4)
        self.assertEqual(fields, [
            {
                'indexed': 'true',
                'type': 'text',
                'stored': 'true',
                'field_name': 'text',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'date',
                'stored': 'true',
                'field_name': 'pub_date',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'text',
                'stored': 'true',
                'field_name': 'name',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'field_name': 'name_exact',
                'stored': 'true',
                'type': 'string',
                'multi_valued': 'false'
            }
        ])
        
        self.site.unregister(MockModel)
        self.site.register(MockModel, SolrComplexFacetsMockSearchIndex)
        (content_field_name, fields) = self.sb.build_schema(self.site.all_searchfields())
        self.assertEqual(content_field_name, 'text')
        self.assertEqual(len(fields), 15)
        self.assertEqual(fields, [
            {
                'indexed': 'true',
                'type': 'text',
                'stored': 'true',
                'field_name': 'name',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'boolean',
                'stored': 'true',
                'field_name': 'is_active_exact',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'date',
                'stored': 'true',
                'field_name': 'created',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'slong',
                'stored': 'true',
                'field_name': 'post_count',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'date',
                'stored': 'true',
                'field_name': 'created_exact',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'string',
                'stored': 'true',
                'field_name': 'sites_exact',
                'multi_valued': 'true'
            },
            {
                'indexed': 'true',
                'type': 'boolean',
                'stored': 'true',
                'field_name': 'is_active',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'text',
                'stored': 'true',
                'field_name': 'sites',
                'multi_valued': 'true'
            },
            {
                'indexed': 'true',
                'type': 'slong',
                'stored': 'true',
                'field_name': 'post_count_i',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'sfloat',
                'stored': 'true',
                'field_name': 'average_rating',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'text',
                'stored': 'true',
                'field_name': 'text',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'date',
                'stored': 'true',
                'field_name': 'pub_date_exact',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'string',
                'stored': 'true',
                'field_name': 'name_exact',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'date',
                'stored': 'true',
                'field_name': 'pub_date',
                'multi_valued': 'false'
            },
            {
                'indexed': 'true',
                'type': 'sfloat',
                'stored': 'true',
                'field_name': 'average_rating_exact',
                'multi_valued': 'false'
            }
        ])
    
    def test_verify_type(self):
        import haystack
        haystack.site.unregister(MockModel)
        haystack.site.register(MockModel, SolrMaintainTypeMockSearchIndex)
        self.sb.update(self.smtmmi, self.sample_objs)
        
        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual([result.month for result in self.sb.search('*:*')['results']], [u'02', u'02', u'02'])
Exemplo n.º 20
0
 def setUp(self):
     super(SolrSearchQueryTestCase, self).setUp()
     self.sq = SearchQuery(backend=SearchBackend())
Exemplo n.º 21
0
class SolrSearchBackendTestCase(TestCase):
    def setUp(self):
        super(SolrSearchBackendTestCase, self).setUp()
        
        # Stow.
        self.old_solr_url = getattr(settings, 'HAYSTACK_SOLR_URL', 'http://localhost:9001/solr/test_default')
        settings.HAYSTACK_SOLR_URL = 'http://localhost:9001/solr/test_default'
        
        self.raw_solr = pysolr.Solr(settings.HAYSTACK_SOLR_URL)
        self.raw_solr.delete(q='*:*')
        
        self.site = SearchSite()
        self.sb = SearchBackend(site=self.site)
        self.smmi = SolrMockSearchIndex(MockModel, backend=self.sb)
        self.site.register(MockModel, SolrMockSearchIndex)
        
        # Stow.
        import haystack
        self.old_site = haystack.site
        haystack.site = self.site
        
        self.sample_objs = []
        
        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2, 25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)
    
    def tearDown(self):
        import haystack
        haystack.site = self.old_site
        settings.HAYSTACK_SOLR_URL = self.old_solr_url
        super(SolrSearchBackendTestCase, self).tearDown()
    
    def test_update(self):
        self.sb.update(self.smmi, self.sample_objs)
        
        # Check what Solr thinks is there.
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        self.assertEqual(self.raw_solr.search('*:*').docs, [{'django_id': '1', 'django_ct': 'core.mockmodel', 'name': 'daniel1', 'text': 'Indexed!\n1', 'pub_date': '2009-02-24T00:00:00Z', 'id': 'core.mockmodel.1'}, {'django_id': '2', 'django_ct': 'core.mockmodel', 'name': 'daniel2', 'text': 'Indexed!\n2', 'pub_date': '2009-02-23T00:00:00Z', 'id': 'core.mockmodel.2'}, {'django_id': '3', 'django_ct': 'core.mockmodel', 'name': 'daniel3', 'text': 'Indexed!\n3', 'pub_date': '2009-02-22T00:00:00Z', 'id': 'core.mockmodel.3'}])
    
    def test_remove(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.remove(self.sample_objs[0])
        self.assertEqual(self.raw_solr.search('*:*').hits, 2)
        self.assertEqual(self.raw_solr.search('*:*').docs, [{'django_id': '2', 'django_ct': 'core.mockmodel', 'name': 'daniel2', 'text': 'Indexed!\n2', 'pub_date': '2009-02-23T00:00:00Z', 'id': 'core.mockmodel.2'}, {'django_id': '3', 'django_ct': 'core.mockmodel', 'name': 'daniel3', 'text': 'Indexed!\n3', 'pub_date': '2009-02-22T00:00:00Z', 'id': 'core.mockmodel.3'}])
    
    def test_clear(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear()
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)
        
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear([AnotherMockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear([MockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)
        
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.sb.clear([AnotherMockModel, MockModel])
        self.assertEqual(self.raw_solr.search('*:*').hits, 0)
    
    def test_search(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        self.assertEqual(self.sb.search(''), [])
        self.assertEqual(self.sb.search('*:*')['hits'], 3)
        self.assertEqual([result.pk for result in self.sb.search('*:*')['results']], ['1', '2', '3'])
        
        self.assertEqual(self.sb.search('', highlight=True), [])
        self.assertEqual(self.sb.search('Index', highlight=True)['hits'], 3)
        self.assertEqual([result.highlighted['text'][0] for result in self.sb.search('Index', highlight=True)['results']], ['<em>Indexed</em>!\n1', '<em>Indexed</em>!\n2', '<em>Indexed</em>!\n3'])
        
        self.assertEqual(self.sb.search('Indx')['hits'], 0)
        self.assertEqual(self.sb.search('Indx')['spelling_suggestion'], 'index')
        
        self.assertEqual(self.sb.search('', facets=['name']), [])
        results = self.sb.search('Index', facets=['name'])
        self.assertEqual(results['hits'], 3)
        self.assertEqual(results['facets']['fields']['name'], [('daniel1', 1), ('daniel2', 1), ('daniel3', 1)])
        
        self.assertEqual(self.sb.search('', date_facets={'pub_date': {'start_date': datetime.date(2008, 2, 26), 'end_date': datetime.date(2008, 2, 26), 'gap': '/MONTH'}}), [])
        results = self.sb.search('Index', date_facets={'pub_date': {'start_date': datetime.date(2008, 2, 26), 'end_date': datetime.date(2008, 2, 26), 'gap': '/MONTH'}})
        self.assertEqual(results['hits'], 3)
        # DRL_TODO: Correct output but no counts. Another case of needing better test data?
        self.assertEqual(results['facets']['dates']['pub_date'], {'end': '2008-02-26T00:00:00Z', 'gap': '/MONTH'})
        
        self.assertEqual(self.sb.search('', query_facets={'name': '[* TO e]'}), [])
        results = self.sb.search('Index', query_facets={'name': '[* TO e]'})
        self.assertEqual(results['hits'], 3)
        self.assertEqual(results['facets']['queries'], {'name:[* TO e]': 3})
        
        self.assertEqual(self.sb.search('', narrow_queries=['name:daniel1']), [])
        results = self.sb.search('Index', narrow_queries=['name:daniel1'])
        self.assertEqual(results['hits'], 1)
    
    def test_more_like_this(self):
        self.sb.update(self.smmi, self.sample_objs)
        self.assertEqual(self.raw_solr.search('*:*').hits, 3)
        
        # A functional MLT example with enough data to work is below. Rely on
        # this to ensure the API is correct enough.
        self.assertEqual(self.sb.more_like_this(self.sample_objs[0])['hits'], 0)
        self.assertEqual([result.pk for result in self.sb.more_like_this(self.sample_objs[0])['results']], [])
Exemplo n.º 22
0
    def test_all_cases(self):
        self.sample_objs = []

        for i in xrange(1, 4):
            mock = MockModel()
            mock.id = i
            mock.author = 'daniel%s' % i
            mock.pub_date = datetime.date(2009, 2,
                                          25) - datetime.timedelta(days=i)
            self.sample_objs.append(mock)

        # Stow.
        # Point the backend at a URL that doesn't exist so we can watch the
        # sparks fly.
        old_solr_url = settings.HAYSTACK_SOLR_URL
        settings.HAYSTACK_SOLR_URL = "%s/foo/" % settings.HAYSTACK_SOLR_URL
        cap = CaptureHandler()
        logging.getLogger('haystack').addHandler(cap)
        import haystack
        logging.getLogger('haystack').removeHandler(haystack.stream)

        # Setup the rest of the bits.
        site = SearchSite()
        site.register(MockModel, SolrMockSearchIndex)
        sb = SearchBackend(site=site)
        smmi = SolrMockSearchIndex(MockModel, backend=sb)

        # Prior to the addition of the try/except bits, these would all fail miserably.
        self.assertEqual(len(CaptureHandler.logs_seen), 0)
        sb.update(smmi, self.sample_objs)
        self.assertEqual(len(CaptureHandler.logs_seen), 1)
        sb.remove(self.sample_objs[0])
        self.assertEqual(len(CaptureHandler.logs_seen), 2)
        sb.search('search')
        self.assertEqual(len(CaptureHandler.logs_seen), 3)
        sb.more_like_this(self.sample_objs[0])
        self.assertEqual(len(CaptureHandler.logs_seen), 4)
        sb.clear([MockModel])
        self.assertEqual(len(CaptureHandler.logs_seen), 5)
        sb.clear()
        self.assertEqual(len(CaptureHandler.logs_seen), 6)

        # Restore.
        settings.HAYSTACK_SOLR_URL = old_solr_url
        logging.getLogger('haystack').removeHandler(cap)
        logging.getLogger('haystack').addHandler(haystack.stream)