solr在linux下的安装和学习
solr了解
Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器。Solr提供
了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展,并对索引、搜索性能进行了优化。
Solr可以独立运行,运行在Jetty、Tomcat等这些Servlet容器中,Solr 索引的实现方法很简单,用 POST 方法向
Solr 服务器发送一个描述 Field 及其内容的 XML 文档,Solr根据xml文档添加、删除、更新索引 。Solr 搜索只需要发送
HTTP GET 请求,然后对 Solr 返回Xml、json等格式的查询结果进行解析,组织
页面布局。Solr不提供构建UI的功能,Solr提供了一个管理界面,通过管理界面可以查询Solr的配置和 运行情况。
全文索引服务器(solr,Lucene)
按照原有模式:web浏览器—>web服务器—>数据库(可以通过各个字段搜索 模糊匹配 like)(效率极低)—>提升效率—>web服务器—>solr服务 。
下载地址:https://www.apache.org/dyn/closer.lua/lucene/solr/8.5.0/solr-8.5.0.tgz
了解目录结构:window中的目录跟linux中的目录一样
bin:solr的运行脚本
contrib:solr的一些软件/插件,用于增强solr的功能。
dist:该目录包含build过程中产生的war和jar文件,以及相关的依赖文件。
docs:solr的API文档
example:solr工程的例子目录:
server:solr 运行的 war 文件和core
licenses:solr相关的一些许可信息
安装
安装前你的linux中要有jdk。因为我的已经有的所以直接安装
解压到指定目录 /usr/local/
tar -zxvf 压缩包.tgz
启动solr服务
切换到bin目录
cd /usr/local/solr-8.5.0/bin
启动服务:
./solr start -force
停止服务:
./solr stop -all
在外部访问
启动时遇到的问题
问题一:
linux 启动solr服务提示Your open file limit is currently 1024
解决方案:
一、简介
在linux系统中,当安装如elasticsearch、solr等应用时,linux系统会提示各种因各种限制
而导致安装失败,这里对遇到的一些进行介绍。
二、各种限制
1、查看系统所有限制
命令: ulimit -a
2、修改打开文件数限制
现象:
*** [WARN] *** Your open file limit is currently 1024. It should be set to 65000 to avoid operational disruption.
解决:
a)先切换到root账号(注意,若没切换,操作是不成功的);
b)以root身份修改/etc/security/limits.conf文件,在最后添加
(注意/是转义字符。如果在文件中不要写上/)
/* hard nofile 65000
/* soft nofile 65000
3、修改打开进程限制
现象:
*** [WARN] *** Your Max Processes Limit is currently \7219. It should be set to 65000 to avoid operational disruption.
解决:
a)先切换到root账号(注意,若没切换,操作是不成功的);
b)以root身份修改/etc/security/limits.conf文件,在最后添加
(注意/是转义字符。如果在文件中不要写上/)
/* hard nproc 65000
/* soft nproc 65000
问题:
* [WARN] * Your Max Processes Limit is currently 3806. It
should be set to 65000 to avoid opera
展开
solr启动报错
将/usr/solr-7.5.0/bin/solr.in.sh文件中,SOLR_ULIMIT_CHECKS设置为false,消除WARN
/# Settings for common system values that may cause operational imparement when system defaults are used.
/ # Solr can use many processes and many file handles. On modern operating systems the savings by leaving
/ # these settings low is minuscule, while the consequence can be Solr instability. To turn these checks off, set
/ # SOLR_ULIMIT_CHECKS=false either here or as part of your profile.
/ # Different limits can be set in solr.in.sh or your profile if you prefer as well.
/ #SOLR_RECOMMENDED_OPEN_FILES=
#SOLR_RECOMMENDED_MAX_PROCESSES=
SOLR_ULIMIT_CHECKS=false
创建solr实例 core
在/usr/local/solr-8.5.0/bin下创建实例的命令
./solr create -c new_core -force
创建document
查询document
通过mysql导入数据到solr
第一步:导入相关包:
从Solr源码包的dist文件夹中导入两个solr-dataimporthandler包,以及一个mysql驱动包。放置
到/Users/qingyun/Documents/solr-8.5.0/server/solr-webapp/webapp/WEB-INF/lib 目录中。
cp solr-dataimporthandler-* /usr/local/solr-8.5.0/server/solr-webapp/webapp/WEB-INF/lib
第二步:编辑下边路径中的solrconfig文件(new_core 为自己所创建的核心文件夹):
solr-8.0.0/server/solr/new_core/conf
vi 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进行如下编辑(没有则创建):
<?xml version="1.0" encoding="UTF-8" ?> <dataConfig>
<!-- 数据库信息 -->
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/solr" user="root" password="root"/>
<document>
<!-- document实体 -->
<entity name="test" query="SELECT * FROM solr_test">
<!-- 数据库字段映射solr字段 -->
<field column="id" name="id"/>
<field column="title" name="title"/>
</entity>
</document>
</dataConfig>
然后在当前路径的managed-schema文件中加入相应的类型映射
重新启动solr
./solr stop -all
./solr start -force
进入以下模块,执行excute就导入数据了,没反应过来可以进行
refresh status
删除core
中文分词器
solr8.0 ik中文分词器的简单配置
1、下载ik分词器,以下是solr8.0 ik分词器下载地址
https://files.cnblogs.com/files/ITDreamer/ikanalyzer-solr6.5.zip
2、然后将解压出来的两个jar包放到以下路径:
solr-8.5.0/server/solr-webapp/webapp/WEB-INF/lib
3、其它的三个文件放到以下路径:
/Documents/solr-8.5.0/server/solr-webapp/webapp/WEB-INF/classes
如果没有classes文件夹就创建一个
4、然后进行ik分词器的配置,编辑以下路径的managed-schema文件
cd /usr/local/solr-8.5.0/server/solr/new_core/conf
将以下配置放到后边
<!-- 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>
5、扩展字典的设置
到这里ik分词器就配置好了,如果需要设置扩展字典和扩展停止字典,只需要编辑下列路径的ext(扩展
字典)和stopword(扩展停止字典)文件就行了
/Users/qingyun/Documents/solr-8.0.0/server/solr-webapp/webapp/WEB-INF/classes
第二种方式安装中文分词器的方式
第一步:将分词器依赖包添加到solr类路径中
默认smartcn、icu分词器是在{solr-install-home}/contrib/analysis-extras/lucene-libs下的,我们需要
将其添加到solr识别的路径下
官方的方法是在solrconfig.xml文件中添加标签(里面已经有一些默认配置了,复制一下,改改路径就行)
将这两个目录中的jar包copy 到 /usr/local/solr-8.5.0/server/solr-webapp/webapp/WEB-INF/lib
第二步:修改managed-schema添加中文分词FieldType
<fieldType name="text_zh" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
<filter class="solr.CJKWidthFilterFactory"/>
<filter class="solr.StopFilterFactory" words="org/apache/lucene/analysis/cn/smart/stopwords.txt"/>
<filter class="solr.PorterStemFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
<filter class="solr.CJKWidthFilterFactory"/>
<filter class="solr.StopFilterFactory" words="org/apache/lucene/analysis/cn/smart/stopwords.txt"/>
<filter class="solr.PorterStemFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
managed-schema配置业务字段
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="item_category_name" type="text_general" multiValued="false" stored="true"/>
<field name="item_desc" type="text_zh" multiValued="false" indexed="true" stored="false"/>
<field name="item_image" type="text_general" multiValued="false" stored="true"/>
<field name="item_price" type="plong" multiValued="false" stored="true"/>
<field name="item_sell_point" type="text_zh" multiValued="false" indexed="true" required="false" stored="true"/>
<field name="item_title" type="text_zh" multiValued="false" indexed="true" required="false" stored="true"/>
<field name="item_keywords" type="text_zh" multiValued="true" indexed="true" required="false" stored="false"/>
<copyField source="item_title" dest="item_keywords"/>
<copyField source="item_sell_point" dest="item_keywords"/>
<copyField source="item_category_name" dest="item_keywords"/>
<copyField source="item_desc" dest="item_keywords"/>
solr与java的结合
创建一个项目加入solr的jar包
pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import java.io.IOException;
public class TestSolr {
@Test
public void add() throws IOException, SolrServerException {
//数据库服务 搜索引擎
//创建solr客户端对象,目的是连接solr服务器
//数据库服务 搜索引擎
// 创建solr客户端对象,目的是链接solr服务器
HttpSolrClient client = new HttpSolrClient.Builder() .withBaseSolrUrl("http://10.10.13.122:8983/solr/new_core").build();
//创建文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
// 第一个参数:域的名称,域的名称必须是在schema.xml中定义的
// 第二个参数:域的值
document.addField("id", 3);
document.addField("name", "使用solrJ添加的文档");
document.addField("age", 18);
//添加document到client
client.add(document);
//提交到solr
client.commit();
}
@Test
public void update() throws IOException, SolrServerException {
//数据库服务 搜索引擎
//创建solr客户端对象,目的是连接solr服务器
//数据库服务 搜索引擎
// 创建solr客户端对象,目的是链接solr服务器
HttpSolrClient client = new HttpSolrClient.Builder() .withBaseSolrUrl("http://10.10.13.122:8983/solr/new_core").build();
//创建文档对象
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
// 第一个参数:域的名称,域的名称必须是在schema.xml中定义的
// 第二个参数:域的值
document.addField("id", 2);
document.addField("name", "使用solrJ添加的文档dd");
document.addField("age", 18);
//添加document到client
client.add(document);
//提交到solr
client.commit();
}
//查询
@Test
public void query() throws IOException, SolrServerException {
HttpSolrClient client = new HttpSolrClient.Builder() .withBaseSolrUrl("http://10.10.13.122:8983/solr/new_core").build();
SolrQuery solrQuery = new SolrQuery();
//设置查询条件
solrQuery.setQuery("*:*");
// 执行查询
QueryResponse queryResponse = client.query(solrQuery);
//取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
for (SolrDocument solrDocument:solrDocumentList){
System.out.print(solrDocument.get("id"));
System.out.print(solrDocument.get("name"));
System.out.print(solrDocument.get("age"));
System.out.println();
}
}
//查询 复杂
@Test
public void query2() throws IOException, SolrServerException {
HttpSolrClient client = new HttpSolrClient.Builder() .withBaseSolrUrl("http://10.10.13.122:8983/solr/new_core").build();
SolrQuery solrQuery = new SolrQuery();
//设置查询条件
solrQuery.setQuery("*:*");
solrQuery.setFilterQueries("name:*");
solrQuery.setSort("id", SolrQuery.ORDER.desc);
solrQuery.setStart(0);
solrQuery.setRows(10);
solrQuery.setFields("id","name","age");
// 执行查询
QueryResponse queryResponse = client.query(solrQuery);
//取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
for (SolrDocument solrDocument:solrDocumentList){
System.out.print(solrDocument.get("id"));
System.out.print(solrDocument.get("name"));
System.out.print(solrDocument.get("age"));
System.out.println();
}
}
@Test
public void deleteById() throws IOException, SolrServerException {
HttpSolrClient client = new HttpSolrClient.Builder() .withBaseSolrUrl("http://10.10.13.120:8983/solr/new_core").build();
client.deleteById("2");
client.commit();
}
@Test
public void deleteByQuery() throws IOException, SolrServerException {
HttpSolrClient client = new HttpSolrClient.Builder() .withBaseSolrUrl("http://10.10.13.120:8983/solr/new_core").build();
client.deleteByQuery("*:*");
client.commit();
}
}
solr搜索结果如下所示: