solr6 tomcat8 windows-x64 服务端运行步骤讲解












一基本配置和安装:

windows7 ,64位

JDK1.8.0_31

solr-6.1.0

apache-tomcat-8.0.33-windows-x64

1、在D盘建立D:\solr文件夹

2、在刚创建的文件夹solr中创建两个文件夹起名home和server

3、copyE:\it\api\solr\solr-6.1.0\solr-6.1.0\server\solr下的文件到home文件夹中,这个及solr的相关服务

4、copyE:\it\api\solr\solr-6.1.0\solr-6.1.0\server\solr-webapp\下的文件到server文件夹中,并重命名为solr,这个及solr的应用

5、部署solr的应用到tomcat下:这里采用jndi的方式部署,其他发布方式请自行配置。

     在D:\meTools\java\apache-tomcat-8.0.36\conf\Catalina\localhost文件夹下建立solr.xml

     内容为
    <Context path="/solr" docBase="D:\solr\server\solr" reloadable="true" > 
<Environment name="solr/home" type="java.lang.String" value="D:\solr\home" override="true" />
</Context>

6、修改tomcat的conf/server.xml   内容:

<Context path="/solr" docBase="D:\solr\server\solr" reloadable="true" > 
<Environment name="solr/home" type="java.lang.String" value="D:\solr\home" override="true" />
</Context>

7、把solr-6.0.0\server\lib\ext包里的jar包拷贝到tomcat8的lib文件夹中

8、启动服务

9、访问路径为 http://localhost:8080/solr/index.html,不带index.html访问不到。

【注意】请使用jdk8和tomcat8,如果使用tomcat7会报错,之前在这栽过跟头。

中途遇到的问题:1、如果启动报错查看tomcat下的logs文件,出现SFL4类似的错误,是因为第7步没有导入jar包。

2、出现Version52 类似的错误是因为你的JDK版本不对,我用的是JDK1.8,solr6.0,用JDK1.7会报版本不对这个错。

=================以下基本的配置就已经配置完了===============================

二、core配置

1、copyD:\solr\home\configsets下的basic_configs文件夹到home下并修改文件夹名字为core1  

2、  在浏览器中,选择core admin,点击add core,输入一下下内容,instanceDir一定要写为上面一步中修改后的文件夹名。

solrconfig.xml 和  managed-schema 两个文件在 D:\solr\home\core1\conf 中

 

 ======权限管理这块暂时还未做测试==================

solr管理界面登录权限配置(

注:

权限配置 最好先别配置 等 测试代码连接到 solr 并成功返回后 再尝试配置

)

1、修改tomcat的tomcat-user.xml ,</tomcat-user>上面添加:

<role rolename="solr"/>

<user username="admin" password="admin" roles="solr"/>

2、  修改solr项目中的D:\projects\solr\server\webapps\solr\WEB-INF \web.xml, 在</web-app>上面添加:

  <security-constraint>

    <web-resource-collection>

           <web-resource-name>Solr Lockdown</web-resource-name>

           <url-pattern>/</url-pattern>

</web-resource-collection>

<auth-constraint>

           <role-name>solr</role-name>

           <role-name>admin</role-name>

</auth-constraint>

  </security-constraint>

  <login-config>

<auth-method>BASIC</auth-method>

<realm-name>Solr</realm-name>

  </login-config>

3、重新启动tomcat,再使用管理界面就必须要输入用户名/密码(admin/admin)了。

客户端访问权限控制

在D:\projects\solr\server\webapps下创建:communityserver_override.config,内容如下:

<?xml version="1.0" encoding="utf-8"?>

<Overrides>

<Override xpath="/CommunityServer/Search/Solr" mode="change" name="host" value="http://localhost:8080&quot; />

</Overrides>

value的值为浏览器中访问地址?待确认.

三、中文分词器

mmseg4j中文分词器

1、  从网上maven库下载最新版本: mmseg4j-core-1.10.0.jar; mmseg4j-solr-2.3.0.jar(中有这二个jar, mmseg4j-analysis-1.9.1.jar不能放,否则会报java.lang.NoSuchMethodError: com.chenlb.mmseg4j.analysis.MMSegTokenizer.<init>(Lcom/chenlb/mmseg4j/Seg;)V错误,因为mmseg4j-solr-2.3.0已经包含了mmseg4j-analysis)。放在D:\projects\solr\server\webapps\solr\WEB-INF\lib\下。

2、 配置schema.xml。

schema.xml的配置也挺简单,首先需要在schema.xml文件中配置一个fieldType节点,如下:

<!– mmseg4j –>
<fieldType name="text_zh" class="solr.TextField" positionIncrementGap="100">
<span style="white-space:pre"> </span><analyzer>
<span style="white-space:pre"> </span><tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" />
<span style="white-space:pre"> </span></analyzer>
</fieldType>

然后就可以在field节点中引用该filedType了,假设你有个字段叫keyWord需要支持中文分词,只需要定义示例filed节点如下:

<field name="keyWord" type="text_zh" indexed="true" stored="false" multiValued="true"/>

四、使用solrj操作solr

相关参考地址


以下是个人的测试代码:

package com.noteshare.common.solr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation;
import org.apache.solr.client.solrj.response.SpellCheckResponse.Correction;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestSolr {

// solr路径
String baseUrl = null;
HttpSolrClient.Builder solrBuilder = null;
HttpSolrClient solrClient = null;

@Before
public void start() {
    baseUrl = &quot;http://localhost:8080/solr/core1&quot;;
    solrBuilder = new HttpSolrClient.Builder(baseUrl);
    solrClient = solrBuilder.build();
}

/**
 * @throws IOException
 * @throws SolrServerException
 * @Description: 查询总数 
 * @ @author : 
 * Create Date : 2016年7月6日 下午3:29:22 
 * @throws
 */
@Test
public void search() throws SolrServerException, IOException {
    SolrQuery params = new SolrQuery();
    params.add(&quot;q&quot;, &quot;*:*&quot;);
    QueryResponse query = solrClient.query(params);
    SolrDocumentList results = query.getResults();
    long numFound = results.getNumFound();
    System.out.println(&quot;总数:&quot; + numFound);
    for (SolrDocument solrDocument : results) {
        // 打印
        Collection&lt;String&gt; solrDocuments = solrDocument.getFieldNames();
        for (String field : solrDocuments) {
            System.out.println(field + &quot;:&quot; + solrDocument.get(field));
        }
    }
}

/**
 * @throws IOException
 * @throws SolrServerException
 * @Description: 查询并高亮显示
 * @ @author : 
 * Create Date : 2016年7月6日 下午4:49:17 @throws
 */
@Test
public void search01() throws SolrServerException, IOException {
    SolrQuery params = new SolrQuery();
    params.setQuery(&quot;测试&quot;);
    params.setHighlight(true).setHighlightSnippets(1); // set other params
    params.setParam(&quot;hl.fl&quot;, &quot;title&quot;);
    QueryResponse query = solrClient.query(params);
    SolrDocumentList results = query.getResults();
    long numFound = results.getNumFound();
    System.out.println(&quot;总数:&quot; + numFound);
    //显示高亮部分
    for (SolrDocument solrDocument : results) {
        // id is the
        // uniqueKey
        // field
        String id = (String) solrDocument.getFieldValue(&quot;id&quot;);
        if (query.getHighlighting().get(id) != null) {
            List&lt;String&gt; highlightSnippets = query.getHighlighting().get(id).get(&quot;title&quot;);
            for (String string : highlightSnippets) {
                System.out.println(&quot;高亮:--&quot; + string);
            }
        }
    }
}
/**
 * @Title           : searchByHighlight 
 * @Description     : 高亮查询结果中的查询字段---这种高亮方式有个问题,那就是必须存储,这个还需要测试。
 * @throws SolrServerException
 * @throws IOException          : void
 * @author          : xingchen
 * @date            : 2016年7月13日 下午10:37:01 
 * @throws
 */
@Test
public void searchByHighlight() throws SolrServerException, IOException {
    SolrQuery params = new SolrQuery();
    params.setQuery(&quot;测试&quot;);
    //开启高亮

// params.setHighlight(true).setHighlightSnippets(1); // set other params
params.setHighlight(true); // set other params
//设置高亮前缀和后缀
params.setHighlightSimplePre("<span class=’highligter’>").setHighlightSimplePost("</span>").setStart(0).setRows(5);
//设置高亮区域及过滤区域
params.setParam("hl.fl", "title,summary");
QueryResponse query = solrClient.query(params);
SolrDocumentList results = query.getResults();
long numFound = results.getNumFound();
System.out.println("总数:" + numFound);
//显示高亮部分
for (SolrDocument solrDocument : results) {
System.out.println("=============================打印高亮字段格式===========================");
// id is the
// uniqueKey
// field
String id = (String) solrDocument.getFieldValue("id");
if (query.getHighlighting().get(id) != null) {
System.out.println(query.getHighlighting().get(id));
System.out.println(query.getHighlighting().get(id).get("title"));
System.out.println(query.getHighlighting().get(id).get("summary"));
System.out.println("===");
List<String> highlightSnippets = query.getHighlighting().get(id).get("title");
for (String string : highlightSnippets) {
System.out.println("高亮:–" + string);
}
}
}
}

/**
 * @throws IOException
 * @throws SolrServerException
 * @Description: 拼写检查spellcheck
 *               设置查询的handler,修改为/spell,获取查询结果,如果查询结果为0,则说明没有查询到结果,
 *               可能就是输入的查询关键字有错误,
 *               这样就可以根据getspellcheckresponse方法获取拼写检查的response,
 *               根据这个response的一些方法就可以获取到建议的结果。
 *               可以根据下面的方式获取建议的结果。在这是从collation中获取结果。
 *               或者根据下面的方法获取建议结果。在这是从suggestion中获取结果。 [此方法测试不通过] @ 
 * @author :  Create Date : 2016年7月6日 下午4:21:25 
 * @throws
 */
@Test
public void search02() throws SolrServerException, IOException {
    SolrQuery params = new SolrQuery();
    params.set(&quot;qt&quot;, &quot;/spell&quot;);
    params.set(&quot;q&quot;, &quot;text:sumsung&quot;);
    QueryResponse queryResponse = solrClient.query(params);
    SolrDocumentList results = queryResponse.getResults();
    long numFound = results.getNumFound();
    if (0 == numFound) {
        System.out.println(&quot;拼写错误!&quot;);
        SpellCheckResponse spellCheckResponse = queryResponse.getSpellCheckResponse();
        // 方式01
        List&lt;Collation&gt; collatedResults = spellCheckResponse.getCollatedResults();
        for (Collation collation : collatedResults) {
            long numberOfHits = collation.getNumberOfHits();
            System.out.println(&quot;推荐的词语个数:&quot; + numberOfHits);
            List&lt;Correction&gt; misspellingsAndCorrections = collation.getMisspellingsAndCorrections();
            for (Correction correction : misspellingsAndCorrections) {
                System.out.println(&quot;原始:&quot; + correction.getOriginal() + &quot;推荐:&quot; + correction.getCorrection());
            }
        }
        // 方式02--从suggestion中获取结果。
        // List&lt;Suggestion&gt; suggestions =
        // spellCheckResponse.getSuggestions();
        // for (Suggestion suggestion : suggestions) {
        // int numFound2 = suggestion.getNumFound();
        // System.out.println(&quot;推荐的词语个数:&quot; + numFound2);
        // List&lt;String&gt; suggestions2 = suggestion.getAlternatives();
        // for (String string : suggestions2) {
        // System.out.println(string);
        // }
        // }
    } else {
        System.out.println(&quot;一共:&quot; + numFound);
        for (SolrDocument solrDocument : results) {
            Collection&lt;String&gt; fieldNames = solrDocument.getFieldNames();
            for (String field : fieldNames) {
                System.out.println(field + &quot;:&quot; + solrDocument.get(field));
            }
        }
    }
}
/**
 * @Title           : searchByProperties 
 * @Description     : 指定属性进行搜索
 * @throws SolrServerException
 * @throws IOException 
 * @author          : xingchen
 * @date            : 2016年7月10日 下午2:06:40 
 * @throws
 */
@Test
public void searchByProperties() throws SolrServerException, IOException{
    SolrQuery query = new SolrQuery();
    query.setQuery(&quot;title:测试&quot;);
    QueryResponse queryResponse = solrClient.query(query);
    SolrDocumentList results = queryResponse.getResults();
    long numFound = results.getNumFound();
    System.out.println(&quot;总数:&quot; + numFound);
    for (SolrDocument solrDocument : results) {
        // 打印
        Collection&lt;String&gt; solrDocuments = solrDocument.getFieldNames();
        for (String field : solrDocuments) {
            System.out.println(field + &quot;:&quot; + solrDocument.get(field));
        }
    }
}
/**
 * @Title           : searchByPage 
 * @Description     : 分页查询
 * 基于SolrDocumentList的查询和基于bean的查询两种方式
 * @throws SolrServerException
 * @throws IOException          : void
 * @author          : xingchen
 * @date            : 2016年7月13日 下午10:13:55 
 * @throws
 */
@Test
public void searchByPage() throws SolrServerException, IOException{
    SolrQuery query = new SolrQuery();
    query.setQuery(&quot;title:测试&quot;);
    query.setStart(2);
    query.setRows(5);
    QueryResponse queryResponse = solrClient.query(query);

    //分页结果集1---此种方法遍历拿不到总数。
    List&lt;Entity&gt; entitys = queryResponse.getBeans(Entity.class);
    //这个不能获取到总数
    System.out.println(entitys.size());
    System.out.println(&quot;=======================================&quot;);
    //分页结果集2
    SolrDocumentList results = queryResponse.getResults();
    long numFound = results.getNumFound();
    System.out.println(&quot;总数:&quot; + numFound);
    for (SolrDocument solrDocument : results) {
        // 打印
        Collection&lt;String&gt; solrDocuments = solrDocument.getFieldNames();
        for (String field : solrDocuments) {
            System.out.println(field + &quot;:&quot; + solrDocument.get(field));
        }
    }
}

/**
 * @throws IOException
 * @throws SolrServerException
 * @Description: 普通方式添加索引
 * @throws IOException 
 * @author : 
 * Create Date :2016年7月6日 下午3:48:18 
 * @throws
 */
@Test
public void add1() throws SolrServerException, IOException {
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField(&quot;id&quot;, &quot;1&quot;);
    doc.setField(&quot;title&quot;, &quot;测试01&quot;);
    doc.setField(&quot;summary&quot;, &quot;地址概要:深圳市南山区比克大厦14楼&quot;);
    doc.setField(&quot;text&quot;, &quot;此人可爱活泼,有大智慧。&quot;);
    solrClient.add(doc);
    solrClient.commit();
}

/**
 * @throws SolrServerException
 * @throws IOException
 * @Description: 通过注解的方式来添加索引 
 * @throws IOException
 * @author :  
 * Create Date : 2016年7月6日 下午3:57:17 
 * @throws
 */
@Test
public void add2() throws IOException, SolrServerException {
    Entity entity = new Entity();
    entity.setId(&quot;2&quot;);
    entity.setSummary(&quot;测试00002-summary&quot;);
    entity.setText(&quot;测试000002-text&quot;);
    entity.setTitle(&quot;测试00002-title&quot;);
    solrClient.addBean(entity);
    solrClient.commit();
}

/**
 * @Title           : addByBeanList 
 * @Description     : 基于bean的list进行添加
 * @throws SolrServerException
 * @throws IOException          : void
 * @author          : xingchen
 * @date            : 2016年7月13日 下午10:00:10 
 * @throws
 */
@Test
public void addByBeanList() throws SolrServerException, IOException{
    List&lt;Entity&gt; entityList = new ArrayList&lt;Entity&gt;();
    for(int i = 10;i &lt; 20;i++){
        Entity entity = new Entity();
        entity.setId(&quot;&quot; + i);
        entity.setSummary(&quot;测试00002-summary&quot;);
        entity.setText(&quot;测试000002-text&quot;);
        entity.setTitle(&quot;测试00002-title&quot;);
        entityList.add(entity);
    }
    solrClient.addBeans(entityList);
    solrClient.commit();
}



/**
 * @Title           : addByList 
 * @Description     : 批量添加索引
 * @throws SolrServerException
 * @throws IOException
 * @author          : xingchen
 * @date            : 2016年7月10日 下午2:01:11 
 * @throws
 */
@Test
public void addByList() throws SolrServerException, IOException {
    List&lt;SolrInputDocument&gt; docs = new ArrayList&lt;SolrInputDocument&gt;();
    // 待索引、查询字段 http://localhost:8080/solr53/#/connection1
    String[] contents = { &quot;Solr是一个独立的企业级搜索应用服务器&quot;,
            &quot;它对外提供类似于Web-service的API接口&quot;,
            &quot;用户可以通过http请求&quot;,
            &quot;向搜索引擎服务器提交一定格式的XML文件生成索引&quot;,
            &quot;也可以通过Http Get操作提出查找请求&quot;,
            &quot;并得到XML格式的返回结果&quot; };
    int i = 0;
    for (String title : contents) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField(&quot;id&quot;, &quot;id&quot; + i);
        i++;
        doc.addField(&quot;title&quot;, title);
        docs.add(doc);
    }
    solrClient.add(docs);
    solrClient.commit();
}

/**
 * @throws IOException
 * @throws SolrServerException
 * @Description: 根据id删除索引 
 * @throws IOException 
 * @author : 
 * Create Date : 2016年7月6日 下午4:04:17 
 * @throws
 */
@Test
public void delete() throws SolrServerException, IOException {
    solrClient.deleteById(&quot;1&quot;);
    solrClient.commit();
}

@After
public void end() throws SolrServerException, IOException {
    System.out.println(&quot;-----------执行search方法------------&quot;);
    search();
    solrClient = null;
    // 强制调用已经失去引用的对象的finalize方法
    System.runFinalization();
    // 告诉垃圾收集器打算进行垃圾收集,而垃圾收集器进不进行收集是不确定的
    System.gc();
}

}

package com.noteshare.common.solr;

import org.apache.solr.client.solrj.beans.Field;

public class Entity {
@Field
private String id;
@Field
private String title;
@Field
private String summary;
@Field
private String text;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getSummary() {
    return summary;
}

public void setSummary(String summary) {
    this.summary = summary;
}

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

}

 注: 如果配置了 访问权限  下面在连接的时候 会报错 

  如果想要在有用户名密码的环境下连接使用  则以下代码在 连接的时候  需要添加  http的用户名密码 验证(具体添加方法 自行网络搜索 )

  另:安全配置除了 添加用户名密码 外 还需要 绑定  允许访问的ip地址 为localhost 127.0.0.1 或者服务器 真实Ip地址  防止 被恶意攻击

  绑定方法 自行 网络搜索

      问题解决记录

  如果solr启动过程中包少了类,请查看tomcat下是否有以下几个包,如果没有的话下载这几个包放到solr的WEB-INF的lib下,

之所以放在此处而不是放在tomcat的lib下是因为怕其他系统依赖的并不是这个版本的jar,以免引起冲突。


 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值