def test_scores(self):
        """Test the max/min properties."""
        sq = SearchQuery()
        self.assertEqual(sq.max_score, 0)
        self.assertEqual(sq.min_score, 0)

        sq.hits = [{"score": 1}, {"score": 2}]
        self.assertEqual(sq.max_score, 2)
        self.assertEqual(sq.min_score, 1)
Exemplo n.º 2
0
def elasticsearch(request, query):
    search = Search(using=get_client(), index='gam')
    state = SearchQuery.execute(search)
    for obj in Imagen.objects.from_search_query(sq):
        print(obj.search_score, obj.search_rank)
        context  = {'state':state}
    return render(request, 'all_documents_page.html', context)
    def test_paging(self):
        """Test the paging properties."""
        sq = SearchQuery()
        self.assertEqual(sq.page_slice, None)

        # no hits, so should all be 0
        sq.query = {"from": 0, "size": 25}
        self.assertEqual(sq.page_slice, (0, 25))
        self.assertEqual(sq.page_from, 0)
        self.assertEqual(sq.page_to, 0)
        self.assertEqual(sq.page_size, 0)

        # three hits
        sq.hits = [1, 2, 3]  # random list of size = 3
        sq.query = {"from": 0, "size": 25}
        self.assertEqual(sq.page_from, 1)
        self.assertEqual(sq.page_to, 3)
        self.assertEqual(sq.page_size, 3)
Exemplo n.º 4
0
    def test_execute(self, mock_save, mock_execute, mock_now):
        """Test the execute class method."""
        search = Search()
        sq = SearchQuery.execute(search)
        self.assertEqual(sq.user, None)
        self.assertEqual(sq.search_terms, "")
        self.assertEqual(sq.index, "_all")
        self.assertEqual(sq.query, search.to_dict())
        self.assertEqual(sq.hits, [])
        self.assertEqual(sq.total_hits, mock_execute.return_value.hits.total)
        self.assertEqual(sq.reference, "")
        self.assertTrue(sq.duration > 0)
        self.assertEqual(sq.executed_at, mock_now.return_value)
        self.assertEqual(sq.response, mock_execute.return_value)
        mock_save.assert_called_once_with()

        # try without saving
        mock_save.reset_mock()
        sq = SearchQuery.execute(search, save=False)
        mock_save.assert_not_called()
Exemplo n.º 5
0
 def test_save(self):
     """Try saving unserializable JSON."""
     today = datetime.date.today()
     sq = SearchQuery(
         user=None,
         index="foo",
         query={"today": today},
         hits={"hits": decimal.Decimal("1.0")},
         total_hits=100,
         reference="bar",
         executed_at=tz_now(),
         duration=0,
     )
     sq.save()
     sq.refresh_from_db()
     # invalid JSON values will have been converted
     self.assertEqual(sq.search_terms, "")
     self.assertEqual(sq.query["today"], today.isoformat())
     self.assertEqual(sq.hits["hits"], "1.0")
     self.assertEqual(sq.query_type, SearchQuery.QUERY_TYPE_SEARCH)
 def test_object_ids(self):
     """Test the object_ids property."""
     obj = SearchQuery(hits=SearchQueryTests.hits)
     self.assertEqual(set(obj.object_ids), set([1, 2, 3]))
 def test__extract_set(self):
     """Test the _extract_set method."""
     obj = SearchQuery(hits=SearchQueryTests.hits)
     self.assertEqual(set(obj._extract_set("id")), set([1, 2, 3]))
Exemplo n.º 8
0
 def test_doc_types(self):
     """Test the doc_types property."""
     obj = SearchQuery(hits=SearchQueryTests.hits)
     self.assertEqual(set(obj.doc_types), set(["foo", "bar"]))