1.搭建solr服务器
新建一个solr目录,将tomcat解压;从http://lucene.apache.org/solr/下载solr(这里使用solr4.10.3.zip)同样解压,将solr\solr-4.10.3\example\webapps目录下的solr.war包复制到solr\apache-tomcat-7.0.68\webapps,解压并删除war包(不删除tomcat启动会自动创建一个solr项目并覆盖),将solr\solr-4.10.3\example\lib\ext包下的jar包复制到solr\apache-tomcat-7.0.68\webapps\solr\WEB-INF\lib下;新建一个solrhome文件夹,复制solr\solr-4.10.3\example\solr下的全部到solrhome,打开solr\apache-tomcat-7.0.68\webapps\solr\WEB-INF下的web.xml文件配置solrhome路径
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>E:\solr\solrhome</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
启动Tomcat访问http://localhost:8080/solr/
2.打开solr\solrhome\collection1\conf下的schema.xml配置IKAnalyzer 中文分析器
<!-- IKAnalyzer -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<field name="title_ik" type="text_ik" indexed="true" stored="true" />
<field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true" />
<!--product-->
<field name="product_name" type="text_ik" indexed="true" stored="true" />
<field name="product_catalog_name" type="string" indexed="true" stored="true" />
<field name="product_price" type="float" indexed="true" stored="true" />
<field name="product_description" type="text_ik" indexed="true" stored="false" />
<field name="product_picture" type="string" indexed="true" stored="true" />
<field name="product_keywords" type="text_ik" indexed="true" stored="false" />
<copeField source="product_name" dest="product_keywords"/>
<copeField source="product_description" dest="product_keywords"/>
solrconfig.xml
<luceneMatchVersion>4.10.3</luceneMatchVersion>
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
data-config.xml(新建)
<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
<dataSource name="jdbcDataSource" type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/lucene"
user="root" password="root"/>
<document>
<entity dataSource="jdbcDataSource" name="product"
query="select * from products" >
<field column="pid" name="id"></field>
<field column="name" name="product_name"></field>
<field column="catalog_name" name="product_catalog_name"></field>
<field column="price" name="product_price"></field>
<field column="description" name="product_description"></field>
<field column="picture" name="product_picture"></field>
</entity>
</document>
</dataConfig>
3.复制solr\solr-4.10.3\dist下的solr-dataimporthandler-4.10.3.jar,solr-dataimporthandler-extras-4.10.3.jar到solr\solrhome\collection1\lib下(新建lib包)在copy一个MySQL数据库驱动包。
4.solr后台crud操作
public class SolrJManager {
// 添加
@Test
public void testadd() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", 4);
doc.setField("name", "范冰冰");
solrServer.add(doc);
solrServer.commit();
}
// 删除
@Test
public void testdelete() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
solrServer.deleteByQuery("id:4", 1000);
solrServer.commit();
}
// 修改
@Test
public void testupdate() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
// 与添加相同只要ID相同就是添加不同就是更新
}
// 查询
@Test
public void testquery() throws Exception {
String baseURL = "http://localhost:8080/solr/collection1";
// String baseURL = "http://localhost:8080/solr/collection2";
// 单机版
SolrServer solrServer = new HttpSolrServer(baseURL);
// 查询 关键词 饼干
// 过滤条件“product_catalog_name:零食”,“product_price:9.9”价格排序,分页,每页条数
SolrQuery solrQuery = new SolrQuery();
solrQuery.set("q", "product_name:饼干");
// solrQuery.setQuery("product_name:饼干");
solrQuery.set("fq", "product_catalog_name:零食");
solrQuery.set("fq", "product_price:[* TO 10]");
solrQuery.addSort("product_price", ORDER.desc);
// 分页
solrQuery.setStart(0);
solrQuery.setRows(1);
// 高亮
// 1打开开关
solrQuery.setHighlight(true);
// 2指定高亮部分
solrQuery.addHighlightField("product_name");
// 3前缀
solrQuery.setHighlightSimplePre("<span style='color:red'>");
// 4后缀
solrQuery.setHighlightSimplePost("</span>");
// 执行查询
QueryResponse response = solrServer.query(solrQuery);
// 文档结果集
SolrDocumentList docs = response.getResults();
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
//Map <K id:V map>
//Map <K 域名:V list>
//List list.get(0);
// 总条数
long numFound = docs.getNumFound();
System.out.println(numFound);
/**
* "product_catalog_name": "零食", "product_price": 9.9, "product_name":
* "小熊饼干", "id": "1", "product_picture": "1234.jpg",
*/
for (SolrDocument doc : docs) {
System.out.println(doc.get("id"));
System.out.println(doc.get("product_name"));
System.out.println(doc.get("product_catalog_name"));
System.out.println(doc.get("product_price"));
System.out.println(doc.get("product_picture"));
System.out.println("-----------");
Map<String, List<String>> map = highlighting.get(doc.get("id"));
List<String> list = map.get("product_name");
System.out.println(list.get(0));
}
}
}