- 打开solr-7.7.3里面的bin文件夹,cmd输入solr start启动,默认端口8983
- localhost:8983/solr/
- 新建一个java项目
- pom.xml:
<!-- springboot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<!-- 引入solr依赖 -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
- application.yml:
spring:
data:
solr:
host: http://localhost:8983/solr/an-core #这里面的an-core
6.Test:
public class SolrUtilTest {
public static void main(String[] args) throws SolrServerException, IOException {
// addDocument();
// deteleDocument();
// updateDocument();
//queryDocument();
// updateSolrData();
queryDim();
}
/**
* 添加文档内容
*
* @throws SolrServerException
* @throws IOException
*/
public static void addDocument() throws SolrServerException, IOException {
// 连接solr服务器
HttpSolrClient httpSolrServer = new HttpSolrClient("http://localhost:8983/solr/testcore");
// 创建一个文档对象, 向文档中添加域,必须有id域,域的名称必须在scheme.xml中定义
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
SolrInputDocument document = new SolrInputDocument();
document.addField("id", 2);
document.addField("seqNo", "2");
document.addField("area", "2");
document.addField("local", "2");
document.addField("plateTime", formatter.format(date));
// 把文档对象写入索引库
httpSolrServer.add(document);
// 提交
httpSolrServer.commit();
}
/**
* 更新文档内容,跟添加的区别是:id不能变,其他的可以变
*
* @throws SolrServerException
* @throws IOException
*/
public static void updateDocument() throws SolrServerException, IOException {
// 连接solr服务器
HttpSolrClient httpSolrServer = new HttpSolrClient("http://localhost:8983/solr/testcore");
// 创建一个文档对象, 向文档中添加域,必须有id域,域的名称必须在scheme.xml中定义
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
SolrInputDocument document = new SolrInputDocument();
document.addField("id", 1);
document.addField("seqNo", "222");
document.addField("area", "222");
document.addField("local", "222");
document.addField("plateTime", formatter.format(date));
// 把文档对象写入索引库
httpSolrServer.add(document);
// 提交
httpSolrServer.commit();
}
/**
* java操作Solr的全量或增量更新,可以结合定时任务做定时全量或增量更新
* @throws SolrServerException
* @throws IOException
*/
public static void updateSolrData() throws SolrServerException, IOException {
// 连接solr服务器
HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/testcore");
//创建一个查询对象
SolrQuery solrQuery = new SolrQuery();
//增量更新全部完成;注意这里entity的值为solr-data-config.xml里entity标签里的name值
final String SOLR_DELTA_PARAM = "/dataimport?command=delta-import&entity=order_info&clean=false&commit=true";
//全量更新全部完成
final String SOLR_FULL_PARAM = "/dataimport?command=full-import&entity=order_info&clean=true&commit=true";
//设置更新方式
solrQuery.setRequestHandler(SOLR_DELTA_PARAM);
// 执行查询
QueryResponse query = httpSolrServer.query(solrQuery);
//提交
httpSolrServer.commit();
}
/**
* 查询文档内容
*
* @throws SolrServerException
* @throws IOException
*/
public static void queryDocument() throws SolrServerException, IOException {
// 连接solr服务器
HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/testcore");
// 创建一个查询条件
SolrQuery solrQuery = new SolrQuery();
// 设置查询条件
solrQuery.setQuery("*:*");
// 设置分页
solrQuery.setStart(1);
solrQuery.setRows(10);
// 执行查询
QueryResponse query = httpSolrServer.query(solrQuery);
// 取查询结果
SolrDocumentList solrDocumentList = query.getResults();
System.out.println("总记录数:" + solrDocumentList.getNumFound());
for (SolrDocument sd : solrDocumentList) {
System.out.println(sd.get("id"));
System.out.println(sd.get("seqNo"));
System.out.println(sd.get("area"));
System.out.println(sd.get("local"));
System.out.println(sd.get("plateTime"));
}
}
/**
* 模糊查询
*
* @throws SolrServerException
* @throws IOException
*/
public static void queryDim() throws SolrServerException, IOException {
// 连接solr服务器
HttpSolrServer httpSolrServer = new HttpSolrServer("http://localhost:8983/solr/testcore");
// 创建一个查询条件
SolrQuery solrQuery = new SolrQuery();
// 设置查询条件
solrQuery.setQuery("*"+ 2 +"*");
// 设置分页
solrQuery.setStart(1);
solrQuery.setRows(10);
// 执行查询
QueryResponse query = httpSolrServer.query(solrQuery);
// 取查询结果
SolrDocumentList solrDocumentList = query.getResults();
System.out.println("总记录数:" + solrDocumentList.getNumFound());
for (SolrDocument sd : solrDocumentList) {
System.out.println(sd.get("id"));
System.out.println(sd.get("seqNo"));
System.out.println(sd.get("area"));
System.out.println(sd.get("local"));
System.out.println(sd.get("plateTime"));
}
}
/**
* 删除文档
*
* @throws SolrServerException
* @throws IOException
*/
public static void deteleDocument() throws SolrServerException, IOException {
// 连接solr服务器
HttpSolrClient httpSolrServer = new HttpSolrClient("http://localhost:8983/solr/testcore");
// 根据id删除
httpSolrServer.deleteById("1");
// 根据条件删除
// httpSolrServer.deleteByQuery("");
// 提交
httpSolrServer.commit();
}