etianen / django-watson

Full-text multi-table search application for Django. Easy to install and use, with good performance.
BSD 3-Clause "New" or "Revised" License
1.2k stars 130 forks source link

Watson ignore 'fields' when model is registering #170

Closed ghost closed 6 years ago

ghost commented 8 years ago

So, watson in my module working good. But when I add a new model to register, watson somehow ignore all arguments that I try to pass. My new model doesn't have any Text- or Charfield, only DateTime, Small Int and some Foreign Key. Well, I am expect from watson to not index this model at all, still, watson index all Foreign Key fields and DateTime fields too. It is little bit awkward, because other models (even with ManyToMany fields) indexing very well. I also tried to use watson.filter(...) with excluded models that connect via foreign key and i do not want to indexing them, but it really doesn't help me at all. Sorry if I do something wrong.

Django Version: 1.9.7 Watson Version: 1.2.2 Database: SQLite3

etianen commented 8 years ago

What code are you using to register your model?

If you're passing an empty tuple, then I believe it actually registers all fields.

Why do you want to register a model with django-watson, but not register any fields?!

On Wed, 6 Jul 2016 at 04:17 barsoo notifications@github.com wrote:

So, watson in my module working good. But when I add a new model to register, watson somehow ignore all arguments that I try to pass. My new model doesn't have any Text- or Charfield, only DateTime, Small Int and some Foreign Key. Well, I am expect from watson to not index this model at all, still, watson index all Foreign Key fields and DateTime fields too. It is little bit awkward, because other models (even with ManyToMany fields) indexing very well. I also tried to use watson.filter(...) with excluded models that connect via foreign key and i do not want to indexing them, but it really doesn't help me at all. Sorry if I do something wrong.

Django Version: 1.9.7 Watson Version: 1.2.2 Database: SQLite3

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/etianen/django-watson/issues/170, or mute the thread https://github.com/notifications/unsubscribe/AAJFCHpsuYEG2pGMMd0Bx0ity47Org7eks5qSx4zgaJpZM4JFuEg .

ghost commented 8 years ago

Here it is, apps.py:

from django.apps import AppConfig
from watson import search as watson

class DirectoryConfig(AppConfig):
    name = 'api'

    def ready(self):
        event = self.get_model('Event')
        watson.register(event, fields=('patient',))

init.py

default_app_config = 'api.apps.DirectoryConfig'

where patient is ForeignKey field. I even tried to pass exclude with all other model's fields and rebuild watson, but it index all stuff again

etianen commented 8 years ago

Hmm, so, the unicode/str method of your model will also be indexed, in addition to any fields that you specify.

That might be causing the effect you're describing.

If you want to avoid indexing the unicode/str method, you need to create a custom SearchAdapter, and override the get_title() method to return "".

https://github.com/etianen/django-watson/wiki/registering-models#tweaking-search-weighting

ghost commented 8 years ago

Oh. It actualy was in the wiki. But I did not see it so many times. Anyway, custom SearchAdapter and overriding get_title() solve my problem. Now it is searching as I expect it to search. Many thanks.

etianen commented 8 years ago

No worries! The behaviour of indexing the str/unicode method is definitely a bit odd and unexpected when explicitly specifying fields to watson.register().

At some point I'll attempt to improve the docs and API, but I don't have time right now. :)

On Thu, 7 Jul 2016 at 10:02 barsoo notifications@github.com wrote:

Oh. It actualy was in the wiki. But I did not see it so many times. Anyway, custom SearchAdapter and overriding get_title() solve my problem. Now it is searching as I expect it to search. Many thanks.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/etianen/django-watson/issues/170#issuecomment-231022819, or mute the thread https://github.com/notifications/unsubscribe/AAJFCM5PR5dcC7DPlP63lyKQm6FFPq3cks5qTMCmgaJpZM4JFuEg .