安装配置
1、准备一个干净的tomcat 2、把solr-4.10.3\dist中的war拷贝到tomcat的webapps文件夹下
3、把war解压 (直接启动tomcat,然后删除压缩包)
4、配置solrHome(就是配置索引库位置)
1)、把solr-4.10.3\example\solr下的所有文件拷贝到指定的位置
2)、配置solr项目和solrHome的关系:修改solr项目的web.xml的第43行,将solrHome的路径填入,并将注解标签放开
5、添加solr项目运行的扩展包,把solr-4.10.3\example\lib\ext下的所有jar放到solr项目中
6、重新启动tomcat 7、访问solr http://localhost:8080/solr
solr core 类比成mysql的database
添加solr core步骤
1、赋值solrHome下的collection1文件夹 2、粘贴到solrHome下,修改名字collection2
3、修改collection2文件夹下的core.properties 4、重启tomcat
使用solr控制台
1、添加索引
注意:1、solr中的域名是先定义后使用 2、添加文档时id不能少
2、删除索引
<delete><id>1</id></delete> <commit/> -- 根据id删除
<delete><query>id:2</query></delete> <commit/> --根据Lucene的语法删除
<delete><query>*:*</query></delete> <commit/> --根据Lucene的语法删除所有
3、修改索引
只要ID相同就会被修改(覆盖),方法和添加一样
java代码实现
需要jar包
commons-io-2.3.jar
httpclient-4.3.1.jar
httpcore-4.3.jar
httpmime-4.3.1.jar
jcl-over-slf4j-1.7.6.jar
jul-to-slf4j-1.7.6.jar
log4j-1.2.17.jar
noggit-0.5.jar
slf4j-api-1.7.6.jar
slf4j-log4j12-1.7.6.jar
solr-solrj-4.10.3.jar
wstx-asl-3.2.7.jar
zookeeper-3.4.6.jar
public class SolrManager {
public SolrManager() {
}
@Test
public void addIndex() throws Exception, IOException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrInputDocument doc = new SolrInputDocument();
doc.addField("name", "solrJ添加name");
doc.addField("content", "solrJ添加的内容");
doc.addField("id", "2");
solrServer.add(doc);
solrServer.commit();
}
@Test
public void updateIndex() throws Exception, IOException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrInputDocument doc = new SolrInputDocument();
doc.addField("name", "solrJ添加name修改");
doc.addField("content", "solrJ添加的内容修改");
doc.addField("id", "2");
solrServer.add(doc);
solrServer.commit();
}
@Test
public void deleteIndex() throws Exception, IOException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
solrServer.deleteByQuery("*:*");
solrServer.commit();
}
@Test
public void searchIndex() throws Exception, IOException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/collection1");
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("*:*");
QueryResponse queryResponse = solrServer.query(solrQuery);
SolrDocumentList results = queryResponse.getResults();
System.out.println("总记录数:" + results.getNumFound());
Iterator var6 = results.iterator();
while(var6.hasNext()) {
SolrDocument doc = (SolrDocument)var6.next();
System.out.println(doc.get("id"));
System.out.println(doc.get("name"));
System.out.println(doc.get("content"));
}
}
}