【问题标题】:How to Index & Search Nested Json in Solr 4.9.0如何在 Solr 4.9.0 中索引和搜索嵌套的 Json
【发布时间】:2016-05-04 05:18:31
【问题描述】:

我想在 solr 中索引和搜索嵌套的 json。这是我的json代码

{
        "id": "44444",
        "headline": "testing US",
        "generaltags": [
            {
                "type": "person",
                "name": "Jayalalitha",
                "relevance": "0.334",
                "count": 1
            },
            {
                "type": "person",
                "name": "Kumar",
                "relevance": "0.234",
                "count": 1
            }
        ],
        "socialtags": {
            "type": "SocialTag",
            "name": "US",
            "importance": 2
        },
        "topic": {
            "type": "Topic",
            "name": "US",
            "score": "0.936"
        }
    }

当我尝试建立索引时,我收到错误“解析 JSON 字段值时出错。意外的 OBJECT_START”

当我们尝试使用多值字段和索引时,我们无法使用多值字段进行搜索?它返回“未定义字段”

如果我需要对 schema.xml 文件进行任何更改,请告知?

【问题讨论】:

    标签: json solr


    【解决方案1】:

    您正在文档中嵌套子文档。您需要对 JSON 中的嵌套子文档使用正确的语法:

    [
      {
        "id": "1",
        "title": "Solr adds block join support",
        "content_type": "parentDocument",
        "_childDocuments_": [
          {
            "id": "2",
            "comments": "SolrCloud supports it too!"
          }
        ]
      },
      {
        "id": "3",
        "title": "Lucene and Solr 4.5 is out",
        "content_type": "parentDocument",
        "_childDocuments_": [
          {
            "id": "4",
            "comments": "Lots of new features"
          }
        ]
      }
    ]
    

    看看这个article,它描述了 JSON 子文档和块连接。

    【讨论】:

      【解决方案2】:

      使用@qux 提到的格式,你将面对"Expected: OBJECT_START but got ARRAY_START at [16]", "code": 400 当以 [....] 开头的 JSON 将被解析为 JSON 数组时

      {
              "id": "44444",
              "headline": "testing US",
              "generaltags": [
                  {
                      "type": "person",
                      "name": "Jayalalitha",
                      "relevance": "0.334",
                      "count": 1
                  },
                  {
                      "type": "person",
                      "name": "Kumar",
                      "relevance": "0.234",
                      "count": 1
                  }
              ],
              "socialtags": {
                  "type": "SocialTag",
                  "name": "US",
                  "importance": 2
              },
              "topic": {
                  "type": "Topic",
                  "name": "US",
                  "score": "0.936"
              }
          }

      上面的格式是正确的。 关于搜索。请使用索引搜索 JSON 数组的元素。 解决方法是将整个 JSON 对象保存在其他 JSON 对象中并对其进行索引

      我建议将整个数据保存在另一个 JSON 对象中。您可以尝试以下方式

      {
      "data": [
          {
              "id": "44444",
              "headline": "testing US",
              "generaltags": [
                  {
                      "type": "person",
                      "name": "Jayalalitha",
                      "relevance": "0.334",
                      "count": 1
                  },
                  {
                      "type": "person",
                      "name": "Kumar",
                      "relevance": "0.234",
                      "count": 1
                  }
              ],
              "socialtags": {
                  "type": "SocialTag",
                  "name": "US",
                  "importance": 2
              },
              "topic": {
                  "type": "Topic",
                  "name": "US",
                  "score": "0.936"
              }
          }
      ]
      }
      

      【讨论】:

      • Error parsing JSON field value. Unexpected OBJECT_START at [112], field=generaltags 错误代码 400 与您的示例代码。有什么想法吗?
      • 示例与@w3lessons 发布的相同。
      【解决方案3】:

      查看http://yonik.com/solr-nested-objects/中的语法

      $ 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."
          }
        ]
       }
      ]'
      

      从 solr 5.3 开始支持

      【讨论】:

        猜你喜欢
        • 2013-01-04
        • 1970-01-01
        • 1970-01-01
        • 2011-03-21
        • 1970-01-01
        • 1970-01-01
        • 2016-10-09
        • 2012-07-03
        • 2012-08-23
        相关资源
        最近更新 更多