django-haystack 对 多对多字段( ManyToManyField )进行索引

django-haystack 对 多对多字段( ManyToManyField )进行索引,第1张

django-haystack 对 多对多字段( ManyToManyField )进行索引

源自: Django Haystack and Taggit - Stack Overflow

我的错误栈如下:

values.append(current_object())
TypeError: __call__() missing 1 required keyword-only argument: 'manager'
Traceback (most recent call last):
  File "D:Program FilesJetBrainsPyCharmpluginspythonhelperspycharmdjango_manage.py", line 52, in 
    run_command()
  File "D:Program FilesJetBrainsPyCharmpluginspythonhelperspycharmdjango_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "C:ProgramsMiniconda3envsdjg3211envlibrunpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:ProgramsMiniconda3envsdjg3211envlibrunpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:ProgramsMiniconda3envsdjg3211envlibrunpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:WebProjects/aisitevmanage.py", line 22, in 
    main()
  File "D:WebProjects/aisitevmanage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packagesdjangocoremanagement__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packagesdjangocoremanagement__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packagesdjangocoremanagementbase.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packagesdjangocoremanagementbase.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackmanagementcommandsrebuild_index.py", line 65, in handle
    call_command("update_index", **update_options)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packagesdjangocoremanagement__init__.py", line 181, in call_command
    return command.execute(*args, **defaults)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packagesdjangocoremanagementbase.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackmanagementcommandsupdate_index.py", line 297, in handle
    self.update_backend(label, using)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackmanagementcommandsupdate_index.py", line 342, in update_backend
    max_pk = do_update(
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackmanagementcommandsupdate_index.py", line 119, in do_update
    backend.update(index, current_qs, commit=commit)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackbackendselasticsearch_backend.py", line 215, in update
    prepped_data = self._prepare_object(index, obj)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackbackendselasticsearch_backend.py", line 196, in _prepare_object
    return index.full_prepare(obj)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackindexes.py", line 235, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackindexes.py", line 226, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackfields.py", line 236, in prepare
    return self.convert(super().prepare(obj))
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackfields.py", line 105, in prepare
    values = self.resolve_attributes_lookup(current_objects, attrs)
  File "C:ProgramsMiniconda3envsdjg3211envlibsite-packageshaystackfields.py", line 157, in resolve_attributes_lookup
    values.append(current_object())
TypeError: __call__() missing 1 required keyword-only argument: 'manager'

Is there anybody using Django taggit with haystack? How can we make tags field indexable by haystack?

I have tried:

class EventIndex(indexes.SearchIndex, indexes.Indexable):
        text = indexes.CharField( model_attr='descr_en', document=True, use_template=True)
        text_tr = indexes.CharField(model_attr='descr_tr')
        tags = indexes.MultiValueField()

        def prepare_text(self, obj):
            return '%s %s' % (obj.title_en, obj.descr_en)

        def prepare_text_tr(self, obj):
            return '%s %s' % (obj.title_tr, obj.descr_tr)

        def prepare_tags(self, obj):
            return [tag.name for tag in obj.tags.all()]

        def get_model(self):
            return Event

And i am using a custom searchqueryset for multilingual search :

class MlSearchQuerySet(SearchQuerySet):
    def filter(self, **kwargs):
        """Narrows the search based on certain attributes and the default operator."""
        if 'content' in kwargs:
            kwd = kwargs.pop('content')
            currentLngCode = str(get_language())
            lngCode = settings.LANGUAGE_CODE
            if currentLngCode == lngCode: 
                kwdkey = "text" 
                kwargs[kwdkey] = kwd
            else:
                kwdkey = "text_%s" % currentLngCode
                kwargs[kwdkey] = kwd


        if getattr(settings, 'HAYSTACK_DEFAULT_OPERATOR', DEFAULT_OPERATOR) == 'OR':
           return self.filter_or(**kwargs)
        else:
            return self.filter_and(**kwargs)

djangodjango-haystack

Share

Follow

edited May 17 '13 at 17:19

asked May 17 '13 at 16:19

ratata

1,1011313 silver badges3737 bronze badges

Add a comment

1 Answer

ActiveOldestScore

8

To get the tags into the search index we added them to our content template file eg

{{ object.title }}
{{ object.body }}
{% for tag in object.tags.all %} {{ tag.name }} {% endfor %}
{{ object.user.get_full_name }}

We also include it as a MultiValueField

tags = indexes.MultiValueField()

def prepare_tags(self, obj):
    return [tag.name for tag in obj.tags.all()]

Haven't had success trying to make boost work in either case, but the search definitely indexes them correctly.

Share

Follow

answered May 17 '13 at 17:03

Rafe Hatfield

59511 gold badge55 silver badges1010 bronze badges

  • I have updated my question in order to represent my real code structure better. Because i am not getting any tags related search results with the ways you suggest... 

    – ratata

     May 17 '13 at 17:23
  • can you include your text template? if your tags are in there they should get indexed, they will be treated the same as any other content in your template. 

    – Rafe Hatfield

     May 17 '13 at 17:28
  • in 'event_text.txt' i have : {{ object.title }} {{ object.descr }} {% for tag in object.tags.all %} {{ tag.name }} {% endfor %} 

    – ratata

     May 17 '13 at 17:30
  • hmm seems ok to me - sorry to ask what may be a silly question, but just to make sure; you are reindexing after each change, correct? (ie run "python manage.py rebuild_index") 

    – Rafe Hatfield

     May 17 '13 at 17:38
  • 1

    ah you probably also need to add tags to your prepare_text method - not confident on that but seems like its missing (still new to haystack, you're past my basic knowledge now) 

    – Rafe Hatfield

     May 17 '13 at 17:52 

    Show 1 more comment

    欢迎分享,转载请注明来源:内存溢出

    原文地址: https://outofmemory.cn/zaji/5721208.html

  • (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2022-12-18
    下一篇 2022-12-18

    发表评论

    登录后才能评论

    评论列表(0条)

    保存