Solr问题: 一对多数据导入solr处理

----------------------------------------------------------------------------------------------------------------------==
-----== solr 一对多数据导入solr处理

前言:
    我们平时的业务中,很可能出现,一对多的情况,比如一个人有多个爱好,一个教练有多个特长,我们在筛选的时候,想通过一个条件筛选出所有有此条件的数据.
    例如 A有[1,2,3]属性,B有[2,5,8]属性,C有[2,9,8]属性,如果我想筛选2属性,那么A、B、C三个数据,应该都得筛选出来;如果筛选2,8属性,那么结果应该是B、C。
    这种情况,大多都会有三张表(我这里说的就是带关系表的这种情况),那么,我们该怎么写data-config.xml来方便数据导入呢?
    下面,我们就来看看,要怎么处理!

1、数据表情景

    -- 教练表
    mysql> select id,name,gender,top,area_city,area_street,createtime,updatetime from coach limit 5;
    +----+-----------+--------+-----+-----------+-------------+---------------------+---------------------+
    | id | name      | gender | top | area_city | area_street | createtime          | updatetime          |
    +----+-----------+--------+-----+-----------+-------------+---------------------+---------------------+
    |  1 | 王大伟    |      1 |   0 |         0 |           0 | 2019-01-13 15:44:32 | 2019-07-11 12:56:09 |
    |  2 | 李佳钊    |      1 |   0 |         0 |           0 | 2019-01-13 15:44:32 | 2019-07-17 13:51:49 |
    |  3 | 于春雷    |      1 |   0 |         0 |           0 | 2019-01-13 15:44:32 | 2019-07-11 12:56:09 |
    |  4 | 张咪      |      2 |   0 |         0 |           0 | 2019-01-13 15:44:32 | 2019-07-11 12:56:09 |
    | 11 | 陈云鹏    |      1 |   0 |         0 |           0 | 2019-01-13 15:44:32 | 2019-07-11 12:56:09 |
    +----+-----------+--------+-----+-----------+-------------+---------------------+---------------------+
    5 rows in set (0.00 sec)


    -- 特长表
    mysql> select * from specialty;
    +-------------+--------------------+------+---------+----------+--------+---------------------+
    | specialtyid | specialty_name     | type | user_id | coach_id | sortid | createtime          |
    +-------------+--------------------+------+---------+----------+--------+---------------------+
    |           1 | 减脂               |    2 |       0 |        0 |      1 | 2019-04-27 21:16:12 |
    |           2 | 塑形               |    2 |       0 |        0 |      2 | 2019-04-27 21:16:14 |
    |           3 | 增肌               |    2 |       0 |        0 |      3 | 2019-04-27 21:16:17 |
    |           4 | 体态调整           |    2 |       0 |        0 |      0 | 2019-04-26 21:58:13 |
    |           5 | 体能训练           |    2 |       0 |        0 |      0 | 2019-04-26 21:58:13 |
    +-------------+--------------------+------+---------+----------+--------+---------------------+
    15 rows in set (0.00 sec)

    -- 教练特长关系表
    mysql> select * from coach_specialty where coach_id in (1,2,3,4,11);
    +----+----------+--------------+---------------------+
    | id | coach_id | specialty_id | createtime          |
    +----+----------+--------------+---------------------+
    |  1 |        1 |            2 | 2019-01-13 15:44:32 |
    |  2 |        1 |            5 | 2019-01-13 15:44:32 |
    |  3 |        2 |            3 | 2019-01-13 15:44:32 |
    |  4 |        2 |            4 | 2019-01-13 15:44:32 |
    |  5 |        3 |            3 | 2019-01-13 15:44:32 |
    |  6 |        4 |            5 | 2019-01-13 15:44:32 |
    +----+----------+--------------+---------------------+
    6 rows in set (0.00 sec)

2、solr数据形式

    我们最终要的结果是,每个教练,将自己所有的特长都跟到自己的对应的数据中,以数组形式存储。

3、solr中sql该如何写

    这块是关键点,直接关乎solr中最终导入数据的格式,所以,一定要注意!关键操作如下:

    ---== data-config.xml文件

        <document>
           <entity name="coach" dataSource="source" pk="id"
                query="select id,name,head_img,certificate,score,cityid,gym_id,area_city,area_district,area_street,longitude,latitude,concat(latitude,',',longitude) as location,updatetime,gender,status,is_have_course from coach"
                deltaImportQuery="select id,name,cityid,gym_id,area_city,area_district,area_street,longitude,latitude,concat(latitude,',',longitude) as location,updatetime,gender,status,is_have_course from coach where id='${dataimporter.delta.id}'"
                deltaQuery="select id from coach where updatetime > '${dataimporter.last_index_time}'" >
                <field column="id" name="id" />
                <field column="name" name="name" />
                <field column="head_img" name="head_img" />
                <field column="certificate" name="certificate" />
                <field column="score" name="score" />
                <field column="cityid" name="cityid" />
                <field column="area_city" name="area_city" />
                <field column="area_district" name="area_district" />
                <field column="area_street" name="area_street" />
                <field column="longitude" name="longitude" />
                <field column="latitude" name="latitude" />
                <field column="location" name="location" />
                <field column="updatetime" name="updatetime" />
                <field column="gender" name="gender" />
                <field column="status" name="status" />
                <field column="is_have_course" name="is_have_course" />
                <entity name="coach_specialty"
                    query="select cs.specialty_id from coach_specialty cs  where cs.coach_id='${coach.id}'"
                    deltaQuery="select coach_id from coach_specialty where createtime > '${dataimporter.last_index_time}'"
                    parentDeltaQuery="select id from coach where id='${coach.id}'">
                    <field column="specialty_id" name="specialty_id" />【关键点1】
                    <entity name="specialty"
                    query="SELECT specialtyid, specialty_name FROM `specialty` WHERE specialtyid = '${coach_specialty.specialty_id}'" >
                            <field column="specialty_name" name="specialty_name" />【关键点2】
                    </entity>
                </entity>
            </entity>
        </document>

    ---== managed-schema 文件

        <field name="specialty_id" type="pint" indexed="true"  stored="true" multiValued="true" />
        <field name="specialty_name" type="string" indexed="true"  stored="true" multiValued="true" />

    注意:如果数据是数组格式的,这里一定记得加【multiValued="true"】这个属性,切记!

4、保存,重启solr,并对数据进行导入,我这里作的是clean,并且,full-import。

5、用solr控制台,query一下,最终结果

    {
      "responseHeader":{
        "status":0,
        "QTime":0,
        "params":{
          "q":"*:*",
          "_":"1565262090439"}},
      "response":{"numFound":2939,"start":0,"docs":[
          {
            "gender":1,
            "area_city":0,
            "area_street":[0],
            "latitude":"0E-7",
            "certificate":0,
            "cityid":201,
            "is_have_course":1,
            "area_district":0,
            "score":5.0,
            "name":"王大伟",
            "location":"0.0000000,0.0000000",
            "id":"1",
            "updatetime":"2019-07-11T12:56:09Z",
            "longitude":"0E-7",
            "status":2,
            "specialty_id":[2,
              5],
            "specialty_name":["塑形",
              "体能训练"],
            "_version_":1641298795321884672},
          {
            "gender":1,
            "area_city":0,
            "area_street":[0],
            "latitude":"0E-7",
            "certificate":1,
            "cityid":201,
            "is_have_course":1,
            "area_district":0,
            "score":5.0,
            "name":"李佳钊",
            "location":"0.0000000,0.0000000",
            "id":"2",
            "updatetime":"2019-07-17T13:51:49Z",
            "longitude":"0E-7",
            "status":1,
            "specialty_id":[3,
              4],
            "specialty_name":["增肌",
              "体态调整"],
            "_version_":1641298795521114112},
          {
            "gender":1,
            "area_city":0,
            "area_street":[0],
            "latitude":"0E-7",
            "certificate":2,
            "cityid":201,
            "is_have_course":1,
            "area_district":0,
            "score":5.0,
            "name":"于春雷",
            "location":"0.0000000,0.0000000",
            "id":"3",
            "updatetime":"2019-07-11T12:56:09Z",
            "longitude":"0E-7",
            "status":2,
            "specialty_id":[3],
            "specialty_name":["增肌"],
            "_version_":1641298795526356992}]
      }
    }

结语:
    到这里,就基本上完事了,剩下的就是你代码里怎么处理搜索条件的拼接了。
    目前solr问题,网上,并不是很多,所以,以分享精神,积极上贴,利己利人,这样solr会越用越顺利!

----------------------------------------------------------------------------------------------------------------------==
发布了59 篇原创文章 · 获赞 2 · 访问量 5595

猜你喜欢

转载自blog.csdn.net/LDR1109/article/details/98955892