Esempio n. 1
0
    def test_search(self):
        responses = MultipleResponse()

        responses.push(self.index.save_objects([
            {"company": "Algolia", "name": "Julien Lemoine",
             "objectID": "julien-lemoine"},  # noqa: E501
            {"company": "Algolia", "name": "Nicolas Dessaigne",
             "objectID": "nicolas-dessaigne"},  # noqa: E501
            {"company": "Amazon", "name": "Jeff Bezos"},
            {"company": "Apple", "name": "Steve Jobs"},
            {"company": "Apple", "name": "Steve Wozniak"},
            {"company": "Arista Networks", "name": "Jayshree Ullal"},
            {"company": "Google", "name": "Larry Page"},
            {"company": "Google", "name": "Rob Pike"},
            {"company": "Google", "name": "Serguey Brin"},
            {"company": "Microsoft", "name": "Bill Gates"},
            {"company": "SpaceX", "name": "Elon Musk"},
            {"company": "Tesla", "name": "Elon Musk"},
            {"company": "Yahoo", "name": "Marissa Mayer"}
        ], {'autoGenerateObjectIDIfNotExist': True}))

        responses.push(self.index.set_settings({
            'attributesForFaceting': ["searchable(company)"]
        }))

        responses.wait()

        # Perform a search query using search with the query `algolia` and no
        # parameter and check that the number of returned hits is equal to 2
        result = self.index.search('algolia')
        self.assertEqual(result['nbHits'], 2)
        self.assertEqual(SearchIndex.get_object_position(
            result, 'nicolas-dessaigne'), 0
        )
        self.assertEqual(SearchIndex.get_object_position(
            result, 'julien-lemoine'), 1
        )

        self.assertEqual(SearchIndex.get_object_position(result, ''), -1)

        # Call find_object with the following parameters and check that
        # no object is found
        with self.assertRaises(ObjectNotFoundException):
            self.index.find_object(lambda _: False)

        # Call find_object with the following parameters and check that
        # the first object is returned with a `position=0` and `page=0`
        found = self.index.find_object(lambda _: True)
        self.assertEqual(found['position'], 0)
        self.assertEqual(found['page'], 0)

        def callback(obj):
            # type: (dict) -> bool
            return obj.get('company') == 'Apple'

        # Call find_object with the following parameters and check that
        # no object is found
        with self.assertRaises(ObjectNotFoundException):
            self.index.find_object(callback, {
                'query': 'algolia'
            })

        # Call find_object with the following parameters and check that
        # no object is found
        with self.assertRaises(ObjectNotFoundException):
            self.index.find_object(callback, {
                'query': '',
                'paginate': False,
                'hitsPerPage': 5
            })

        # Call find_object with the following parameters and check that
        # the first object is returned with a `position=0` and `page=2`
        found = self.index.find_object(callback, {
            'query': '',
            'paginate': True,
            'hitsPerPage': 5
        })

        self.assertEqual(found['position'], 0)
        self.assertEqual(found['page'], 2)

        # Perform a search using search with the query `elon` and the
        # following parameter and check that the queryID field from
        # the response is not empty
        result = self.index.search('elon', {'clickAnalytics': True})
        self.assertIn('queryID', result)

        # Perform a faceted search using search with the query `elon` and the
        # following parameters and check that the number of returned hits is
        # equal to 1
        result = self.index.search('elon', {
            'facets': '*',
            'facetFilters': 'company:tesla'
        })
        self.assertEqual(result['nbHits'], 1)

        # Perform a filtered search using search with the query `elon` and the
        # following parameters and check that the number of returned hits is
        # equal to 2
        result = self.index.search('elon', {
            'facets': '*',
            'filters': '(company:tesla OR company:spacex)'
        })

        self.assertEqual(result['nbHits'], 2)

        result = self.index.search_for_facet_values('company', 'a')[
            'facetHits']

        values = list(
            map(lambda facet: facet['value'], result))

        self.assertIn('Algolia', values)
        self.assertIn('Amazon', values)
        self.assertIn('Apple', values)
        self.assertIn('Arista Networks', values)
Esempio n. 2
0
    def test_search(self):
        responses = MultipleResponse()

        responses.push(
            self.index.save_objects(
                [
                    {
                        "company": "Algolia",
                        "name": "Julien Lemoine",
                        "objectID": "julien-lemoine",
                    },  # noqa: E501
                    {
                        "company": "Algolia",
                        "name": "Nicolas Dessaigne",
                        "objectID": "nicolas-dessaigne",
                    },  # noqa: E501
                    {
                        "company": "Amazon",
                        "name": "Jeff Bezos"
                    },
                    {
                        "company": "Apple",
                        "name": "Steve Jobs"
                    },
                    {
                        "company": "Apple",
                        "name": "Steve Wozniak"
                    },
                    {
                        "company": "Arista Networks",
                        "name": "Jayshree Ullal"
                    },
                    {
                        "company": "Google",
                        "name": "Larry Page"
                    },
                    {
                        "company": "Google",
                        "name": "Rob Pike"
                    },
                    {
                        "company": "Google",
                        "name": "Serguey Brin"
                    },
                    {
                        "company": "Microsoft",
                        "name": "Bill Gates"
                    },
                    {
                        "company": "SpaceX",
                        "name": "Elon Musk"
                    },
                    {
                        "company": "Tesla",
                        "name": "Elon Musk"
                    },
                    {
                        "company": "Yahoo",
                        "name": "Marissa Mayer"
                    },
                ],
                {"autoGenerateObjectIDIfNotExist": True},
            ))

        responses.push(
            self.index.set_settings(
                {"attributesForFaceting": ["searchable(company)"]}))

        responses.wait()

        # Perform a search query using search with the query `algolia` and no
        # parameter and check that the number of returned hits is equal to 2
        result = self.index.search("algolia")
        self.assertEqual(result["nbHits"], 2)
        self.assertEqual(
            SearchIndex.get_object_position(result, "nicolas-dessaigne"), 0)
        self.assertEqual(
            SearchIndex.get_object_position(result, "julien-lemoine"), 1)

        self.assertEqual(SearchIndex.get_object_position(result, ""), -1)

        # Call find_object with the following parameters and check that
        # no object is found
        with self.assertRaises(ObjectNotFoundException):
            self.index.find_object(lambda _: False)

        # Call find_object with the following parameters and check that
        # the first object is returned with a `position=0` and `page=0`
        found = self.index.find_object(lambda _: True)
        self.assertEqual(found["position"], 0)
        self.assertEqual(found["page"], 0)

        def callback(obj):
            # type: (dict) -> bool
            return obj.get("company") == "Apple"

        # Call find_object with the following parameters and check that
        # no object is found
        with self.assertRaises(ObjectNotFoundException):
            self.index.find_object(callback, {"query": "algolia"})

        # Call find_object with the following parameters and check that
        # no object is found
        with self.assertRaises(ObjectNotFoundException):
            self.index.find_object(callback, {
                "query": "",
                "paginate": False,
                "hitsPerPage": 5
            })

        # Call find_object with the following parameters and check that
        # the first object is returned with a `position=0` and `page=2`
        found = self.index.find_object(callback, {
            "query": "",
            "paginate": True,
            "hitsPerPage": 5
        })

        self.assertEqual(found["position"], 0)
        self.assertEqual(found["page"], 2)

        # Perform a search using search with the query `elon` and the
        # following parameter and check that the queryID field from
        # the response is not empty
        result = self.index.search("elon", {"clickAnalytics": True})
        self.assertIn("queryID", result)

        # Perform a faceted search using search with the query `elon` and the
        # following parameters and check that the number of returned hits is
        # equal to 1
        result = self.index.search("elon", {
            "facets": "*",
            "facetFilters": "company:tesla"
        })
        self.assertEqual(result["nbHits"], 1)

        # Perform a filtered search using search with the query `elon` and the
        # following parameters and check that the number of returned hits is
        # equal to 2
        result = self.index.search(
            "elon", {
                "facets": "*",
                "filters": "(company:tesla OR company:spacex)"
            })

        self.assertEqual(result["nbHits"], 2)

        result = self.index.search_for_facet_values("company",
                                                    "a")["facetHits"]

        values = list(map(lambda facet: facet["value"], result))

        self.assertIn("Algolia", values)
        self.assertIn("Amazon", values)
        self.assertIn("Apple", values)
        self.assertIn("Arista Networks", values)