1.1下载http://archive.apache.org/dist/lucene/solr/8.0.0/ 大家也可以下载其他的
1.2下载 ik分词
https://search.maven.org/search?q=com.github.magese
选择jar包下载
2.1 安装core 将solr-8.0.0\example\example-DIH\solr\db文件夹拷贝到\solr-8.0.0\server\solr
并改名字student>文件内的croe.properties打开写入 name=student
这是界面识别core的配置
2.2进入\solr-8.0.0\server\solr\student\conf
编辑managed-schema,在fieldType便签上下文处添加ik分词器
<!-- 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>
编辑db-data-config.xml 从数据库导入数据设置
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/school?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false"
user="rootUser"
password="111"/>
<document name="testDoc">
<entity name="user" pk="id" query="select * from student">
<field column="id" name="id"/>
<field column="name" name="name"/>
<field column="age" name="age"/>
</entity>
</document>
</dataConfig>
进入\solr-8.0.0\server\solr-webapp\webapp\WEB-INF\lib把分词器考进去 ik-analyzer-8.3.0.jar
进入\solr-8.0.0\server\solr-webapp\webapp\WEB-INF\classes(你要创建一个)
新建IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 扩展配置</comment>
<!--用户可以在这里配置自己的扩展字典-->
<entry key="ext_dict">mydict.dic;</entry>
<!--用户可以在这里配置自己的扩展停止词字典-->
<entry key="ext_stopwords">ext_stopword.dic</entry>
</properties>
新建
ext_stopword.dic
这个是分词过滤,这些字都不会被作为分词
也
了
仍
从
以
使
则
却
又
及
对
就
并
很
或
把
是
的
着
给
而
被
让
在
还
比
等
当
与
于
但
乃
了
好
进入\solr-8.0.0\server\lib 考入mysql-connector-java-5.1.18.jar (可以从自己的maven库中找到)
进入\solr-8.0.0\bin 运行 访问localhost:8983/solr/
导入数据
查询数据 当然我们还有2个字段没有显示,因为没有设置field
添加字段
那么就到了SolrJ的环节了 本想用springbootsolr 但是,试了1天也没有反应
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.4.0</version>
</dependency>
直接上代码,
注意的是solr7版本和4版本有很大的区别
说明下代码逻辑:
首先是根据不同的功能提供了不用使用的方法
1.链接solr客户端
2.main方法运行
3.根据查询条件
4.根据map查询
5.添加单个数据
6.List添加数据
7.通过对象添加数据
8.通过查询返回对象
9搜索删除
package cn.activemq.solr.service;
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.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.MapSolrParams;
import java.io.IOException;
import java.util.*;
public class SolrTest {
public void query() throws IOException, SolrServerException {
HttpSolrClient build=getBuild();//获取链接对象
//封装查询条件
SolrQuery query = new SolrQuery("*:*");
QueryResponse response = build.query(query);
SolrDocumentList documents = response.getResults();
for(SolrDocument doc : documents) {
System.out.println("id:"+doc.get("id")+"name:"+doc.get("name"));
}
build.close();
}
public void queryByMap() throws IOException, SolrServerException {
HttpSolrClient build=getBuild();//获取链接对象
//封装查询参数
Map<String, String> queryParamMap = new HashMap<String, String>();
queryParamMap.put("q", "*:*");
//添加到SolrParams对象
MapSolrParams queryParams = new MapSolrParams(queryParamMap);
QueryResponse response = build.query(queryParams);
SolrDocumentList documents = response.getResults();
for(SolrDocument doc : documents) {
System.out.println("id:"+doc.get("id")+"name:"+doc.get("name"));
}
build.close();
}
//添加索引信息
public void addSolr() throws IOException, SolrServerException {
HttpSolrClient build=getBuild();//获取链接对象
SolrInputDocument doc = new SolrInputDocument();//[2]创建文档doc
String str = UUID.randomUUID().toString();//[3]添加内容
System.out.println(str);
doc.addField("id", str);//设置添加的字段域
doc.addField("name", "Amazon Kindle Paperwhite");//设置添加的字段域
UpdateResponse updateResponse = build.add(doc);//添加到client
System.out.println(updateResponse.getElapsedTime());
build.commit();//索引文档必须commit
build.close();
}
//删除索引单个id
public void solrDeleteByOne(String id) throws Exception{
HttpSolrClient client = getBuild();//[1]获取连接
client.deleteById(id);//[2]通过id删除
client.commit();//[3]提交
client.close();//[4]关闭资源
}
//多个索引删除用List
public void solrDeleteByList() throws Exception{
HttpSolrClient client = getBuild();//[1]获取连接
ArrayList<String> ids = new ArrayList<String>();//[2]通过id删除
ids.add("3");
ids.add("4");
client.deleteById(ids);
client.commit();//[3]提交
client.close();//[4]关闭资源
}
//通过对象添加
public void addBean() throws IOException, SolrServerException {
HttpSolrClient client=getBuild();
Student student=new Student(1,"张大明白",24,3);
UpdateResponse response = client.addBean(student);
client.commit();//[5]提交操作
client.close();//[6]关闭资源
}
//查询返回对象
public void quertyReturnBean() throws IOException, SolrServerException {
HttpSolrClient client=getBuild();
SolrQuery query=new SolrQuery("*:*");//查询条件
//添加回显的内容
query.addField("id");
query.addField("name");
query.addField("age");
query.addField("clazz");
query.setRows(3);//设置每页显示多少条
QueryResponse response = client.query(query);
List<Student> list=response.getBeans(Student.class);
for (Student li:list){
System.out.println(li.toString());
}
client.close();
}
//搜索删除
public void deleteByQuery() throws IOException, SolrServerException {
HttpSolrClient client=getBuild();
client.deleteByQuery("id:5");//执行删除
client.commit();//提交操作
client.close(); //关闭资源
}
public static void main(String[] args) {
SolrTest solrTest=new SolrTest();
try {
solrTest.deleteByQuery();
} catch (IOException e) {
e.printStackTrace();
} catch (SolrServerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//封装链接对象
public HttpSolrClient getBuild(){
final String solrUrl = "http://127.0.0.1:8983/solr/student/";
//创建solrClient同时指定超时时间,不指定走默认配置
HttpSolrClient build = new HttpSolrClient.Builder(solrUrl)
.withConnectionTimeout(10000)
.withSocketTimeout(60000)
.build();
return build;
}
}
package cn.activemq.solr.service;
import lombok.Data;
import org.apache.solr.client.solrj.beans.Field;
import java.io.Serializable;
@Data
public class Student implements Serializable {
@Field("id")
private Integer id;
@Field("name")
private String name;
@Field("age")
private Integer age;
@Field("clazz")
private Integer clazz;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getClazz() {
return clazz;
}
public void setClazz(Integer clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", clazz=" + clazz +
'}';
}
public Student( ) { }
public Student(Integer id, String name, Integer age, Integer clazz) {
this.id = id;
this.name = name;
this.age = age;
this.clazz = clazz;
}
}
设置登录权限
进入\solr-8.0.0\server\etc创建
verify.properties
文件写入
#用户名 密码 权限
admin1:111,admin
admin2:222,admin
admin3:333,admin
进入\solr-8.0.0\server\contexts下修改
solr-jetty-context.xml
GET标签被为新添加部分
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath"><Property name="hostContext" default="/solr"/></Set>
<Set name="war"><Property name="jetty.base"/>/solr-webapp/webapp</Set>
<Set name="defaultsDescriptor"><Property name="jetty.base"/>/etc/webdefault.xml</Set>
<Set name="extractWAR">false</Set>
<Get name="securityHandler">
<Set name="loginService">
<New class="org.eclipse.jetty.security.HashLoginService">
<Set name="name">verify—name</Set>
<Set name="config"><SystemProperty name="jetty.home" default="."/>/etc/verify.properties</Set>
</New>
</Set>
</Get>
</Configure>
进入\solr-8.0.0\server\solr-webapp\webapp\WEB-INF修改
web.xml
找到security-constraint
标签 将其内容全部删掉替换
<security-constraint>
<web-resource-collection>
<web-resource-name>Solr</web-resource-name>
<url-pattern>/</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>verify-name</realm-name>
</login-config>
访问时我们也可以使用
http://admin:111@localhost:8088/solr/