solr4.0安装和使用

(笔记自己看的,参考博客:http://guoyunsky.iteye.com/blog/1738168 等)

 

1.apache-solr-4.0.0.zip包中自带的solr.war项目copy到$TOMCAT_HOME/webapps目录下

 

2.$TOMCAT_HOME/conf/Catalina/localhost里新建solr.xml文件

xml:

<Context docBase="E:/temp/tomcat/webapps/solr" debug="0" crossContext="true" >   
    <Environment name="solr/home" type="java.lang.String" value="E:/temp/tomcat/work/Catalina/localhost/solr" override="true" />  
</Context> 
 

3.中文分词器引入:IKAnalyzer2012FF_u1.jar,IKAnalyzer.cfg.xml,stopword.dic3个文件copy到solr项目的lib目录下,在$TOMCAT_HOME\work\Catalina\localhost\solr\collection2\conf\schema.xml中的<type></types>中间添加:

<!--ik_anlyzer-->
<fieldType name="text_ik" class="solr.TextField">
        <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
        <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

4.部署完毕,进行调用(可远程http协议传输文件)

private static final String url = "http://127.0.0.1:8888/solr/";
	
public static HttpSolrServer getSolrServer() {
	HttpSolrServer server = new HttpSolrServer(url);
	server.setMaxRetries(1); 
	server.setConnectionTimeout(5000); 
	server.setParser(new XMLResponseParser()); 
	server.setSoTimeout(1000);
	server.setDefaultMaxConnectionsPerHost(100);
	server.setMaxTotalConnections(100);
	server.setFollowRedirects(false); 
	server.setAllowCompression(true);
	return server;
}
public static void create() throws SolrServerException, IOException{
	HttpSolrServer server = getSolrServer();
	SolrInputDocument doc = new SolrInputDocument();
	doc.addField("id", System.currentTimeMillis());
	doc.addField("title", "60只蚊子写作文");
	server.add(doc);
	server.commit();
}
public static void search() throws SolrServerException{
	SolrServer server = getSolrServer();
	SolrQuery query = new SolrQuery();
	query.setQuery("*:*");
	QueryResponse rsp = server.query(query);
	SolrDocumentList docs = rsp.getResults();
	for (SolrDocument sd : docs) {
		System.out.println(sd.toString());
	}
}

猜你喜欢

转载自itace.iteye.com/blog/1881934