solr配置域及其应用——向索引库添加数据,读取索引库数据。

一、solr域的介绍

solrhome就是存放数据的地方,里面的每个Core相当于一个数据库,Core里面有三个东西(conf、data、.properties),conf下有一个schema.xml或managed-schema文件在其里面配置域,solr旧版本在schema.xml配置,新版本在managed-schema配置。

相当于数据库的表字段,用户存放数据,因此用户根据业务需要去定义相关的Field(域),一般来说,每一种对应着一种数据,用户对同一种数据进行相同的操作。

域的常用属性:

  1. name:指定域的名称
  2. type:指定域的类型
  3. indexed:是否索引
  4. stored:是否存储到solrhome
  5. required:是否必须
  6. multiValued:是否多值

 二、solr域的配置

比如我们有这样一个表:

打开solrhome中Core下的conf里面schema.xml或managed-schema文件在其里面配置域,solr旧版本在schema.xml配置,新版本8.9.0在managed-schema配置。

在solr新版本中type中的值比如int,double,date...要在其前面加p,如pint,pdouble,pdate,不然启动tomcat会报错,这些类型可以在你启动tomcat后浏览器显示的页面看到。

text_ik是我自己添加的中文分词器,添加中文分词器方式链接在文末。

将下面代码复制到schema.xml/managed-schema的末尾(</schema>里面),重启tomcat即可。

    <!--基本域  对应book表-->
    <field name="book_id" type="pint" indexed="true" stored="true"/>
    <field name="book_title" type="text_ik" indexed="true" stored="true"/>
    <field name="book_author" type="text_ik" indexed="true" stored="true"/>
    <field name="book_price" type="pdouble" indexed="true" stored="true"/>
    <field name="book_describe" type="text_ik" indexed="true" stored="true"/>
    <field name="book_count" type="text_ik" indexed="true" stored="true"/>
    <field name="book_time" type="pdate" indexed="true" stored="true"/>
    
    <!--将某一个域中的数据复制到另一个域中-->
    <field name="book_keywords" type="text_ik" indexed="true" stored="true" multiValued="true"/>
    <copyField source="book_title" dest="book_keywords"/>
    <copyField source="book_author" dest="book_keywords"/>
    <copyField source="book_describe" dest="book_keywords"/>
    
    <!--动态域 special下有尺寸和版本 其值是动态不确定的-->
    <dynamicField name="book_special_*" type="string" indexed="true" stored="true"/>
    

 三、solr域的使用

向索引库中添加数据和读取索引库中的数据。

1.创建maven项目,当然普通Java项目也可以(maven项目导入jar包方便点,不然有一堆jar包万一不小心就少了或者冲突,排查起来非常麻烦),我这里是在Springboot的测试类里面写的。

2.添加依赖。solr的jar包版本不同写法不同,基本以版本5为分界线。

        <!-- solr -->
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>8.9.0</version>
        </dependency>

        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>        

3.向索引库添加数据

@Test
    public void setData() throws IOException, SolrServerException {
        //参数:solr服务器的地址
        //SolrServer solrServer=new HttpSolrServer("http://localhost:8087/solr");//旧版本jar写法,新版本已经弃用

        //connection1为你solr Core的名字,我这里为connection1,在solrhome里面可以看到你的Core文件夹名字,新版本jar必须加Core
        SolrClient solrClient=new HttpSolrClient.Builder("http://localhost:8087/solr/connection1").build();

        SolrInputDocument inputDocument = new SolrInputDocument();

        inputDocument.addField("id",4);
        inputDocument.addField("book_price",45.6);
        inputDocument.addField("book_describe","了特乐特认同了");
        inputDocument.addField("book_special_size","小");

        //solrClient.add(inputDocument);
        solrClient.add(inputDocument);
        solrClient.commit();

        System.out.println("添加成功!");
    }

 

 4.读取索引库中的数据。

   @Test
    public void getData() throws IOException, SolrServerException {
        //参数:solr服务器的地址
        //SolrServer solrServer=new HttpSolrServer("http://localhost:8087/solr");//旧版本jar写法,新版本已经弃用

        //connection1为你solr Core的名字,我这里为connection1,在solrhome里面可以看到你的Core文件夹名字,新版本jar必须加Core
        SolrClient solrClient=new HttpSolrClient.Builder("http://localhost:8087/solr/connection1").build();

        //查询条件--参数说明查看下一步骤No.5
        SolrQuery query = new SolrQuery();
        query.set("q","*:*");//查询所有
        query.set("sort","id desc");//排序查询-根据id从大到小显示

        //分页-从显示索引为0-10的数据
        query.setStart(0);
        query.setRows(10);

        QueryResponse queryResponse = solrClient.query(query);

        SolrDocumentList list = queryResponse.getResults();

        if (list!=null) {
            for (SolrDocument s :list) {
                System.out.println("id:"+s.get("id"));
                System.out.println("book_price:"+s.get("book_price"));
                System.out.println("book_describe:"+s.get("book_describe"));
                System.out.println("book_special_size:"+s.get("book_special_size"));
                System.out.println("------------------------------");
            }
        }

    }

 

 5.query 参数说明及客户端常见操作

 附:

solr8.9.0安装配置:https://www.cnblogs.com/smiles365/articles/15269607.html

solr配置中文分词器IKAnalyzer:https://www.cnblogs.com/smiles365/articles/15271630.html

posted @ 2021-09-16 21:33  蒂雪凌星  阅读(468)  评论(0)    收藏  举报
Live2D