1、安装
下载,解压
java -jar start.jar
访问
http://localhost:8983/solr/
2、数据导入
solr有多种数据导入方式,此处我们直接从数据库导入
solr-4.6.1\solr-4.6.1\example\solr\collection1\conf\db-data-config.xml
<dataConfig>
<dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/docsearch" user="root" password="root" />
<document name="function">
<entity name="function" query="select
functionid as id,
name,
description,
uri as url,
num as num_t,
leader as leader_t,
requestsample as requestsample_t,
responsesample as responsesample_t,
divideid as divideid_i,
remark as remark_t,
lastupdatetime as lastupdatetime_t,
lastupdateperson as lastupdateperson_t,
cachedata as cachedata_t,
cachetime as cachetime_t,
needlogin as needlogin_t,
isapi as isapi_t,
createtime as createtime_t,
integratedapproach as integratedapproach_t,
frequency as frequency_t,
dataamount as dataamount_t,
mode as mode_t,
status_ as status_s,
alteradvice as alteradvice_t
from function">
</entity>
</document>
<document name="fields">
<entity name="fields" query="select
fieldid as id,
num as num_t,
fieldname as fieldname_t,
classify as classify_t,
isnull as isnull_t,
lettername as lettername_t,
javatype as javatype_t,
description as description_t,
type as type_t,
status_ as status_t,
functionid as functionid_i
from fields">
</entity>
</document>
</dataConfig>
参考schema
solr-4.6.1\solr-4.6.1\example\solr\collection1\conf\schema.xml
到控制台导入
http://localhost:8983/solr/#/collection1/dataimport//dataimport
3、查询
http://localhost:8983/solr/#/collection1/query
4、集成到自己项目里
通过solrj方式,http方式调用solr服务器进行查询
<!-- solr client -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.5.0</version>
</dependency>
public class SolrQuery {
private static final String url = "http://localhost:8983/solr";
private static HttpSolrServer server = null;
public static QueryResponse query(SolrParams params){
try {
if(server==null){
server = new HttpSolrServer( url );
}
return server.query(params);
} catch (Exception e) {
return new QueryResponse();
}
}
}
ModifiableSolrParams params = new ModifiableSolrParams();
String q = "name:"+word+" or description:"+word+" or leader_t:"+word+" or url:"+word+" or num_t:"+word;
params.set("q", q);
// params.set("defType", "edismax");
params.set("start",0);
params.set("rows", 100);
params.set("wt", "josn");
QueryResponse res = SolrQuery.query(params);
SolrDocumentList results = res.getResults();
if (logger.isDebugEnabled()) {
logger.debug("q=" + q);
logger.debug("results.size()=" + results.size());
}