【问题标题】:How to store and retrieve Apache Solr fields as a tree (multidimensional array)?如何将 Apache Solr 字段存储和检索为树(多维数组)?
【发布时间】:2013-11-20 21:31:36
【问题描述】:

我需要将结果存储和检索为多维树,而不是扁平的“键”=>“值”对。让我用一个例子来解释一下,我有很多类别的产品,每个类别都有一个优先级值。样本结构:

{
  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>

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

【问题讨论】:

标签: solr solr4


【解决方案1】:

Solr 只能维护数据的“平面”表示。您尝试做的事情实际上是不可能的。有很多变通方法,例如使用dynamic fields 并使用solr join 链接多个数据集。

【讨论】:

    【解决方案2】:

    这是实现这一目标的一种快速方法。您可以连接优先级和名称字段。

    所以你的数据看起来像:

    {
      name: "Sample Product"
      categories: [
      {
        priority_category: "9_Category 1",
      },
      {
        priority_category: "5_Category 2",
      }
      ...
      ]
    }
    

    然后你可以在Solr中对priority_category字段进行原生排序,然后如果你想输出这些字段中的任何一个,你可以在PHP级别进行拆分,使用explode之类的。

    【讨论】:

      【解决方案3】:

      尝试使用索引时间块连接的 4.8 来检索这样的结果。但这是为了加入父子文档。否则,您需要使用上述推荐的字符串连接解决方​​案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-07
        • 1970-01-01
        • 2010-10-27
        • 2019-11-18
        • 1970-01-01
        • 2015-08-14
        • 2012-12-04
        • 1970-01-01
        相关资源
        最近更新 更多