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 Files\JetBrains\PyCharm\plugins\python\helpers\pycharm\django_manage.py", line 52, in <module>
    run_command()
  File "D:\Program Files\JetBrains\PyCharm\plugins\python\helpers\pycharm\django_manage.py", line 46, in run_command
    run_module(manage_file, None, '__main__', True)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\WebProjects/aisitev\manage.py", line 22, in <module>
    main()
  File "D:\WebProjects/aisitev\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\management\commands\rebuild_index.py", line 65, in handle
    call_command("update_index", **update_options)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\django\core\management\__init__.py", line 181, in call_command
    return command.execute(*args, **defaults)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\management\commands\update_index.py", line 297, in handle
    self.update_backend(label, using)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\management\commands\update_index.py", line 342, in update_backend
    max_pk = do_update(
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\management\commands\update_index.py", line 119, in do_update
    backend.update(index, current_qs, commit=commit)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\backends\elasticsearch_backend.py", line 215, in update
    prepped_data = self._prepare_object(index, obj)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\backends\elasticsearch_backend.py", line 196, in _prepare_object
    return index.full_prepare(obj)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\indexes.py", line 235, in full_prepare
    self.prepared_data = self.prepare(obj)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\indexes.py", line 226, in prepare
    self.prepared_data[field.index_fieldname] = field.prepare(obj)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\fields.py", line 236, in prepare
    return self.convert(super().prepare(obj))
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\fields.py", line 105, in prepare
    values = self.resolve_attributes_lookup(current_objects, attrs)
  File "C:\Programs\Miniconda3\envs\djg3211env\lib\site-packages\haystack\fields.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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值