如何索引&在Solr 4.9.0中搜索嵌套的Json

时间:2014-10-15 15:26:23

标签: json solr

我想索引&在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”

当我们尝试使用多值字段&索引,我们无法使用多值字段进行搜索?它返回“Undefined Field”

如果我需要在schema.xml文件中进行任何更改,请提供建议吗?

3 个答案:

答案 0 :(得分:6)

您正在文档中嵌套子文档。您需要在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"
      }
    ]
  }
]

看看这个描述JSON子文档和块连接的article

答案 1 :(得分:0)

使用@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"
        }
    }
]
}

答案 2 :(得分:-2)

请参阅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