1.下载solr
下载地址 http://www.apache.org/dyn/closer.lua/lucene/solr/8.0.0
windows下载zip,linux下载tgz
下载完解压
2.solr启动&停止
solr-8.0.0\bin目录下执行cmd
solr start 启动
solr stop -all
3.创建solrcore
-
\solr-8.0.0\server\solr\目录下新建文件夹(如:new_db)
-
将example\example-DIH\solr\db下的文件 copy到/new_core下
-
将数据库驱动(mysql-cnnector-java-5.1.47.jar) 和dist文件夹里的solr-dataimporthandler-8.0.0.jar
拷贝到\solr-8.0.0\server\solr-webapp\webapp\WEB-INF\lib\下
- 打开new_core下的conf目录在db-data-config.xml、managed-schema中进行配置
以下是我的例子
db-data-config.xml
:
<dataConfig>
<!-- 数据源 -->
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC&tinyInt1isBit=false"
user="root"
password="000000"/>
<document>
<!--
entity实体封装sql查出的数据
name:名称
query:sql语句
field:查出的字段
-->
<entity name="item" query="select * from bilibili_channel"
deltaQuery="select id from bilibili_channel where last_modified > '${dataimporter.last_index_time}'">
<field column="id" name="id" />
<field column="title" name="title" />
<field column="cover" name="cover" />
<field column="up" name="up" />
<field column="playCounts" name="playCounts" />
<field column="Thumb" name="Thumb" />
<field column="duration" name="duration" />
<field column="categoryId" name="categoryId" />
</entity>
</document>
</dataConfig>
managed-schema
:
配置了我查出的字段
indexed 是否索引:
索引的的目的是为了搜索.
需要搜索的域就一定要创建索引,只有创建了索引才能被搜索出来
不需要搜索的域可以不创建索引
需要索引: 文件名称, 文件内容, id, 身份证号等
不需要索引: 比如图片地址不需要创建索引, e:\xxx.jpg,因为根据图片地址搜索无意义
stored 是否存储:
存储的目的是为了显示.
是否存储看个人需要,存储就是将内容放入Document文档对象中保存出来,会额外占用磁盘空间, 如果搜索的时候需要马上显示出来可以放入document中也就是要存储,这样查询显示速度快, 如果不是马上立刻需要显示出来,则不需要存储,因为额外占用磁盘空间不划算.
<!-- int类型里面没有自己定义一个 -->
<fieldType name="int" class="solr.IntPointField" positionIncrementGap="0"/>
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="title" type="string" indexed="true" stored="true" />
<field name="cover" type="string" indexed="false" stored="true" />
<field name="up" type="string" indexed="true" stored="true" />
<field name="playCounts" type="int" indexed="false" stored="true" />
<field name="Thumb" type="int" indexed="false" stored="true" />
<field name="duration" type="string" indexed="false" stored="true" />
<field name="categoryId" type="int" indexed="false" stored="false" />
- ik分词器的配置
下载地址:https://search.maven.org/search?q=com.github.magese
将下载好的jar包放入solr-7.4.0/server/solr-webapp/webapp/WEB-INF/lib目录中
然后再解压一份jar包,将里面的5个配置文件放入D:\study\solr-8.0.0\server\solr-webapp\webapp\WEB-INF\classes目录下(如果无classes新建一个)
IKAnalyzer.cfg.xml、 ext.dic、 stopword.dic、 ik.conf、 dynamicdic.txt
ext.dic中可以自定义你的分词,比如我在里面输入了"希没"两个毫无关联不会被分成词语的字,在查询"有冠希没关系" ,"希没"就会被分成一个词
在managed-schema中加入以下代码
<!-- ik分词器 -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
提示:报错请查看客户端页面的Logging (localhost:8983) 或者\solr-8.0.0\server\logs\查看更详细的日志
在客户端测试下看可不可以查出来
4.编写Java代码
引入对应版本的solrJ依赖
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>8.0.0</version>
</dependency>
solr初始化
//solr连接url
public static final String solrUrl = "http://localhost:8983/solr/bilibili_db/";
//创建Solr的客户端连接对象
SolrClient solrServer = new HttpSolrClient.Builder(solrUrl).build();
//创建一个索引文档
SolrInputDocument sd = new SolrInputDocument();
下面是我的查询方法
public static SolrDocumentList search(SolrQueryUtil q) throws IOException, SolrServerException {
//定义url
String solrUrl = "http://localhost:8983/solr/"+q.getCoreName();
//创建Solr的客户端连接对象
HttpSolrClient solrClient = new HttpSolrClient.Builder(solrUrl).build();
//创建查询对象
SolrQuery query = new SolrQuery();
//q查询条件
query.set("q",q.getQuery());
//根据id排序
query.setSort(q.getSort(),SolrQuery.ORDER.asc);
query.setStart(q.getStart());
query.setRows(q.getRows());
//查询响应对象
QueryResponse response = solrClient.query(query);
//获取查询结果列表
SolrDocumentList docs = response.getResults();
//获取查询出的数量
long count = docs.getNumFound();
/*遍历结果列表
for (SolrDocument doc : docs) {
System.out.println("id:"+doc.get("id")+",title:"+doc.get("title")+"up:"+doc.get("up"));
}*/
//关闭客户端
solrClient.close();
return docs;
}
controller中调用
@RequestMapping("searchvideos")
public Dto searchVideo(Video v){
//传入coreName 查询条件(name:"" 支持or或者and及模糊查询**)
SolrQueryUtil query = new SolrQueryUtil("bilibili_db",
"title:*"+v.getTitle()+"* or up:*"+v.getUp()+"*","id",0,5);
SolrDocumentList docs = null;
try {
docs = SolrService.search(query);
} catch (IOException | SolrServerException e) {
e.printStackTrace();
return DtoUtil.returnFail("系统异常,获取失败","searchError");
}
return DtoUtil.returnDataSuccess(docs);
}