Full text search FTS 全文检索模块移植到Odoo 8.0绿色版经验及后续需求
-
有感于新odoo的论坛,文档管理,邮件等含大量文本内容的搜索标准功能中只能用SQL中的Like加通配符% ,也就是说只能模糊查询特定字符串,而不能真正像搜索引擎那样用时搜索多个关键字。幸好7.0版本中有个全文检索的模块。下载下来经过一番折腾,终于在绿色版的8.0上装上了,也基本能用了,现分享一些心得,也提出一些改善需求
要改2个文件,3 个地方
1 fts_base下的_openerp_.ini
里的update_xml 改为data, 变这样 'data': ["fts_proxy.xml", 'wizard/fts_config.xml'],
2.fts_base.py
2.1 因为pool里没有DB属性了,第40行 去掉.DB, 变成这样 cr = self.pool.cursor()
2.2 因为使用to_tsquery时如果输入多个关键字其中有空格的话,系统会报错,查询字符串不会法, 改为 plainto_tsquery就可以了(fts_proxy里也要改)
除了fts_base,我还安装了fts_document,fts_mail,装上后要重启系统,就会在setting configure 下多一个full text 的配置菜单,点那个对应的生成索引按钮,等一会儿后在对应的ir_attachment,mail_message表里面会增加一个存放索引字串的栏位,一个对应的索引和触发器,就可以去message旁边那个full text search顶层菜单上搜索已启用全文搜索功能的内容了(正如FTS文档所说,可以将实现跨模块统一全文检索,类似ERP内的google)。
昨晚折腾了很久,终于初步将此全文检索功能扩展到论坛模块,也就是直接在论坛模块就可实现全文搜索问题,答案及评论。
主要的思路及做法如下
1.继承fts_base,在forum_post表上增加全文检索栏位
class Fts_Forum(fts_base):
_model = 'forum.post'
_indexed_column = 'content'
_title_column = 'name'
2.继承forum post,扩展search函数,获取搜索文本,参考fts base直接使用全文检索的SQL语句取得ID清单,
特别的地方有两个,
1.是默认情况下因为传到search函数中的filter已经有parent_id = blank的内容(此类记录表示是回复answer),需要去掉这种条件,不然就搜不到答案里的内容,
2.论坛的评论comment并不保存在forum_post表里,而是保存在mail_message里的,所以在search函数中如果要同时搜索评论,还要去mail_message表中全文检索一下,再合并。
代码即文档,见附件
目前的问题
1. 索引不支持中文,在windows 7上还不知道怎么安装zhparser
2. fts base的全文检索出来的清单不支持双击跳转到显示对应的resource,如文档,邮件或论坛
3. 还需要增加对问题标题及邮件主题的全文检索
4. 其实可以考虑做成通用的全文检索,增加ORM定义时一个全文检索栏位属性,最终实现在定义search view时可以使用全文检索栏位,ORM可以自动转换为对应的全文检索SQL语句,用这样的方式使全文检索变成ORM的一部分。
注,本人非专职程序员,大家多提意见,多交流。 -
在 8.0正式版安装 这三个addons 后 后台的 菜单上的搜索没问题,
前台website的 forums 模块上的论坛一搜索出现 500错误
Error
Error message:
'NoneType' object has no attribute '_columns'
Traceback
Traceback (most recent call last):
File "/home/lv/g101/source/addons/website/models/ir_http.py", line 204, in _handle_exception
response = super(ir_http, self)._handle_exception(exception)
File "/home/lv/g101/source/openerp/addons/base/ir/ir_http.py", line 100, in _handle_exception
return request._handle_exception(exception)
File "/home/lv/g101/source/openerp/http.py", line 596, in _handle_exception
return super(HttpRequest, self)._handle_exception(exception)
File "/home/lv/g101/source/openerp/addons/base/ir/ir_http.py", line 126, in _dispatch
result = request.dispatch()
File "/home/lv/g101/source/openerp/http.py", line 614, in dispatch
r = self._call_function(**self.params)
File "/home/lv/g101/source/openerp/http.py", line 283, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/lv/g101/source/openerp/service/model.py", line 113, in wrapper
return f(dbname, *args, **kwargs)
File "/home/lv/g101/source/openerp/http.py", line 280, in checked_call
return self.endpoint(*a, **kw)
File "/home/lv/g101/source/openerp/http.py", line 733, in call
return self.method(*args, **kw)
File "/home/lv/g101/source/openerp/http.py", line 376, in response_wrap
response = f(*args, **kw)
File "/home/lv/g101/source/addons/website_forum/controllers/main.py", line 106, in questions
question_count = Post.search(cr, uid, domain, count=True, context=context)
File "/home/lv/g101/source/openerp/api.py", line 237, in wrapper
return old_api(self, *args, **kwargs)
File "/home/lv/g101/source/openerp/addons/fts_forum/fts_forum.py", line 154, in search
filters = self._get_filter_expression(cr, uid, args, context).to_sql()
File "/home/lv/g101/source/openerp/api.py", line 237, in wrapper
return old_api(self, *args, **kwargs)
File "/home/lv/g101/source/openerp/addons/fts_forum/fts_forum.py", line 118, in _get_filter_expression
args = get_applicable_args(expression.normalize_domain(args), 0)[0]
File "/home/lv/g101/source/openerp/addons/fts_forum/fts_forum.py", line 106, in get_applicable_args
op1 = get_applicable_args(args, index + 1)
File "/home/lv/g101/source/openerp/addons/fts_forum/fts_forum.py", line 106, in get_applicable_args
op1 = get_applicable_args(args, index + 1)
File "/home/lv/g101/source/openerp/addons/fts_forum/fts_forum.py", line 106, in get_applicable_args
op1 = get_applicable_args(args, index + 1)
File "/home/lv/g101/source/openerp/addons/fts_forum/fts_forum.py", line 92, in get_applicable_args
args[index][0] in orm_model._columns or
AttributeError: 'NoneType' object has no attribute '_columns'
想写个产品tfs搜索...
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login