我需要将结果存储和检索为多维树,而不是平面“键”=>“值”对。让我用一个例子来解释,我有很多类别的产品,每个类别都有一个优先级值。样本结构:
{
name: "Sample Product"
categories: [
{
category: "Category 1",
priority: 9
},
{
category: "Category 2",
priority: 5
}
...
]
}
这是我的 data-config.xml:
<dataConfig>
<dataSource name="ds" type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/dbname" user="user" password="pass"/>
<document name="products">
<entity name="product" query="SELECT name FROM dbname.product">
<field name="name" column="name" />
<entity name="categories" query="SELECT category, priority FROM dbname.category WHERE product_id='${product.id}'">
<field name="category" column="category" />
<field name="priority" column="priority" />
</entity>
</entity>
</document>
</dataConfig>
架构.xml:
<schema name="Products" version="1.1">
<fields>
<field name="name" type="string" indexed="true" stored="true" multiValued="false" required="true" />
<field name="category" type="string" indexed="true" stored="true" multiValued="true" />
<field name="priority" type="sint" indexed="true" stored="true" multiValued="true" />
</fields>
...
</schema>
这是一个示例查询结果:
{
"responseHeader": {
"status": 0,
"QTime": 14
},
"response": {
"numFound": 45,
"start": 0,
"docs": [
{
"name": "Product Name",
"category": [
"Category 1",
"Category 2"
],
"priority": [
8,
6
]
},
...
我想要的是这样的:
{
"responseHeader": {
"status": 0,
"QTime": 14
},
"response": {
"numFound": 45,
"start": 0,
"docs": [
{
"name": "Product Name",
"categories": [
{
"name": "Category 1",
"priority": 8
},
{
"name": "Category 2",
"priority": 6
}
...
]
},
...
因此,当我根据优先级对结果进行排序时,我不会失去类别和优先级之间的联系。因此,我可以在 PHP 中为每个产品选择前 1、2 或 3 个类别。否则我必须在 PHP 端进行一些自定义排序来选择我不想要的顶级类别。我想在 Solr 服务器上进行所有搜索和排序。
我正在使用 Apache Solr 4.5.1