Searching wagtail images

442 views Asked by At

I'm implementing a image rich webapp with wagtail and elasticsearch. To search for images I want to use elasticsearch and the built-in capabilities of wagtail to index and search for images.

The images in wagtail are tagged and I want to use those tags to find images. The indexing of images already worked out of the box and now I'm strugling with searching for them. The challenge is that I want to search for text in tags. Wagtail provides a search() function on the object manager which can be used for searching:

images = Image.objects.search("sometag")

To restrict the search on a specific field, it is possible to do the following:

images = Image.objects.search("sometag", fields=['title'])

Now to restrict on the tag which is a defined as a related field for search:

search_fields = CollectionMember.search_fields + [
    index.SearchField('title', partial_match=True, boost=10),
    index.AutocompleteField('title'),
    index.FilterField('title'),
    index.RelatedFields('tags', [
        index.SearchField('name', partial_match=True, boost=10),
        index.AutocompleteField('name'),
    ]),
    index.FilterField('uploaded_by_user'),
]

I'd expect the search() function to work with either the 'tag' or 'tag.name' parameter:

images = Image.objects.search("sometag", fields=['tags.name'])
images = Image.objects.search("sometag", fields=['tags'])

But I only get the following error:

wagtail.search.backends.base.SearchFieldError: Cannot search with field "tags.name". Please add index.SearchField('tags.name') to Image.search_fields.

or

wagtail.search.backends.base.SearchFieldError: Cannot search with field "tags". Please add index.SearchField('tags') to Image.search_fields.

How can I use the fields=... functionality with a RelatedFields definition in Images?

0

There are 0 answers