django-haystack icon indicating copy to clipboard operation
django-haystack copied to clipboard

Forcing indexing to use different connection than Default - mutiple languages

Open archdata opened this issue 3 years ago • 0 comments

Hi,

I am having trouble understanding how to route a search index to the correct connection.

I have two defined


HAYSTACK_CONNECTIONS = {
    "default": {
        "ENGINE": "haystack.backends.solr_backend.SolrEngine",
        "URL": "http://10.4.30.1:8985/solr/collection_en",
        "BATCH_SIZE": 1000,
    },
    "french": {
        "ENGINE": "haystack.backends.solr_backend.SolrEngine",
        "URL": "http://10.4.30.1:8985/solr/collection_fr",
        "BATCH_SIZE": 1000,
    },
}

I have two models - exactly the same, but one holds the English content, the other French. I have also created two search indexes, again the same, but pulls from different models.

# English
class CollectionItemIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(
        document=True, use_template=True, template_name="search/text.txt"
    )

    catalogue_number = indexes.CharField(model_attr="catalogue_number")
  
    ...<snip>Removed lots of fields<snip>

    media = indexes.MultiValueField()

    def prepare_media(self, obj):
        return [media_itm.image_name for media_itm in obj.web_media.all()]

    def no_query_found(self):
        return self.searchqueryset.all()

    def get_model(self):
        return CollectionItem

    def index_queryset(self, using=None):
        return self.get_model().objects.all()
# French
class CollectionItemIndexFrench(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(
        document=True, use_template=True, template_name="search/text.txt"
    )

    catalogue_number = indexes.CharField(model_attr="catalogue_number")
  
     ...<snip>Removed lots of fields<snip>

    media = indexes.MultiValueField()

    def prepare_media(self, obj):
        return [media_itm.image_name for media_itm in obj.web_media_fr.all()]

    def no_query_found(self):
        return self.searchqueryset.all()

    def get_model(self):
        return CollectionItemFrench

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

Problem: The French content is not being sent to the French Connection, instead the English is going to both.

How to I tell the CollectionItemIndexFrench to index the French content in the French Connection? The documentation isn't clear how this is possible. I see that routers may be the solution here, but I don't understand from the documentation how this may work.

Thanks for any help.

archdata avatar Apr 13 '22 20:04 archdata