研究了coreseek下的sphinx 配置及api调用收获颇多_第1页
免费预览已结束,剩余1页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、研究了coreseek下的sphinx 配置及api调用,收获颇多。前言: 之前向来用法lucene,有无数优点及缺点,最大的缺点就是要维护一个索引的成本很高,需要牵扯到无数方面,其中也包含业务方面;优点呢,不用多说了,速度快,支持查询的模式多,各种条件下的查询都能实现,所以想找一个越发符合现有应用情况的搜寻引擎,故想到了coreseek=(sphinx 中文分词 框架) sphinx 最大的益处是业务层面不需要你去关怀索引的建立、更新等,后台定时去维护主索引和增量索引即可。同时有无数人性化的功能,比如防止建立索引时全表读取造成数据库挂掉,而举行的分批读取。增量索引方面可以读取一段时光内更新的

2、数据,对于一个网站网站来说,更新的量占全部的数据的量比较小。增量索引适合短时光内更新,主索引适合长时光内更新。读取索引的时候同时读取这2个索引。 sphinxex引擎,这个个人觉得只适合终端查看些数据,做调试相关的,线上用法还是得用他的api,支持n种语言了,今日测试的是java接口,感觉还不错,不过也找到一些小缺点,少float类型的filter啊(用float_range也还可以), 少int类型的属性,不明了sing类型的为啥没写出来。 和lucene比较除了上述的外,好的功能有:支持一对多的搜寻,sql_attr_mult 这个很有用,比如 我要找到公交车站叫a的旁边的房子,一个房子对

3、应着好几个公交车站a,b,c,d等等的,很有用,杠杠的!呵呵。竟然还有地理坐标的查询功能,正巧符合我们的业务,查询 房子周围100米以内的东西之类。 正文: 以下是实际用法的配置参数及功能,有写的不对的还请指正。 1、官方和网上现有的增量索引方式都仅仅为自增的主键增强了多少,而并不包括之前的数据更新,按照这个情况,可以用法mysql里面的一个特别字段的功能:创建一个字段: last_upd_ timestamp not null default current_timestamp on update current_timestamp, 即只要该行有数据变更,这个字段的 即变成最新的时光,同时

4、增强记录表,记录主索引索引的最后时光。 create table sph_counter ( counter_ int(11) not null, max_doc_id int(11) not null, primary key (counter_id) ) engine=innodb default charset=utf8 collate=utf8_bin 然后呢,就是在sphinx配置文件里写上相关的sql了。 source tasks type = mysql sql_host = 192.168.0.21 sql_user = root sql_pass = cjkjb110 sql

5、_db = design sql_port = 3306 sql_query_pre = set names utf8 sql_query_pre = set session query_cache_type=off sql_query_pre = replace into sph_counter select 1, unix_timestamp(max(last_updat_time) from tasks sql_query = select id,task_no as taskno,title,title as title1,reward,us,room,hall,wei,area,to

6、tal_budget as totalbudget,district,city,provie,unix_timestamp(created) as created_time,decoration_style_id,unix_timestamp(last_updated_time) as last_updated_time from tasks where last_updated_time = (select from_unixtime(max_doc_id) from sph_counter where counter_id=1) sql_attr_float = reward sql_at

7、tr_uint = status sql_attr_uint = room sql_attr_bigint = hall sql_attr_bigint = wei sql_attr_float = area sql_attr_float = totalbudget sql_attr_uint = district sql_attr_uint = city sql_attr_uint = province sql_attr_uint = created_time sql_attr_uint = last_updated_time sql_attr_bigint =taskno sql_attr

8、_string =title sql_attr_multi =uint decoration_style_id from field; sql_query_info_pre = set names utf8 行查询时,设置正确的字符集 sql_query_info = select * from tasks where id=$id 指令行查询时,从数据库读取原始数据信息 source tasks_delta : tasks sql_query_pre =set names utf8 sql_query = select id,task_no as taskno,title,title as

9、title1,reward,status,room,hall,wei,area,total_budget as totalbudget,district,city,province,unix_timestamp(created) as created_time,decoration_style_id,unix_timestamp(last_updated_time) as last_updated_time from tasks where last_updated_time (select from_unixtime(max_doc_id) from sph_counter where co

10、unter_id=1) 上面定义了主索引:tasks,增量索引tasks_delta 主索引里sql_query_pre 里插入了最新的last_update_time字段,执行主索引的时候该语句就能执行了,执行增量索引的时候推断的条件即是大于这个主索引的最后更新时光,这样的话就能猎取到更新和添加的最新数据了。有一个缺点就是不能找到记录删除数据,我们的解决办法是用法规律删除即增强一个is_delete字段来推断是否是删除。 2、搜寻1对多的状况,打个比喻,数据库中有n个文章的数据,每个文章对应有n个标签,若是要求搜寻标签得到m个文章的时候,一般的查询就有困难了,之前的办法也就是把多个标签放在同

11、一个字段中,用逗号隔开之类,用法like查询等,或者用法联合查询,没有太好的解决计划,sphinx里的sql_attr_multi字段就是为了解决这个问题而建。 上述表中的 sql_attr_multi =uint decoration_style_id from field; 该行就用法了这个办法,详细怎样操作呢? decoration_style_id 字段是tasks表中的其中一个varchar列,里面记录了例如12,45,2,89,323之类的数据,其他什么都不需要配置挺直用法这一行代码,就能给你自动按uint类型分成多行了,搜寻的时候挺直_filter(decoration_styl

12、e_id,89,fae) 就能搜寻出带有89这个的记录。功能十分有用,在无数方面都可以用的上,除了上面用的from field 还可以用法from query,from range_query等,这样就可以联合其他的表举行索引查询了。 3、精心的人可能发觉我写的sql 中间加了无数别号,好多重复的字段,例如 title,title as title1 ,这样的写法是迎合sphinx的功能特点的,下面可以看到sql_attr_string =title 这句是定义一个属性 attr,title1字段为field,attr 是在搜寻结果中可以显示的,但是不能作为分词举行搜寻的,field 是可以作

13、为分词举行搜寻查询的,两者不能同时起作用,所以复制了一列,作不同的办法用,这是我的方法,不知道有没有更好的方法,有的话棘手告知我下感谢。 4、说下api,用法的是java ,常用的几个语言办法名和参数都类 ,贴上几个主要的地方: sphinxclient cl =null; searchrelt t sr = null; sphinxresult res=null; int pagesize=6; int pageind=1; cl = new sphinxclient(); cl.setserver ( mons.constants.pubconstant.sphinxserver, 931

14、2 ); cl.setconnecttimeout(2000); cl.setmatchmode(sphinxclient.sph_match_all); if(chk(info.getarea() int a=integer.parseint(info.getarea(); float area_min=0.00f; float area_max=100000.00f; switch(a) case 1: area_min=0.00f; area_max=40.00f; break; case 2: area_min=41.00f; area_max=60.00f; break; case

15、3: area_min=61.00f; area_max=90.00f; break; case 4: area_min=91.00f; area_max=120.00f; break; case 5: area_min=121.00f; area_max=150.00f; break; case 6: area_min=151.00f; area_max=200.00f; break; default: break; cl.setfilterfloatrange( area , area_min, area_max, false); if(chk(info.getpagesize() pag

16、esize=integer.parseint(info.getpagesize(); if(chk(info.getpageindex() pageindex=integer.parseint(info.getpageindex(); cl.setlimits ( (pageindex-1)*pagesize, pagesize ); cl.setsortmode(sphinxclient.sph_sort_attr_desc, last_updated_time /根据字段排序 string q= if(info.getkeywords()!=null) q=info.getkeywords(); string index= * if(chk(info.gettypename() if(info.gettypename().equals(searchconstant.task) index= tasks,tasks_delta else if(info.gettypename().equals(searchconstant.case) index= cases,cases_delta res = cl.query(q,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论