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

        
           
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                    【关键点1】
                    
                            【关键点2】
                    
                
            
        

    ---== managed-schema 文件

        
        

    注意:如果数据是数组格式的,这里一定记得加【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会越用越顺利!

----------------------------------------------------------------------------------------------------------------------==

你可能感兴趣的:(Linux)