solr7.0.0使用ikanalyzer中文分词器

来源:互联网 发布:win7无损分区软件 编辑:程序博客网 时间:2024/06/06 04:19

1,下载ikanalyzer-solr6.5.zip ,地址:http://download.csdn.net/download/u014028634/10018052 ;

2, 解压ikanalyzer-solr6.5.zip ,把ext.dicIKAnalyzer.cfg.xmlstopword.dic 复制到Tomcat \webapps\solr\WEB-INF\classes中,把ik-analyzer-solr5-5.x.jarsolr-analyzer-ik-5.1.0.jar 复制到Tomcat \webapps\solr\WEB-INF\lib中;

3,修改 E:\solrhome\core1 下的  managed-schema文件,在</schema> 中加入配置:

<!-- IK分词器--> <!-- 此处不需要加 dynamicField -->
    <fieldType name="text_ik" class="solr.TextField">
        <analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer" />
        <analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>

<field name="content" type="text_ik" indexed="true" stored="true" required="true" multiValued="false" />


4,重启Tomcat,执行Java代码;


注:以上路径等部署情况见:http://blog.csdn.net/u014028634/article/details/78203558;


5,Java 测试类如下:

public class MySolr {    public static final String URL = "http://localhost:8080/solr";    public static final String SERVER = "core1";    public static String[] docs = {"Solr是一个独立的企业级搜索应用服务器",            "它对外提供类似于Web-service的API接口",            "用户可以通过http请求",            "向搜索引擎服务器提交一定格式的XML文件生成索引",            "也可以通过Http Get操作提出查找请求",            "并得到XML格式的返回结果",            "随便写点"};    public static SolrClient getSolrClient(){        System.out.println(URL +"/"+SERVER);        HttpSolrClient client = new HttpSolrClient.Builder(URL +"/"+SERVER).build();        return client;    }    public static void createIndex(){        SolrClient client  = getSolrClient();        int i = 0;        List<SolrInputDocument> docList = new ArrayList<>();        for(String str : docs){            SolrInputDocument doc  = new SolrInputDocument();            doc.addField("id",i++);            doc.addField("content",str);            docList.add(doc);        }        try{            client.add(docList);            client.commit();            //search(client);        }catch(SolrServerException e){            e.printStackTrace();        }catch(IOException e){            e.printStackTrace();        }    }    public static void search(){        SolrClient client = getSolrClient();        SolrQuery query = new SolrQuery();        query.setQuery("content:搜索");        QueryResponse response = null;        try{            response = client.query(query);            System.out.println(response.toString());            System.out.println();            SolrDocumentList docs = response.getResults();            System.out.println("文档个数:"+ docs.getNumFound());            System.out.println("查询时间:"+ response.getQTime());            for(SolrDocument doc :docs){                System.out.println("id:"+ doc.getFieldValue("id")+ "   content:"+ doc.getFieldValue("content"));            }        }catch(SolrServerException e){            e.printStackTrace();        }catch(IOException e){            e.printStackTrace();        }    }    public static void main(String[] args){        createIndex();        search();    }

原创粉丝点击