# SearchQuery and duplicate in queryset

**URL:** <https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152>\
**Category:** Using the ORM\
**Created:** [April 13, 2022, 4:02pm UTC](https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152 "2022-04-13T16:02:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kimiyori](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kimiyori/32/7742_2.png) [@Kimiyori](https://forum.djangoproject.com/u/Kimiyori)\
**Post date:** [April 13, 2022, 4:02pm UTC](https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152/1 "2022-04-13T16:02:19Z")

</div>

Last time I [asked](https://forum.djangoproject.com/t/how-to-filter-with-list-of-values/13105) how to filter without getting duplicates and I was advised to use distinct() and it helped, but now I wanted to add sorting by relevance using SearchRank and after that duplicates appeared in the queryset again, and distinct( ) dosent help.  
my models:

```auto
class Book(models.Model):
    ...
    genres = models.ManyToManyField(
        Genre, related_name='book', blank=True)
    ...
class Genre(models.Model):
    name = models.CharField(max_length=100, null=True, blank=True)

```

my query string

```auto
?genre=Supernatural&genre=Romance

```

and view

```auto
class BookList(ListView):
    template_name='book/list.html'
    context_object_name = 'list'
    def get_queryset(self):
            book=Book
            genres = self.request.GET.getlist('genre')
            search_vector = SearchVector('genres__name')
            if genres:
                search_query = SearchQuery(genres.pop())
                for genre in genres:
                    search_query |= SearchQuery(genre)  
                book=book.objects.all().values('id','title__original_name','image').\
                                   annotate(rank=SearchRank(search_vector,search_query)).\
                                   filter(genres __name__ in=genres).order_by('rank').distinct()
            else:
                book=book.objects.all().values('id','title__original_name','image')
            return book

```

As i understand, i get duplicate for each match in SeqrchQuery and if my book, for example, match 4 times with SeqrchQuery, then i get 4 instance of it in my queryset.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 13, 2022, 4:56pm UTC](https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152/2 "2022-04-13T16:56:26Z")

</div>

The [distinct clause](https://docs.djangoproject.com/en/4.0/ref/models/querysets/#distinct) has some caveats in it about how it compares rows. They advise that it may be necessary to specify on which fields you want to ensure the result set being distinct. You may need to do something like `.distinct('rank', 'id')` to get the results you want.

You may also need to turn your query into a subquery with the distinct and allow the outer query to perform the sort by rank.

---

<div class="post-metadata">

**Author:** ![plopidou](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/plopidou/32/5148_2.png) [@plopidou](https://forum.djangoproject.com/u/plopidou)\
**Post date:** [April 13, 2022, 5:12pm UTC](https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152/3 "2022-04-13T17:12:37Z")

</div>

as far as im aware though, `DISTINCT ON` still does not work on MySQL. :’[

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [April 13, 2022, 5:23pm UTC](https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152/4 "2022-04-13T17:23:16Z")

</div>

I also believe that to be true. (Which is why I mentioned the subquery as an alternative to the distinct.)

---

<div class="post-metadata">

**Author:** ![Kimiyori](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kimiyori/32/7742_2.png) [@Kimiyori](https://forum.djangoproject.com/u/Kimiyori)\
**Post date:** [April 13, 2022, 9:04pm UTC](https://forum.djangoproject.com/t/searchquery-and-duplicate-in-queryset/13152/5 "2022-04-13T21:04:59Z")

</div>

Well, i kinda solved my problem with this code

```auto
book=book.objects.all().values('id','title__original_name','image').filter(genres__name__in=genres).annotate(relevance=Count('id')).order_by('-relevance','title__original_name') 

```
