solr的使用
打开
进入solr-7.7.2\bin文件夹,进入cmd控制台,输入solr start
默认端口号是8983,注意窗口不要关闭[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lbDyc5Ya-1576120772037)(F:\课上截图\cmd.png)]
浏览器输入 http://localhost:8983/solr可以访问solr后台管理
点击选项中的new_core创建一个实例,
(注意:)如果创建失败把solr-7.7.2\server\solr\configsets_default下的面conf文件夹复制到new_core实例中
** 导入驱动**
在实例中创建lib文件夹存入jar包
放好后刷新core,在core admin选择reload刷新
在实例下conf文件下添加 solrconfig.xml 文件
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
创建data-config.xml文件
在conf文件夹下创建data-config.xml,配置对应的数据库以及对应的索引数据
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/itripdb"
user="root"
password="root"/>
<document>
<entity name="product" query="SELECT * FROM itrip_user"
deltaQuery="select id from itrip_user where updateTime >'${dih.last_index_time}'"
deltaImportQuery="select id,userCode,userPassword,userType,flatID,userName,weChat,QQ,weibo,baidu,creationDate,createdBy,modifyDate,modifiedBy,activated from itrip_user where id ='${dih.delta.id}'">
<field column="id" name="id" />
<field column="userCode" name="userCode" />
<field column="userPassword" name="userPassword" />
<field column="userType" name="userType" />
<field column="flatID" name="flatID" />
<field column="userName" name="userName" />
<field column="weChat" name="weChat" />
<field column="QQ" name="QQ" />
<field column="weibo" name="weibo" />
<field column="baidu" name="baidu" />
<field column="creationDate" name="creationDate" />
<field column="createdBy" name="createdBy" />
<field column="modifyDate" name="modifyDate" />
<field column="modifiedBy" name="modifiedBy" />
<field column="activated" name="activated" />
</entity>
</document>
</dataConfig>
在managed-schema文件添加
<field name="userCode" type="text_ik" indexed="true" stored="true"/>
<field name="userPassword" type="string" indexed="false" stored="false"/>
<field name="userType" type="pint" indexed="false" stored="false"/>
<field name="flatID" type="pint" indexed="false" stored="false"/>
<field name="userName" type="text_ik" indexed="true" stored="true"/>
<field name="weChat" type="string" indexed="false" stored="false"/>
<field name="QQ" type="string" indexed="false" stored="false"/>
<field name="weibo" type="string" indexed="false" stored="false"/>
<field name="baidu" type="string" indexed="false" stored="false"/>
<field name="creationDate" type="pdate" indexed="false" stored="false"/>
<field name="createdBy" type="pint" indexed="false" stored="false"/>
<field name="modifyDate" type="pdate" indexed="false" stored="false"/>
<field name="modifiedBy" type="pint" indexed="false" stored="false"/>
<field name="activated" type="pint" indexed="false" stored="false"/>
导入
** 增量更新**
1、修改数据库
数据库增加时间字段updateTime,数据类型改为timestamp
数据库更改记录更新时间
alter table `itrip_user` modify updateTime timestamp default current_timestamp on update current_timestamp;
2、添加jar包
在solr-7.7.2\server\solr-webapp\webapp\WEB-INF\lib目录下添加solr-dataimport-scheduler.jar
3、添加配置文件,在solr-7.7.2\server\solr文件下创建conf文件夹,在文件夹中创建dataimport.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
syncCores=实例名称
# solr server name or IP address
# [defaults to localhost if empty]
server=IP地址
# solr server port
# [defaults to 80 if empty]
port=端口号
# application name/context
# [defaults to current ServletContextListener's context (app) name]
webapp=solr
# 增量索引的参数
# URL params [mandatory]
# remainder of URL
params=/dataimport?command=delta-import&clean=true&commit=true
# 重做增量索引的时间间隔
# schedule interval
# number of minutes between two runs
# [defaults to 30 if empty]
interval=1
# 重做全量索引的时间间隔,单位分钟,默认7200,即5天;
# 为空,为0,或者注释掉:表示永不重做索引
#reBuildIndexInterval=7200
# 重做索引的参数
reBuildIndexParams=/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
4、配置web.xml
在solr-7.7.2\server\solr-webapp\webapp\WEB-INF目录下的web.xml中添加
<listener>
<listener-class>org.apache.solr.handler.dataimport.scheduler.ApplicationListener</listener-class>
</listener>
5、修改实例中的data-config.xml
在文件夹solr-7.7.2\server\solr\new_core\conf中的data-config.xml中的entity标签中添加操作
deltaQuery="select id from itrip_user where updateTime >'${dih.last_index_time}'"
deltaImportQuery="select id,userCode,userPassword,userType,flatID,userName,weChat,QQ,weibo,baidu,creationDate,createdBy,modifyDate,modifiedBy,activated from itrip_user where id ='${dih.delta.id}'"
查询
中文分词
1、存放jar包
把ik-analyzer-solr7-7.x.jar存入solr-7.7.2\server\solr-webapp\webapp\WEB-INF\lib文件夹
2、存放配置文件
在solr-7.7.2\server\solr-webapp\webapp\WEB-INF\文件夹下创建classes文件夹
在原本的文件夹中复制三个配置文件到该文件夹下
ext_stopword.dic 分词文件
IKAnalyzer.cfg.xml IK配置文件
mydict.dic 字典文件
3、配置Solr实例
在solr实例中配置
<field name="name" type="text_ik" indexed="true" stored="true"/>
<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>
Java中solrj用法
1、引入jar包
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.4.0</version>
</dependency>
2、编写代码
@Test
public void search() throws IOException, SolrServerException {
String solrUrl = "http://localhost:8983/solr/new_core";
HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
SolrQuery query = new SolrQuery();
query.set("q","*:*");
query.setSort("id", SolrQuery.ORDER.desc);
query.setFields("name");
QueryResponse response = solrClient.query(query);
SolrDocumentList docs = response.getResults();
long cnt = docs.getNumFound();
System.out.println("总条数为"+cnt+"条");
for (SolrDocument doc : docs) {
System.out.println("id:"+ doc.get("u_id") + ",name:"+ doc.get("name"));
}
solrClient.close();
}
;
long cnt = docs.getNumFound();
System.out.println("总条数为"+cnt+"条");
for (SolrDocument doc : docs) {
System.out.println("id:"+ doc.get("u_id") + ",name:"+ doc.get("name"));
}
solrClient.close();
}