1.1 solr 服务器 部署在windows/linux环境

来源:互联网 发布:gta5需网络才能启动 编辑:程序博客网 时间:2024/06/06 01:55

资源地址:http://download.csdn.net/detail/wwwyuanliang10000/4192482

软件版本:
apache-tomcat-6.0.35.tar.gz 下载地址为: http://tomcat.apache.org/download-70.cgi

apache-solr -3.2.0.tgz  下载地址为:    http://apache.etoak.com/lucene/solr/3.2.0/

1.安装tomcat:

 1.1 将apache-tomcat-6.0.35.tar.gz拷贝到路径/usr下后,

  先后执行命令:# cd /usr

 # tar  -zxvf  apache-tomcat-6.0.35.tar.gz

# mv  apache-tomcat-6.0.35   tomcat


 1.2 将apache-solr-3.2.0.tgz 拷贝到路径/usr下后

  执行命令:#tar -zxvf apache-solr-3.2.0.tgz

  进入apache-solr-3.2.0\dist目录:

   #cd  /usr/apache-solr-3.2.0/dist 

  将里面的solr.war放到安装好的tomcat/webapps下:

   #mv  apache-solr-3.2.0.war  /usr/tomcat/webapps/solr.war  

 

2. 

 2.1启动tomcat会自动解压solr.war:

  #cd /usr/tomcat/bin

  #./startup.sh

 2.2  设置context指向你的solr工程:

 在tomcat/conf/Catalina/localhost/目录下创建solr.xml文件

  #cd /usr/tomcat/conf

  #mkdir -p  Catalina/localhost

  #cd Catalina/localhost

  #vi solr.xml

 该文件内容如下:

  <Context docBase="/usr/tomcat/webapps/solr.war"     

debug="0"  crossContext="true" >

     <Environment name="solr/home" type="java.lang.String" 

        value="/usr/solr" override="true" />

</Context>

在上面value的值路径/usr下建立文件夹solr,

#cd /usr

#mkdir solr

(主要是配置文件路径 :可能会出错 can't find resource 'solrconfig.xml' 路径配置不正确)

---------------------------------------------------------------------------------------------


3. 我们需要将一些配置和index库文件放到文件夹/usr/solr下。解压的apache-solr-3.2.0\example\solr目录 下,

#cd /usr/apache-solr-3.2.0/example/solr

将里面的conf目录和solr.xml文件copy到刚才我们建的文件夹/usr/solr目录下,

#cp -rf conf /usr/solr

#cp  solr.xml  /usr/solr

重启tomcat就ok了。 



1.2 solr 和web容器 集合

  1.2.1 在工程目录中 web.xml中配置

 

 <filter>    <filter-name>SolrRequestFilter</filter-name>    <filter-class>org.apache.solr.servlet.SolrDispatchFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>SolrRequestFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>     <env-entry>      <env-entry-name>solr/home</env-entry-name>      <env-entry-value>D:/Tomcat 6.0/webapps/mysolr/conf/multicore</env-entry-value>      <env-entry-type>java.lang.String</env-entry-type>   </env-entry>
 1.2.2 solrj的使用,通过CommonsHttpSolrServer连接多core实例,对其添加删除索引

public static final String url = "http://localhost:28080/mysolr/core0";static CommonsHttpSolrServer server;public void init(){try {server = new CommonsHttpSolrServer(url);server.setSoTimeout(1000);server.setConnectionTimeout(100);server.setDefaultMaxConnectionsPerHost(100);server.setMaxTotalConnections(100);server.setFollowRedirects(false);server.setAllowCompression(true);server.setMaxRetries(1);} catch (MalformedURLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

1.2.3 创建索引 和删除索引,具体需要修改D:\Tomcat 6.0\webapps\mysolr\conf\multicore\core0\conf 下面的schema.xml文件  

<field name="msg_title" type="textComplex" indexed="true" stored="true" />   <field name="msg_content" type="textComplex" indexed="true" stored="true" />   <field name="msg_all" type="textComplex" indexed="true" stored="false" multiValued="true"/>  <!-- field for the QueryParser to use when an explicit fieldname is absent --> <defaultSearchField>msg_all</defaultSearchField>  

@Testpublic void test01() throws SolrServerException, IOException{init();server.deleteByQuery("*:*");//删除所有的索引server.commit();SolrInputDocument doc = new SolrInputDocument();doc.addField("id", "1");doc.addField("msg_title", "这是我的第一个solrj程序");doc.addField("msg_content", "我的solrj的程序究竟能不能跑的起来呢?");server.add(doc);server.commit();}


  









原创粉丝点击