最近由于业务需求一直在弄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页面进行导入测试:
![](https://i-blog.csdnimg.cn/blog_migrate/55d06248f08ed658dad3d97ac64e6440.png)
这是要注意把整个结构外面加一个中括号包起来。也可以使用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:
![](https://i-blog.csdnimg.cn/blog_migrate/7b025b7a07a7e60920f1557261f41adf.png)
对于嵌套结构的查询,可以根据需要返回对应的子文档或者父文档,也可以联合查询:
返回联合查询:
![](https://i-blog.csdnimg.cn/blog_migrate/0bf0f74c13bb6587235a8ba1dc939671.png)
返回子文档查询:
![](https://i-blog.csdnimg.cn/blog_migrate/30c619ae4affd5fe52fa1ad3bd528e83.png)
返回父文档查询:
![](https://i-blog.csdnimg.cn/blog_migrate/1567f74d785d515b1ebc41517fd04379.png)
但是对于多种条件的组合查询,比如多个子文档或者父文档查询,过滤条件的拼接比较困难,暂时没有找到好的解决方案。