solr对嵌套(nesting)结构的支持

最近由于业务需求一直在弄json数据导入solr的相关测试,由于需要导入的数据有分层情况,也就是有相互嵌套的情况,lucene底层对于嵌套本身是不支持的,但是solr对其做了优化。
首先是属性组的展示,在mongodb使用mongo-connector导入solr时对于属性组就不能直接导入,需要对结构进行调整,例如如下属性组:
"subdoc": {
"a": 1,        
"b": 2,        
"c": 3,        
"array": [            
{"name": "elmo"},            
{"name": "oscar"}        
]    
}  
需要调整为:
{
"subdoc.a": 1,    
"subdoc.b": 2,    
"subdoc.c": 3,    
"subdoc.array.0.name": "elmo",    
"subdoc.array.1.name": "oscar"
}
才能够正确的导入到solr中建立索引,好在solr有动态域可以将所有后缀为name的字段统一存储到动态域中,但是这就导致不能精确检索问题。
同样对于嵌套的json文件如下:
{id : book1, type_s:book, title_t : "The Way of Kings", author_s : "Brandon Sanderson",
  cat_s:fantasy, pubyear_i:2010, publisher_s:Tor,
  array: [
    { id: book1_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z",
      stars_i:5, author_s:yonik,
      comment_t:"A great start to what looks like an epic series!"
    }
    ,
    { id: book1_c2, type_s:review, review_dt:"2014-03-15T12:00:00Z",
      stars_i:3, author_s:dan,
      comment_t:"This book was too long."
    }
  ]
 }
也是需要将结构进行转化,将array字段替换为solr特定字段_childDocuments_,solr才能识别到嵌套的关系,修改后的结构如下:
{id : book1, type_s:book, title_t : "The Way of Kings", author_s : "Brandon Sanderson",
  cat_s:fantasy, pubyear_i:2010, publisher_s:Tor,
  _childDocuments_ : [
    { id: book1_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z",
      stars_i:5, author_s:yonik,
      comment_t:"A great start to what looks like an epic series!"
    }
    ,
    { id: book1_c2, type_s:review, review_dt:"2014-03-15T12:00:00Z",
      stars_i:3, author_s:dan,
      comment_t:"This book was too long."
    }
  ]
 }
导入solr测试可以使用solr admin页面进行导入测试:
这是要注意把整个结构外面加一个中括号包起来。也可以使用curl通过http的形式进行导入测试:
$ curl http://localhost:8983/solr/demo/update?commitWithin=3000 -d '
[
 {id : book1, type_s:book, title_t : "The Way of Kings", author_s : "Brandon Sanderson",
  cat_s:fantasy, pubyear_i:2010, publisher_s:Tor,
  _childDocuments_ : [
    { id: book1_c1, type_s:review, review_dt:"2015-01-03T14:30:00Z",
      stars_i:5, author_s:yonik,
      comment_t:"A great start to what looks like an epic series!"
    }
    ,
    { id: book1_c2, type_s:review, review_dt:"2014-03-15T12:00:00Z",
      stars_i:3, author_s:dan,
      comment_t:"This book was too long."
    }
  ]
 }
]'

成功导入可以看到在底层,嵌套的结构还是分着存储的,我的配置文件id换成了objectid,可以直接使用id:
对于嵌套结构的查询,可以根据需要返回对应的子文档或者父文档,也可以联合查询:
返回联合查询:
返回子文档查询:
返回父文档查询:
但是对于多种条件的组合查询,比如多个子文档或者父文档查询,过滤条件的拼接比较困难,暂时没有找到好的解决方案。
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值