1.简单介绍
Solr是一个全文检索的服务,对外提供webservice服务,webservice使用的http协议。
Solr作为一个服务端,同时也提供很多的不同编程语言的客户端,供程序员开发使用。
2.添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mine</groupId>
<artifactId>solr</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>solr Maven Webapp</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<!-- 指定source和target的版本 -->
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<charset>utf-8</charset>
<uriEncoding>UTF-8</uriEncoding>
<port>8888</port>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
</project>
3.简单测试
/**
* Copyright (C), 2015-2017, XXX有限公司
* Description: 索引维护
* History:
* <author> <time> <version> <desc>
* simon 修改时间 版本号 描述
*/
package com.mine.solr.solrjtest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import java.io.IOException;
/**
* 〈一句话功能简述〉<br>
* 〈索引维护〉
*
* @since 1.0.0
*/
public class IndexManager
{
//定义sorl服务的地址
private String solrUrl = "http://localhost:8080/solr";
//solrj更新或添加索引,根据id判断,如果存在,更新索引,如果不存在,添加索引
@Test
public void updateIndex() throws IOException, SolrServerException {
//创建solr服务对象,通过此服务向solr服务发起请求
SolrServer solrServer = new HttpSolrServer(solrUrl);
//创建document
SolrInputDocument document = new SolrInputDocument();
//向文档中添加域
document.addField("id", "8888");
document.addField("product_name", "lucene编程指南");
document.addField("product_price", 88.8f);
document.addField("product_catalog_name", "计算机编程");
document.addField("product_description", "lucene编程指南是一本非常有用的书!");
document.addField("product_picture", "373737722.jpg");
//创建索引
solrServer.add(document);
//提交
solrServer.commit();
}
//删除索引
@Test
public void deleteIndex() throws IOException, SolrServerException {
//创建solr服务对象,通过此服务向solr服务发起请求
SolrServer solrServer = new HttpSolrServer(solrUrl);
//根据主键删除
solrServer.deleteById("8888");
//自定义查询条件删除
solrServer.deleteByQuery("id:1013");
//提交
solrServer.commit();
}
}
4. 数据导入处理器
作为程序员,我们不可能像上面那样一条一条更改添加,很麻烦,数据导入处理器替我们提供了解决方案
solr提供dataimport-Handler数据导入处理器,工作流程:
(1) solr通过dataimport-Handler查询关系数据库中的数据
(2) 对查询到的数据创建索引
上边的过程是自动化完成的。
可以通过此dataimport-Handler,批量将关系数据库中的数据创建索引到solr索引库中。
<1>加载jar包
将solr\dist\ solr-dataimporthandler-4.10.3.jar拷贝至solr\contrib\dataimporthandler
配置solrconfig.xml加载jar包:
<lib dir="${solr.install.dir:../..}/contrib/dataimporthandler/lib" regex=".*\.jar"/>
<2>加载MySQL数据库驱动包
将mysql数据库驱动的jar拷贝至solr\contrib\databaseDriver下
配置solrconfig.xml加载jar包:
<lib dir="${solr.install.dir:../..}/contrib/databaseDriver/lib" regex=".*\.jar"/>
<3>编辑data-config.xml文件,存放在SolrCore的conf目录<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/lucence"
user="root"
password="123456"/>
<document>
<entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
(column数据库列名,name为solr中定义的field的域名)<4>配置完成了,当然是加载这个配置文件,怎么加载呢?修改solrconfig.xml,添加requestHandler
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
<5>导入数据
完成,可以进行搜索,当然,我们也可以在schema.xml文件中进行一下配置,达到组合搜索的结果
<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="product_name" dest="product_keywords"/>
<copyField source="product_description" dest="product_keywords"/>
<6>搜索
可以根据这张图片编写代码
//搜索索引
@Test
public void searchIndex() throws SolrServerException {
//创建solr服务对象,通过此服务向solr服务发起请求
SolrServer solrServer = new HttpSolrServer(solrUrl);
//创建查询对象
SolrQuery solrQuery = new SolrQuery();
//查询关键字("q"不能省去,必须写)
solrQuery.set("q","product_keywords:挂钩");
//添加过滤,过滤一个不满足,想过滤多个可以使用add
solrQuery.set("fq","product_price:[1 TO 3]");
//排序
solrQuery.addSort("product_price", SolrQuery.ORDER.desc);
//如果一个排序不够,可以再加一个
//solrQuery.addSort("", SolrQuery.ORDER.asc);
//分页(知道当前页码和每页显示个数求出开始下标)
int curPage=1;
int rows=15;
//计算开始记录下标
int start=rows*(curPage-1);
//向query中设置分页参数
solrQuery.setStart(start);
solrQuery.setRows(rows);
//指定显示的field
//solrQuery.addField("product_name");
//指定默认搜索的域
solrQuery.set("df","product_keywords");
//想设置高亮的话,可以设置高亮
solrQuery.setHighlight(true);
//设置高亮参数(想设置多个的话,不多说)
solrQuery.addHighlightField("product_name");
//设置高亮前缀和后缀
solrQuery.setHighlightSimplePre("<span style=\"color:red\">");
solrQuery.setHighlightSimplePost("</span>");
//指定查询
QueryResponse response = solrServer.query(solrQuery);
//从响应中得到高亮map
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
//从响应中得到结果
SolrDocumentList docs = response.getResults();
//查看结果
for (SolrDocument doc : docs) {
System.out.print(doc.get("id")+"\t");
System.out.println(doc.get("product_name"));
//获取高亮信息
if (highlighting != null) {
Map<String, List<String>> map = highlighting.get(doc.get("id"));
if (list != null) {
List<String> list = map.get("product_name");
if (map != null) {
System.out.println(list.get(0));
}
}
}
}
}