我需要存儲和檢索結果作爲多維樹而不是平面「鍵」=>「值」對。讓我以一個例子來解釋,我有許多類別的產品,每個類別都有一個優先級值。樣本結構:如何將Apache Solr字段存儲並檢索爲樹(多維數組)?
{
name: "Sample Product"
categories: [
{
category: "Category 1",
priority: 9
},
{
category: "Category 2",
priority: 5
}
...
]
}
這裏是我的數據-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>
Schema.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