0

I have a Solr index with structure as below.

{
"id": "1",
"bookName_s": "Core Concepts",
"bookDescription_s": "This is the description",
"isbn_s": "ABCD:123",    
"reviews": [
  {
    "id": "2",
    "Name_s": "review1",
    "detail_s": "sample review"
  }
],
"students": [
  {
    "id": "3",
    "Name_s": "student1",
    "student_s": "test student"
  }
]

}

How do i search for parent that has a reviewer with Name_s as 'review1' and student with Name_s as 'student'.

I tried parent block chain query like below but nothing seems to work -

q=({!parent which="*:* -_nest_path_:*"}(+_nest_path_:\/reviews +Name_s:*rev*)) AND ({!parent which="*:* -_nest_path_:*"}(+_nest_path_:\/students +Name_s:*stu*)) 

q=({!parent which="*:* -_nest_path_:*"}(+_nest_path_:\/reviews +Name_s:*rev*)(+_nest_path_:\/students +Name_s:*stu*))

Is there a way i can acheive this using the q operator instead of fq parameter? thanks

teja_98666
  • 85
  • 1
  • 11
  • 1
    Yes but you need to properly specifiy _which_ parent, here you pass `*:* -`, but you have better to add a specific field to help distinguish parent-child relationships even if you don't have a recursive structure, eg. using a string field `type` filled accordingly with 'book', 'review' or 'student', and then use a query like `q={!parent which="type:book"}` where is your children filter : `(type:review AND Name_s:review1) OR (type:student AND Name_s:student1)`. – EricLavault Jan 27 '22 at 15:48
  • Hi @EricLavault thanks for your reply. I noticed this statement in the solr documentations. `// BAD! DO NOT USE! q={!parent which="title:join"}comments:support` [link](https://solr.apache.org/guide/8_8/other-parsers.html#block-mask) It says that it would return documents that do no match the filter. Is there a way to get the query working with the nest path? – teja_98666 Jan 28 '22 at 07:22
  • 1
    My proposition has nothing to do with the filter `title:join`, which is bad because a title is (usually) free text and both some of the parents and children documents may contain the term 'join' in their title, so you can't really distinguish them. Now if you read correctly, I precisely suggest to prevent that by using a field whose values are constrained and allows to properly discern which documents are the parents, ie. `type:book`. – EricLavault Jan 28 '22 at 12:35
  • @EricLavault thanks for the reply. It makes sense now. – teja_98666 Jan 28 '22 at 16:16

1 Answers1

1

Based on EricLavault suggestion i modified the index to include type of the object in the index like below -

{
    "id": "1",
    "bookName_s": "Core Concepts",
    "bookDescription_s": "This is the description",
    "isbn_s": "ABCD:123",
    "type_s":"book"    
    "reviews": [
      {
        "id": "2",
        "Name_s": "review1",
        "detail_s": "sample review",
        "type":"review"
      }
    ],
    "students": [
      {
        "id": "3",
        "Name_s": "student1",
        "type":"student",
        "student_s": "test student"
      }
    ]
    }

and below queries worked.

{!parent which="type:book"}(type:review AND Name_s:review1) OR (type:student AND Name_s:student1)

returns all books with review1 and student1

teja_98666
  • 85
  • 1
  • 11
  • I thought we had to use the _childDocuments_ attribute when making nested documents. How would you retain the field names (reviews, students) – heyomi Jun 13 '23 at 09:22