solr第四篇(solr整合数据源)

相关文章:
solr第一篇(solr5.5.4下载linux安装)
solr第二篇(Solr5.5.4集成Tomcat8部署)
solr第三篇(IK分词器安装配置)
solr第四篇(solr整合数据源)
设置完成第四篇必看

#编辑/usr/local/tomcat/solr_home/new_core/conf下的solrconfig.xml
cd /usr/local/tomcat/solr_home/new_core/conf
vim solrconfig.xml
#首先配置jar位置
<lib dir="/usr/local/tomcat/tomcat8/webapps/solr/WEB-INF/lib" regex=".*\.jar" />
#在name="/select"  class="solr.SearchHandler"之上添加如下代码
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
        <lst name="defaults">
            <str name="config">data-config.xml</str>
        </lst>
</requestHandler>

#完成如上操作之后 在和solrconfig.xml的同目录项目添加文件data-config.xml
#在当前目录下创建文件
mkdir data-config.xml
#编辑data-config.xml
vim data-config.xml
#添加如下
<?xml version="1.0" encoding="UTF-8"?>  
<dataConfig>  
	#更改为自己的数据源地址
	<dataSource name="source1" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solrdata" user="root" password="root" batchSize="-1" />  
  	<document>  
	#entity对应表
	<entity name="goods" pk="id"  dataSource="source1"   
		query="select * from  goods"  
		#配置增量更新需要使用
 		deltaImportQuery="select * from goods where id='${dih.delta.id}'"  
		#配置增量更新需要使用
		deltaQuery="select id from goods where updatetime> '${dataimporter.last_index_time}'">  
		#field对应列字段
		<field column="id" name="id"/> 
		<field column="name" name="name"/>  
		<field column="number" name="number"/>  
		<field column="updatetime" name="updatetime"/>  
     	</entity> 
  	</document>  
</dataConfig>
#其中deltaImportQuery,deltaQuery设置的内容是自动更新mysql数据到solr引擎中来所需要的。如果多个表再并列添加一个entity标签

#编辑managed-schema
vim managed-schema
#添加刚才数据源配置里面的field字段
<field name="id" type="int" indexed="true" stored="true" required="true"  multiValued="false"/>
<field name="username" type="string" indexed="true" stored="true" />
#type="text_ik"表示使用自定义分词 可以中文分词
<field name="nickname" type="text_ik" indexed="true" stored="true" />
<field name="password" type="string" indexed="true" stored="true" />
<field name="regTime" type="date" indexed="true" stored="true" />
#我的配置如下图

#配置自动更新数据源
#在solr_home文件夹里面创建conf文件,在conf里面创建dataimport.properties文件
cd /usr/local/tomcat/solr_home
mkdir conf
cd conf
mkdir dataimport.properties
#编辑dataimport.properties 添加如下内容
#内容开始---------------------------
#################################################

#                                               #

#       dataimport scheduler properties         #

#                                               #

#################################################

#  to sync or not to sync
#  1 - active; anything else - inactive
# 这里的配置不用修改
syncEnabled=1

#  which cores to schedule
#  in a multi-core environment you can decide which cores you want syncronized
#  leave empty or comment it out if using single-core deployment

#  修改成你所使用的core,如果自定义了多个core,用逗号隔开
syncCores=new_core
#syncCores=new_core,new_core2,new_core3,....

#  solr server name or IP address
#  [defaults to localhost if empty]

# 这个一般都是localhost不会变
server=localhost

#  solr server port

#  [defaults to 80 if empty]

#  安装solr的tomcat端口,如果你使用的是默认的端口,就不用改了,否则改成自己的端口就好了
port=8080


#  application name/context

#  [defaults to current ServletContextListener's context (app) name]

#  这里默认不改

webapp=solr

#  URL params [mandatory]

#  remainder of URL

#  这里改成下面的形式,solr同步数据时请求的链接

params=/dataimport?command=delta-import&clean=false&commit=true

#  schedule interval

#  number of minutes between two runs

#  [defaults to 30 if empty]

#这里是设置定时任务的,单位是分钟,也就是多长时间你检测一次数据同步,根据项目需求修改

#  开始测试的时候为了方便看到效果,时间可以设置短一点

interval=1


#  重做索引的时间间隔,单位分钟,默认7200,即5天;

#  为空,为0,或者注释掉:表示永不重做索引

reBuildIndexInterval=7200


#  重做索引的参数

reBuildIndexParams=/select?qt=/dataimport&command=full-import&clean=true&commit=true


#  重做索引时间间隔的计时开始时间,第一次真正执行的时间=reBuildIndexBeginTime+reBuildIndexInterval*60*1000;

#  两种格式:2012-04-11 03:10:00 或者  03:10:00,后一种会自动补全日期部分为服务启动时的日期

reBuildIndexBeginTime=03:10:00

#内容结束------------------------------

在/usr/local/tomcat/tomcat8/webapps/solr/WEB-INF/lib下添加自动增量更新Jar包
链接:https://pan.baidu.com/s/1KPf9qRPn3BePVLzdTFEvyQ
提取码:wyn4
这个jar只适合solr5.5.x版本
我的数据源配置的库和表

#执行这句话,用户自动更新判别条件  意思就是结合上面data-config.xml
deltaQuery="select id from goods where regTime> '${dataimporter.last_index_time}'"来做更新
alter table user modify  regTime(由于我这个表没有设置updatetime使用regTime代替) TIMESTAMP  NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

好了测试一下
第一次先手动导入下数据

q: 查询字符串(必须的)。*:*表示查询所有;keyword:东看 表示按关键字“东看”查询
fq: filter query 过滤查询。使用Filter Query可以充分利用Filter Query Cache,提高检索性能。作用:在q查询符合结果中同时是fq查询符合的(类似求交集),例如:q=mm&fq=date_time:[20081001 TO 20091031],找关键字mm,并且date_time是20081001到20091031之间的。
sort: 排序。格式如下:字段名 排序方式;如advertiserId desc 表示按id字段降序排列查询结果。
start,rows:表示查回结果从第几条数据开始显示,共显示多少条。
fl: field list。指定查询结果返回哪些字段。多个时以空格“ ”或逗号“,”分隔。不指定时,默认全返回。
df: default field默认的查询字段,一般默认指定。
Raw Query Parameters:
wt: write type。指定查询输出结果格式,我们常用的有json格式与xml格式。在solrconfig.xml中定义了查询输出格式:xml、json、python、ruby、php、phps、custom。
indent: 返回的结果是否缩进,默认关闭,用 indent=true | on 开启,一般调试json,php,phps,ruby输出才有必要用这个参数。
debugQuery: 设置返回结果是否显示Debug信息。
dismax:
edismax:
hl: high light 高亮。hl=true表示启用高亮
hl.fl : 用空格或逗号隔开的字段列表(指定高亮的字段)。要启用某个字段的highlight功能,就得保证该字段在schema中是stored。如果该参数未被给出,那么就会高 亮默认字段 standard handler会用df参数,dismax字段用qf参数。你可以使用星号去方便的高亮所有字段。如果你使用了通配符,那么要考虑启用 hl.requiredFieldMatch选项。	
hl.simple.pre:
hl.requireFieldMatch: 如果置为true,除非该字段的查询结果不为空才会被高亮。它的默认值是false,意味 着它可能匹配某个字段却高亮一个不同的字段。如果hl.fl使用了通配符,那么就要启用该参数。尽管如此,如果你的查询是all字段(可能是使用 copy-field 指令),那么还是把它设为false,这样搜索结果能表明哪个字段的查询文本未被找到
hl.usePhraseHighlighter:如果一个查询中含有短语(引号框起来的)那么会保证一定要完全匹配短语的才会被高亮。
hl.highlightMultiTerm:如果使用通配符和模糊搜索,那么会确保与通配符匹配的term会高亮。默认为false,同时hl.usePhraseHighlighter要为true。
facet:分组统计,在搜索关键字的同时,能够按照Facet的字段进行分组并统计。
facet.query:Facet Query利用类似于filter query的语法提供了更为灵活的Facet.通过facet.query参数,可以对任意字段进行筛选。
facet.field:需要分组统计的字段,可以多个。
facet.prefix: 表示Facet字段值的前缀。比如facet.field=cpu&facet.prefix=Intel,那么对cpu字段进行Facet查询,返回的cpu都是以Intel开头的, AMD开头的cpu型号将不会被统计在内。
spatial:
spellcheck: 拼写检查。

进行查询
当前查询了所有 并且进行了0-10分页

进行text_ik分词并进行检索 之前在managed-schema中配置了<field name="nickname" type="text_ik" indexed="true" stored="true" /> 
下面我们使用这个字段进行检索
可以看到数据只有5条

为了证明进行了分词而不是模糊查询
我进行如下搜索
可以看到如下 还是5条数据 并且有只为凉城的结果也展示出来了

下面测试增量更新  之前我们设置了自动更新时间为1分钟做测试
当前user表数据只有22条
现在新增一条


可以看到数据已经更新为23条了
等待一分钟后再去查询所有发现已经更新了

现在测试多表多字段联合检索查询
为了方便我在虚拟机再添加一个表 只有三个字段的


已经创建成功了

添加了4条数据

去更新data-conig.xml 和 managed-schema配置
cd /usr/local/tomcat/solr_home/new_core/conf
vim data-conig.xml
#添加entity如下
vim managed-schema
#添加字段如下图


product表也配置增量更新

重启tomcat solr再次导入数据 两个表总共27条数据全部查询出来了

进行多字段查询 结果如下


完成
由于solr整合到项目中有很多方式 这里就不在写了 可以百度博客

转载于:https://my.oschina.net/u/4034278/blog/3083719

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值