Hello,
given the following document structure (books as parent, libraries having these 
books as children):

<update>
    <add>
        <doc>
            <field name="doc_type_s">book</field>
            <field name="id">1000</field>
            <field name="title_t">Mr. Mercedes</field>
            <field name="autor_t">Stephen King</field>
            <doc>
                <field name="doc_type_s">library</field>
                <field name="id">1000/100</field>
                <field name="acquisition_date_i">20160810</field>
                <field name="city_t">Innsbruck</field>
            </doc>
            <doc>
                <field name="doc_type_s">library</field>
                <field name="id">1000/101</field>
                <field name="acquisition_date_i">20180103</field>
                <field name="city_t">Hall</field>
            </doc>
        </doc>
        <doc>
            <field name="doc_type_s">book</field>
            <field name="id">1001</field>
            <field name="title_t">Noah</field>
            <field name="autor_t">Sebastian Fitzek</field>
            <doc>
                <field name="doc_type_s">library</field>
                <field name="id">1001/100</field>
                <field name="acquisition_date_i">20170810</field>
                <field name="city_t">Innsbruck</field>
            </doc>
        </doc>
    </add>
</update>

Now i want to get all books located in libraries in city "Innsbruck", sorted by 
acquisition date descending.
In other words: i want to filter in the field city_i in the child documents, 
but return only the parent document. And i want to sort by the field 
acquisition_date_i in the child documents in descending order, newest first.

My first try:
-----

URL:
http://localhost:8983/solr/test1/select?q=title_t:*&fq={!parent%20which=doc_type_s:book}city_t:Innsbruck&sort={!parent%20which=doc_type_s:book%20score=max%20v=%27%2Bdoc_type_s:library%20%2B{!func}acquisition_date_i%27}%20desc

URL params decoded:
q=title_t:*
fq={!parent which=doc_type_s:book}city_t:Innsbruck
sort={!parent which=doc_type_s:book score=max v='+doc_type_s:library 
+{!func}acquisition_date_i'} desc

Result:
{
  "responseHeader":{
    "status":0,
    "QTime":4,
    "params":{
      "q":"title_t:*",
      "fq":"{!parent which=doc_type_s:book}city_t:Innsbruck",
      "sort":"{!parent which=doc_type_s:book score=max v='+doc_type_s:library 
+{!func}acquisition_date_i'} desc"}},
  "response":{"numFound":2,"start":0,"docs":[
      {
        "doc_type_s":"book",
        "text":["book",
          "Mr. Mercedes",
          "Stephen King"],
        "id":"1000",
        "title_t":"Mr. Mercedes",
        "title_t_fac":"Mr. Mercedes",
        "autor_t":"Stephen King",
        "autor_t_fac":"Stephen King",
        "_version_":1591205521252155392},
      {
        "doc_type_s":"book",
        "text":["book",
          "Noah",
          "Sebastian Fitzek"],
        "id":"1001",
        "title_t":"Noah",
        "title_t_fac":"Noah",
        "autor_t":"Sebastian Fitzek",
        "autor_t_fac":"Sebastian Fitzek",
        "_version_":1591205521256349696}]
  }}

The result is wrong, because "Noah" should be before "Mr. Mercedes" in the 
list. The reason is, i guess, because "Mr. Mercedes" has another child document 
with a newer acquisition_date. But this child document is not in city 
"Innsbruck" and should not influence the sorting.

So i tried to add the city-filter to the sort-parameter as well in my second 
try:
-----

URL:
http://localhost:8983/solr/test1/select?q=title_t:*&fq={!parent%20which=doc_type_s:book}city_t:Innsbruck&sort={!parent%20which=doc_type_s:book%20score=max%20v=%27%2Bdoc_type_s:library%20%2Bcity_t:Innsbruck%20%2B{!func}acquisition_date_i%27}%20desc

URL params decoded:
q=title_t:*
fq={!parent which=doc_type_s:book}city_t:Innsbruck
sort={!parent which=doc_type_s:book score=max v='+doc_type_s:library 
+city_t:Innsbruck +{!func}acquisition_date_i'} desc

(I added "+city_t:Innsbruck" to the sort param)

Result:
{
  "responseHeader":{
    "status":0,
    "QTime":3,
    "params":{
      "q":"title_t:*",
      "fq":"{!parent which=doc_type_s:book}city_t:Innsbruck",
      "sort":"{!parent which=doc_type_s:book score=max v='+doc_type_s:library 
+city_t:Innsbruck +{!func}acquisition_date_i'} desc"}},
  "response":{"numFound":2,"start":0,"docs":[
      {
        "doc_type_s":"book",
        "text":["book",
          "Noah",
          "Sebastian Fitzek"],
        "id":"1001",
        "title_t":"Noah",
        "title_t_fac":"Noah",
        "autor_t":"Sebastian Fitzek",
        "autor_t_fac":"Sebastian Fitzek",
        "_version_":1591205521256349696},
      {
        "doc_type_s":"book",
        "text":["book",
          "Mr. Mercedes",
          "Stephen King"],
        "id":"1000",
        "title_t":"Mr. Mercedes",
        "title_t_fac":"Mr. Mercedes",
        "autor_t":"Stephen King",
        "autor_t_fac":"Stephen King",
        "_version_":1591205521252155392}]
  }}

Now the sorting is correct.

My question: 
Is there a way to achieve the same result without having add all filters to the 
search-param? 
In other words: is there a way to sort on a child field, but only in matching 
child-documents?


Thanks for your help.

Florian

Reply via email to