zoukankan      html  css  js  c++  java
  • Solr4.4入门,介绍Solr的安装、IK分词器的配置及高亮查询结果(转)

    一、Windows下安装solr-4.4.0

    1、  下载solr.4.4

    2、  下载绿色版tomcat6.0.18

    3、  解压下载的solrd:studysolr,将dist目录下的solr4.4.0.war包复制到tomcatwebapps

    4、  web.xml中配置solr.home为解压的 solr中的d:studysolrexamplesolr

    <env-entry>

           <env-entry-name>solr/home</env-entry-name>

           <env-entry-value>D:studysolrsolr-4.4.0examplesolr</env-entry-value>

           <env-entry-type>java.lang.String</env-entry-type>

    </env-entry>

    如果不做此配置,在启动的时候将会报异常 

    solr - org.apache.solr.common.SolrException: Could not load config for solrconfig.xml

    5、  启动tomcat,访问http://localhost:8080/solr-4.4.0即可访问

    6、  在左侧树中,选择collection1,会弹出节点菜单,其中documents功能表示往索引目录中添加记录,而Query可以查询指定目录。

    7、  往服务中添加索引

    String url = "http://localhost:8080/solr-4.4.0";

           SolrServer server = new HttpSolrServer(url);

           SolrInputDocument doc1 = new SolrInputDocument();

           doc1.addField("id""1");

           doc1.addField("title""信息科技");

        doc1.addField("content""企业信息门户,元数据,数字沙盘,知识管理");

    server.add(docs);

        server.commit();

    8、  查询

    public static void main(String[] args) {

            String url = "http://localhost:8080/solr-4.4.0";

            SolrServer server = new HttpSolrServer(url);

            SolrQuery query = new SolrQuery("云南");

            query.setFacetLimit(1);

            //以下两个参数常用作分页时使用

            query.setRows(1);// 设置每次取多少条

            query.setStart(0);//设置从第几条开始查询

    //以下几行设置查询结果关键字高亮显示

            query.setHighlight(true);

            // hl.fl参数表示哪个几Field关键字段高亮

            query.setParam("hl.fl""title, content");

    query.setHighlightSimplePre("<font color=red>"); query.setHighlightSimplePost("</font");                                    query.setSort("name",ORDER.desc);

            try {

                QueryResponse response = server.query(query);

                SolrDocumentList docs = response.getResults();

                System.out.println("文档个数:" + docs.getNumFound());

                System.out.println("查询时间:" + response.getQTime());

                for (SolrDocument doc : docs) {

                   System.out.println("title: " + doc.getFieldValue("title"));

     

                    System.out.println("content : " + doc.getFieldValue("content "));

                 }

            } catch (Exception e) {

                e.printStackTrace();

            }

        }

     二、在solr中配置IK中文分词器

    在目录solr/example/solr/collection1/conf中的 secham.xmltypes标签最后添加定义字段类型。

    secham.xml文件是定义的类型以及存储的域。

    <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>

    field中指定字段类型为上面定义的

    <field name="name" type="text_ik" indexed="true" stored="true"/>

    设置完比后重启服务

    admin管理台中,由于nameIK分词器,所以分词后的效果如下:

    Solr4.4入门,介绍Solr的安装、IK分词器的配置及高亮查询结果(一) - hbin8668 - HBIN8668IYUEE
     

     

    title域是用的默认的分词器,即text_general,分词效果如下:

    Solr4.4入门,介绍Solr的安装、IK分词器的配置及高亮查询结果(一) - hbin8668 - HBIN8668IYUEE
    这说明IK分词器配置成功
  • 相关阅读:
    redis常用方法
    分享朋友圈、qq等思路及代码
    redis 使用例子
    redis使用实例应用
    js对象与jquery对象介绍
    h5网页跳转到小程序
    redis队列思路介绍
    redis队列思路分析
    php原生方法连接mysql数据库
    mysql 原生语句limit 分页
  • 原文地址:https://www.cnblogs.com/rainbowzc/p/3680029.html
Copyright © 2011-2022 走看看