w3lessons
w3lessons

Reputation: 121

How to Index & Search Nested Json in Solr 4.9.0

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

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

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?

Upvotes: 12

Views: 4915

Answers (3)

alex
alex

Reputation: 1917

see the syntax in 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

Upvotes: -2

Dharmvir Tiwari
Dharmvir Tiwari

Reputation: 916

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

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

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

{
"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"
        }
    }
]
}

Upvotes: 0

qux
qux

Reputation: 1165

You are nesting child documents within your document. You need to use the proper syntax for nested child documents in 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.

Upvotes: 6

Related Questions