Make taggit tags searchable

773 views
Skip to first unread message

Ronnie Stevens

unread,
Sep 20, 2016, 1:53:16 PM9/20/16
to Wagtail support
In a project taggit has been enabled on some contenttypes. The tags appear as content. But I want the page to be found if the tag is inserted in the searchbox. I cannot find any documentation how to index the taggit tags.

Matthew Westcott

unread,
Sep 20, 2016, 3:52:56 PM9/20/16
to wag...@googlegroups.com
Hi Ronnie,

Adding the following entry to your model's search_fields should do it:

index.RelatedFields('tags', [
index.SearchField('name', partial_match=True, boost=10),
]),

(Note that you'll probably need to be running the Elasticsearch search backend for this to work - see http://docs.wagtail.io/en/v1.6.2/topics/search/backends.html)

Cheers,
- Matt

Ronnie Stevens

unread,
Sep 21, 2016, 4:25:21 AM9/21/16
to Wagtail support
Hi Matthew, this doesn't seem to work.. No error either. Bit of a django-newbie, learning every day. 
Any suggestions?

Matthew Westcott

unread,
Sep 21, 2016, 6:11:46 AM9/21/16
to wag...@googlegroups.com
Sorry, should have mentioned - you'll need to run
./manage.py update_index
after adding that line.

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/9bbd8c48-de7d-4a3a-a343-91374c5bbecb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Ronnie Stevens

unread,
Sep 21, 2016, 6:43:20 AM9/21/16
to Wagtail support
I did that alright, and I am using ElasticSearch. I see the Model is being re-indexed. 
Perhaps a naming-convention I am not seeing?

This is the relevant code:

from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase


class CasePageTag(TaggedItemBase):
    content_object = ParentalKey('case', related_name='tagged_items')

class Case(Page):

    tags = ClusterTaggableManager(through=CasePageTag, blank=True)
    
    subtitle = models.CharField(max_length=260, help_text="homepage title")
    description = RichTextField(blank=True)

    search_fields = Page.search_fields + [
            index.SearchField('subtitle'),
            index.SearchField('description'),
            index.RelatedFields('tags', [
                index.SearchField('name', partial_match=True, boost=10),
            ]),
        ]


Martin Sandström

unread,
Oct 24, 2016, 5:15:24 AM10/24/16
to Wagtail support
Hi! 

I'm bringing this thread back to life again, I'm experiencing the same thing with elasticsearch2, wagtail 1.7 and taggit with a through model. 

My models:

class ArticleTag(TaggedItemBase):
    content_object
= ParentalKey('article.Article',
                                 related_name
='tagged_items')


class Article(Page):
   
...
    tags
= ClusterTaggableManager(through=ArticleTag, blank=True)


I tried a few different index approaches, some raised issues on indexing, while some created the key in elastic but with null as a value.

1.
index.RelatedFields('tagged_items__tag', [
    index.SearchField('name'),
])

>>>> raise FieldDoesNotExist('%s has no field named %r' % (self.object_name, field_name))
django.core.exceptions.FieldDoesNotExist: Article has no field named u'tagged_items__tag'


2.
index.RelatedFields('tagged_items', [
    index.SearchField('tag__name'),
])

>>>> AttributeError: 'NoneType' object has no attribute '_meta'

3.
index.RelatedFields('tagged_items', [
    index.RelatedFields('tag', [
        index.SearchField('name'),
        index.SearchField('slug'),
    ])
])

>>>> No error, but elastic indexes null
>>>> "article_article__tagged_items": null,

4.
index.SearchField('tagged_items__tag__name'),

>>>> AttributeError: 'NoneType' object has no attribute '_meta'

5. 

With the method def "get_tag_name" on the through model.

index.RelatedFields('tagged_items', [
    index.SearchField('get_tag_name')
])

>>>> "article_article__tagged_items": null,

6.
index.RelatedFields('tags', [
    index.SearchField('name'),
    index.SearchField('slug'),
])

>>>> AttributeError: 'NoneType' object has no attribute '_meta'

My question is that has this type of indexing ever worked in the first place, and if so, what am I doing wrong?

Thanks!
/ Martin

Miguel Vieira

unread,
Oct 27, 2016, 11:06:58 AM10/27/16
to Wagtail support
Hi,

This works for me to search within tags:

search_fields = Page.search_fields + [

    index
.RelatedFields('tags', [
                        index
.SearchField('name'),

                       
]),
]

It didn't work for me initially because I was using the incorrect version of elasticsearch-py. Could that be the same problem you are having?

Regards,
Miguel

Martin Sandström

unread,
Oct 31, 2016, 3:33:58 AM10/31/16
to Wagtail support
Hi Miguel!

Thanks for your reply. I updated elasticsearch to 2.4.0 and that solved my issue and tags got indexed:

"article_article__tags": [
{
   
"name": "tag1",
   
"slug": "tag1"
}
,
{
   
"name": "tag2",
   
"slug": "tag2"
}
]

/ M

Ajith P Mohan

unread,
May 6, 2018, 9:32:29 AM5/6/18
to Wagtail support
Hi Matthew,

I am wagtail newbie. I have also facing the same issue. I am using wagtail default  search as search back-end. Is it necessary to use Elasticsearch as search back-end. Any thoughts ?

Matthew Westcott

unread,
May 8, 2018, 5:19:13 AM5/8/18
to wag...@googlegroups.com
Hi Ajith,
As I mentioned, it probably won't work on the default database search backend - that one only has limited functionality. By all means give it a try, though...

Since my original message, there's now another alternative which may well be more convenient if you're using PostgreSQL - you can use the PostgreSQL search backend: http://docs.wagtail.io/en/v2.0.1/reference/contrib/postgres_search.html

Cheers,
- Matt
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at https://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/33260310-578b-4848-8fa5-bf68cf195358%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages