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

时间:2022-09-01 22:13:02

I want to index & search nested json in solr. Here is my json code

我想在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"
        }
    }

When I try to Index, I'm getting the error "Error parsing JSON field value. Unexpected OBJECT_START"

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

When we tried to use Multivalued Field & index, we couldn't able to search using the multivalued field? Its returning "Undefined Field"

当我们尝试使用多值字段和索引时,我们不能使用多值字段进行搜索?返回“未定义的领域”

Also Please advice if I need to do any changes in schema.xml file?

如果我需要对模式做任何更改,也请建议。xml文件吗?

3 个解决方案

#1


6  

You are nesting child documents within your document. You need to use the proper syntax for nested child documents in JSON:

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

Have a look at this article which describes JSON child documents and block joins.

请看这篇描述JSON子文档和块连接的文章。

#2


0  

Using the format mentioned by @qux you will face "Expected: OBJECT_START but got ARRAY_START at [16]", "code": 400 as when JSON starting with [....] will parsed as a JSON array

使用格式@qux提到的您将面临“预期:OBJECT_START但ARRAY_START[16]”、“代码”: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"
        }
    }

The above format is correct. Regarding searching. Kindly use the index to search for the elements of the JSON array. The workaround for this can be keeping the whole JSON object inside other JSON object and the indexing it

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

I was suggesting to keep the whole data inside another JSON object. You can try the following way

我建议将整个数据保存在另一个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"
        }
    }
]
}

#3


-2  

see the syntax in http://yonik.com/solr-nested-objects/

请参见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."
    }
  ]
 }
]'

supported from solr 5.3

支持从solr 5.3

#1


6  

You are nesting child documents within your document. You need to use the proper syntax for nested child documents in JSON:

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

Have a look at this article which describes JSON child documents and block joins.

请看这篇描述JSON子文档和块连接的文章。

#2


0  

Using the format mentioned by @qux you will face "Expected: OBJECT_START but got ARRAY_START at [16]", "code": 400 as when JSON starting with [....] will parsed as a JSON array

使用格式@qux提到的您将面临“预期:OBJECT_START但ARRAY_START[16]”、“代码”: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"
        }
    }

The above format is correct. Regarding searching. Kindly use the index to search for the elements of the JSON array. The workaround for this can be keeping the whole JSON object inside other JSON object and the indexing it

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

I was suggesting to keep the whole data inside another JSON object. You can try the following way

我建议将整个数据保存在另一个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"
        }
    }
]
}

#3


-2  

see the syntax in http://yonik.com/solr-nested-objects/

请参见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."
    }
  ]
 }
]'

supported from solr 5.3

支持从solr 5.3