环境:Solr5.2 tomcat8.5 jdk1.7
jar准备: solr-dataimportscheduler-1.1(定时任务选择正确版本)、sqljdbc4.jar 、ik-analyzer-5.3.0.jar
js:jquery.autocomplete.min.js、jquery-ui.js
传送门 钥匙: tbad
成果:
一、安装Solr
1、解压tomcat8.5、解压solr5.2
2、发布war包后删除war
3、创建solrhome,新建文件夹solrhome,并配置solrhome:修改tomcat8.5\webapps\solr\WEB-INF\web.xml,
4、复制样例到solrhome中
- 新建核心在solrehome中增加核心core1文件夹,在core中增加conf和data文件夹,复制solr5.2.1\server\solr\configsets\basic_configs\conf所有文件到新建的conf中,复制solr-5.2.1\server\solr\solr.xml到core1文件夹下
5、配置sqlserver数据源-新建data-config.xml,在core1文件夹conf下
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://192.168.82.88:1433;DatabaseName=*;username=*;password=*" user="*" password="*" batchSize="100"/>
<document>
<entity name="ggtable" transformer="ClobTransformer,HTMLStripTransformer,DateFormatTransformer" pk="ggtable_id"
query="select ggtable_id,ggtitle,ggdate from ggtable"
deltaImportQuery="select ggtable_id,ggtitle,ggdate from ggtable"
deltaQuery="select ggtable_id,ggtitle,ggdate from ggtable where ggdate > '${dataimporter.last_index_time}'">
<field column="ggtable_id" name="id" />
<field column="ggtitle" name="ggtitle" />
<field column="ggdate" name="ggdate" />
</entity>
</document>
</dataConfig>
修改core1核心的solrconfig.xml
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">F:\Solr\solrhome\core1\conf\data-config.xml</str>
</lst>
</requestHandler>
修改scheme.xml 增加所需字段和suggestion、IK中文分词器
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!-- 配置suggestion-->
<field name="id" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />
<field name="ggtitle" type="text_ik" indexed="true" stored="true"
termVectors="true" multiValued="true" />
<field name="suggestion" type="text_suggest" multiValued="false" indexed="true" stored="true"/>
<copyField source="ggtitle" dest="suggestion"/>
<field name="ggdate" type="date" indexed="true" stored="true" multiValued="false" />
<fieldType name="text_suggest" class="solr.TextField">
<analyzer type="index">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
配置增量索引:将solr-5.2.1\dist下的solr-dataimporthandler-5.2.1.jar 和solr-dataimporthandler-extras-5.2.1.jar 及下载的solr-dataimportscheduler-1.1放到项目中
在solrhome\conf新建dataimport.properties
#################################################
# #
# dataimport scheduler properties #
# #
#################################################
#Mon Dec 17 16:10:28 CST 2018
last_index_time=2018-12-17 16\:10\:09
ggtable.last_index_time=2018-12-17 16\:10\:09
cost.last_index_time=2018-12-14 10\:00\:50
# 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:simple
syncCores=core1
# solr server name or IP address
# [defaults to localhost if empty]
server=localhost
# solr server port
# [defaults to 80 if empty]
# 安装solr的tomcat端口,如果你使用的是默认的端口,就不用改了,否则改成自己的端口就好了
port=8081
# 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
最后配置solr项目的tomcat8.5\webapps\solr\WEB-INF\web.xml增加监听器
<listener>
<listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>
访问solr、创建suggestion:http://localhost:8081/solr/core1/suggest?suggest=true&suggest.dictionary=suggest&wt=json&suggest.q=Ath&suggest.build=true
solr页面效果:
二、页面展示
1、通过ajax发送jsonp请求返回json数据并在页面展示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=8,9,10" >
<!-- 默认使用最高级的IE -->
<script type="text/javascript" src="<c:url value='/js/jquery-1.8.3.js' />"></script>
<script type="text/javascript" src="<c:url value='/js/jquery.autocomplete.min.js' />"></script>
<script type="text/javascript" src="<c:url value='/js/jquery-ui.js' />"></script>
<link rel="stylesheet" type="text/css" href="<c:url value='/css/jquery-ui.min.css' />">
</head>
<body>
<table width="186" border="0" align="center" cellpadding="0"
cellspacing="0">
<tr>
<td height="30" bgcolor="#1a7530"><div align="center">
<strong><font color="#ffffff"> 检索<font></strong>
<input id="searchbytitle"
type="text" name="searchvalue" size="12" > <a href="javascript:void(0)" id="shouyesousuo" onclick="searchbytitle()" style="CURSOR: hand"><img
src="images/search.gif" width="16" height="16" border=0></a>
</div><div id="result"></div></td>
</tr>
</table>
<script>
var sear;
$(function(){//获取solr
//搜索引擎关键字自动填充
var solrServer = ${solrServer};//"http://192.168.96.234:8081/solr/core1/suggest";
$( "#searchbytitle" ).autocomplete({
source: function( request, response ) {
sear=request.term;
$.ajax({
type : "get",
url : solrServer,
data : {
wt : "json",
q : request.term,
indent : true,
"json.wrf" : 'callback'
},
dataType : "jsonp",
jsonpCallback : "callback",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
success :function(data){
response($.map(data.suggest.suggest[sear].suggestions, function(item) {
return {
label: item.term,
value: item.term
}
}));
},
error : function() {
$("#result").html("<font color=\"red\">Solr服务未开启,请直接回车查询!</font>");
},
select: function(event, ui){
$(this).value = ui.term;
//$("#city").val( ui.term );
// 必须阻止事件的默认行为,否则autocomplete默认会把ui.item.value设为输入框的value值
event.preventDefault();
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
},
minLength: 2//输入两个字符才会发送请求
});
});
function callback(data) {
for( var i = 0; i < data.suggest.suggest[sear].suggestions.length;i++){
//alert(data.suggest.suggest[sear].suggestions[i].term);
}
}
</body>
三、Linux下配置定时任务更新Solr的索引、Solr配置增量索引
1、使用crontab 创建定时任务:每天11点半和17点半执行脚本
crontab -l查看定时任务
tail -f /var/log/cron 查看执行记录
crontab -e
30 11,17 * * * /bin/sh /home/test.sh
创建test.sh脚本
#!/bin/bash
curl -s "http://192.178.10.1:8093/core1/suggest?suggest=true&suggest.dictionary=suggest&wt=json&suggest.q=Ath&suggest.build=true"
2、solr增量索引配置
将dataimport.propertiesf放到solehome/conf下,在web.xml中配置监听器,项目中lib放上solr-dataimportscheduler-1.1,重启tomcat.
判断增量是否成功:新增一条记录,注意判断时间,如果查询结果改变则成共