solr

来源:互联网 发布:icam自动编程破解版 编辑:程序博客网 时间:2024/06/06 00:32

1 Solr 介绍

2 安装及配置

2.1 下载

Solrlucene.gz

2.2 运行环境

solr需要运行在一个servlet容器中,solr默认使用jetty,这里我使用tomcatsolr4 jdk1.7 tomcat7

3 Solr整合Tomcati

3.1 Solr Home和SolrCore

创建一个solr home目录,这是一个solr运行的主目录,包括solr实例所有的配置文件和数据文件,solr实例就是solrcore一个solr home可以包括多个solrcore实例,每个实例对应一个索引目录,提供单独的搜索和索引服务example/solr就是一个solr home目录结构example/solr/collection1就是一个solrcore实例(可复制多份,多个实例)    ----conf 配置文件目录    ----data 存放索引文件,需要创建

3.2 整合步骤

1.安装tomcat2.导入solr的war包    把solr安装目录中的solr/dist/solr.war包移动到tomcat的webapp下,运行tomcat后会自动解压3.导入solr的依赖包    把solr安装目录下的example/lib/ext目录下的所有jar包添加到solr工程中的lib目录4.配置solrhome和solrcore    a.创建一个solrhome目录(复制example/solr)    b.可以建立多个collection        每一个collection是一个solrcore,相当于mysql的一个数据库,多个solrcore间是相互隔离的    c.修改solrcore配置        打开conf/solrconfig.xml            lib 服务以来的扩展包,默认collection/lin            dataDir 索引库存放的位置,默认collection/data            requestHandler 请求处理器,配置查询维护等功能使用的类6.告诉solr服务器solrhome的位置.修改solr工程的web.xml文件    <env-entry>        <env-entry-name>solr/home</env-entry>        <env-entry-value>/usr/zhuo/solrhome</env-entry>        <env-entry-type>java.lang.String</env-entry>    </env-entry>7.启动tomcat

4 配置中文分析器

4.1 schema.xml

位于solrcore的conf目录下,是数据配置文件,定义了索引数据的数据类型

4.2 标签及属性介绍

4.3 安装中文分词器

1.把IKAnalyzer2012FF_u1.jar添加到solr工程下的lib目录2.复制IKAnalyzer的配置文件和自定义词典和停用词典到solr工程的classpath下3.在schemal.xml中添加一个自定义的fieldType,使用中文分词器
    <fieldType name="text_ik" class="solr.TextField">      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>    </fieldType>
4.定义field
    <field name="title_ik" type="text_ik" indexed="true" stored="true" />    <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/>
5.重启tomcat

4.4 设置业务系统的Field

可以根据自己的需求,自定义一套field,例如:
<!--product-->   <field name="product_name" type="text_ik" indexed="true" stored="true"/>   <field name="product_price"  type="float" indexed="true" stored="true"/>   <field name="product_description" type="text_ik" indexed="true" stored="false" />   <field name="product_picture" type="string" indexed="false" stored="true" />   <field name="product_catalog_name" type="string" indexed="true" stored="true" />   <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>   <copyField source="product_name" dest="product_keywords"/>   <copyField source="product_description" dest="product_keywords"/>

5 使用SolrJ管理索引库

5.1 导包

solrj-lib文件夹(example/lib/ext)solr-solrj-4.10.3.jar

5.2 实现步骤

1.导包2.和Solr服务器创立连接,HttpSolrServer对象3.创建SolrInputDocument对象,添加域4.将SolrInputDocument对象添加到索引库5.提交

5.3 向索引库添加索引

    @Test    public void addDocument() throws Exception {        //和solr服务器创建连接        //参数:solr服务器的地址        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");        //创建一个文档对象        SolrInputDocument document = new SolrInputDocument();        //向文档中添加域        //第一个参数:域的名称,域的名称必须是在schema.xml中定义的        //第二个参数:域的值        document.addField("id", "c0001");        document.addField("title_ik", "使用solrJ添加的文档");        document.addField("content_ik", "文档的内容");        document.addField("product_name", "商品名称");        //把document对象添加到索引库中        solrServer.add(document);        //提交修改        solrServer.commit();    }

5.2 删除文档

    <!--根据id-->    public void deleteDocumentByid() throws Exception {        //创建连接        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");        //根据id删除文档        solrServer.deleteById("c0001");        //提交修改        solrServer.commit();    }    <!--根据查询-->    @Test    public void deleteDocumentByQuery() throws Exception {        //创建连接        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");        //根据查询条件删除文档        solrServer.deleteByQuery("*:*");        //提交修改        solrServer.commit();    }

5.2 修改文档

使用add方法,使用相同的id,就能进行修改了

5.2 查询-简单查询

@Test    public void queryIndex() throws Exception {        //创建连接        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");        //创建一个query对象        SolrQuery query = new SolrQuery();        //设置查询条件        query.setQuery("*:*");        //执行查询        QueryResponse queryResponse = solrServer.query(query);        //取查询结果        SolrDocumentList solrDocumentList = queryResponse.getResults();        //共查询到商品数量        System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());        //遍历查询的结果        for (SolrDocument solrDocument : solrDocumentList) {            System.out.println(solrDocument.get("id"));                 }    }

5.2 查询-复杂查询

@Test    public void queryIndex2() throws Exception {        //创建连接        SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");        //创建一个query对象        SolrQuery query = new SolrQuery();        //设置查询条件        query.setQuery("钻石");        //过滤条件        query.setFilterQueries("product_catalog_name:幽默杂货");        //排序条件        query.setSort("product_price", ORDER.asc);        //分页处理        query.setStart(0);        query.setRows(10);        //结果中域的列表        query.setFields("id","product_name","product_price","product_catalog_name","product_picture");        //设置默认搜索域        query.set("df", "product_keywords");        //高亮显示        query.setHighlight(true);        //高亮显示的域        query.addHighlightField("product_name");        //高亮显示的前缀        query.setHighlightSimplePre("<em>");        //高亮显示的后缀        query.setHighlightSimplePost("</em>");        //执行查询        QueryResponse queryResponse = solrServer.query(query);        //取查询结果        SolrDocumentList solrDocumentList = queryResponse.getResults();        //共查询到商品数量        System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());        //遍历查询的结果        for (SolrDocument solrDocument : solrDocumentList) {            System.out.println(solrDocument.get("id"));            //取高亮显示            String productName = "";            Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();            List<String> list = highlighting.get(solrDocument.get("id")).get("product_name");            //判断是否有高亮内容            if (null != list) {                productName = list.get(0);            } else {                productName = (String) solrDocument.get("product_name");            }                       System.out.println(productName);        }    }
这里写代码片

1 Solr 介绍

1 Solr 介绍

1 Solr 介绍

0 0
原创粉丝点击