You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by DorZion <Do...@gmail.com> on 2015/06/05 10:35:06 UTC

Sorting documents by nested / child docs with FunctionQueries

Hey,

I'm using Solr 5.0.0 and I'm trying to sort documents with FunctionQueries.
The problem is that I'm trying to sort those documents with their child
documents elements.

Here is an example:
I have three documents, one is parent, the others are child documents
(_childDocuments_).

{
"id": "ArticleGuid",
"title" : "Sorting Child Documents",
"content_type": "article",
"_childDocuments_": [
  {
    "id": "CommentGuid",
    "content_type": "comment",
    "text": "CommentText"
  },
  {
    "id": "CommentGuid2",
    "content_type": "comment",
    "text": "CommentText"
  }
]
}
*
* This is just an example, the schema and the data aren't real.*

I want to analyze & run an algorithm on the comments text, then sort the
parent documents by it - *This would work with FunctionQuery, if I had one
comment for each article and didn't use child documents*.
I tried to use the LeafReadContext to get the children, but with no luck.

Is there any way to reach the children through FunctionQuery, or even use
something else to do what I want?


Thanks in advance,

Dor



--
View this message in context: http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by Mikhail Khludnev <mk...@griddynamics.com>.
no way. it's SOLR-6096 aka SOLR-6700

On Thu, Jun 25, 2015 at 9:16 AM, מאיה גלעד <ma...@gmail.com> wrote:

> Hey
> Your example works on my cloud but my problem didn't resolve.
>
> I'be checked and found the following :
> 1. When a child is created with multivalues it can be queried correctly
> with the url you've given me.
> 2. If you add a new value to a field in an existing child it doesn't
> return in the father-child query but can be queried individualy.
>
> Thank you,
> Maya
> Just pulled and launched Solr 5.2.1
>
> dropped multivalued child into see data below. Response is quite correct:
>
> "id":"22", "COLOR_s":"Blue","SIZE_ss":["XL","XXL"]}]}]
>
>
> http://localhost:8983/solr/solr/select?q={!parent+which%3Dtype_s%3Aparent}%2BCOLOR_s%3ABlue+%2BSIZE_ss%3AXL&fl=id%2C[child+parentFilter%3Dtype_s%3Aparent+childFilter%3D-type_s%3Aparent+limit%3D100]&wt=json&indent=true
> <http://localhost:8983/solr/solr/select?q=%7B!parent+which%3Dtype_s%3Aparent%7D%2BCOLOR_s%3ABlue+%2BSIZE_ss%3AXL&fl=id%2C[child+parentFilter%3Dtype_s%3Aparent+childFilter%3D-type_s%3Aparent+limit%3D100]&wt=json&indent=true>
>
> {
>   "responseHeader":{
>     "status":0,
>     "QTime":3,
>     "params":{
>       "q":"{!parent which=type_s:parent}+COLOR_s:Blue +SIZE_ss:XL",
>       "indent":"true",
>       "fl":"id,[child parentFilter=type_s:parent childFilter=-type_s:parent limit=100]",
>       "wt":"json"}},
>   "response":{"numFound":2,"start":0,"docs":[
>       {
>         "id":"10",
>         "_childDocuments_":[
>         {
>           "id":"11",
>           "COLOR_s":"Red",
>           "SIZE_ss":["XL"]},
>         {
>           "id":"12",
>           "COLOR_s":"Blue",
>           "SIZE_ss":["XL"]}]},
>       {
>         "id":"20",
>         "_childDocuments_":[
>         {
>           "id":"21",
>           "COLOR_s":"Red",
>           "SIZE_ss":["M"]},
>         {
>           "id":"22",
>           "COLOR_s":"Blue",
>           "SIZE_ss":["XL",
>             "XXL"]}]}]
>   }}
>
> <update>
>   <delete><query>*:*</query></delete>
>   <add>
>     <doc>
>       <field name="id">10</field>
>       <field name="type_s">parent</field>
>       <field name="BRAND_s">Nike</field>
>       <doc>
>         <field name="id">11</field>
>         <field name="COLOR_s">Red</field>
>         <field name="SIZE_ss">XL</field>
>       </doc>
>       <doc>
>         <field name="id">12</field>
>         <field name="COLOR_s">Blue</field>
>         <field name="SIZE_ss">XL</field>
>       </doc>
>     </doc>
>     <doc>
>       <field name="id">20</field>
>       <field name="type_s">parent</field>
>       <field name="BRAND_s">Nike</field>
>       <doc>
>         <field name="id">21</field>
>         <field name="COLOR_s">Red</field>
>         <field name="SIZE_ss">M</field>
>       </doc>
>       <doc>
>         <field name="id">22</field>
>         <field name="COLOR_s">Blue</field>
>         <field name="SIZE_ss">XL</field>
>         <field name="SIZE_ss">XXL</field>
>       </doc>
>     </doc>
>     <doc>
>       <field name="id">30</field>
>       <field name="type_s">parent</field>
>       <field name="BRAND_s">Puma</field>
>       <doc>
>         <field name="id">31</field>
>         <field name="COLOR_s">Red</field>
>         <field name="SIZE_ss">XL</field>
>       </doc>
>       <doc>
>         <field name="id">32</field>
>         <field name="COLOR_s">Blue</field>
>         <field name="SIZE_ss">M</field>
>       </doc>
>     </doc>
>   </add>
>   <commit/>
> </update>
>
>
>
> On Mon, Jun 22, 2015 at 2:04 PM, Maya G <ma...@gmail.com> wrote:
>
>> I've tried your solution and encountered a problem.
>>
>> My child document has a multi-valued field.
>>
>> When I query the doc by its' guid, all of the field's values are returned.
>> When I use the join block query only one value is returned for the
>> multi-value field.
>>
>> Do you have any suggestions?
>> Thank you,
>> Maya
>>
>>
>>
>> --
>> View this message in context:
>> http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940p4213242.html
>> Sent from the Solr - User mailing list archive at Nabble.com.
>>
>
>
>
> --
> Sincerely yours
> Mikhail Khludnev
> Principal Engineer,
> Grid Dynamics
>
> <http://www.griddynamics.com>
> <mk...@griddynamics.com>
>



-- 
Sincerely yours
Mikhail Khludnev
Principal Engineer,
Grid Dynamics

<http://www.griddynamics.com>
<mk...@griddynamics.com>

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by Mikhail Khludnev <mk...@griddynamics.com>.
Just pulled and launched Solr 5.2.1

dropped multivalued child into see data below. Response is quite correct:

"id":"22", "COLOR_s":"Blue","SIZE_ss":["XL","XXL"]}]}]

http://localhost:8983/solr/solr/select?q={!parent+which%3Dtype_s%3Aparent}%2BCOLOR_s%3ABlue+%2BSIZE_ss%3AXL&fl=id%2C[child+parentFilter%3Dtype_s%3Aparent+childFilter%3D-type_s%3Aparent+limit%3D100]&wt=json&indent=true

{
  "responseHeader":{
    "status":0,
    "QTime":3,
    "params":{
      "q":"{!parent which=type_s:parent}+COLOR_s:Blue +SIZE_ss:XL",
      "indent":"true",
      "fl":"id,[child parentFilter=type_s:parent
childFilter=-type_s:parent limit=100]",
      "wt":"json"}},
  "response":{"numFound":2,"start":0,"docs":[
      {
        "id":"10",
        "_childDocuments_":[
        {
          "id":"11",
          "COLOR_s":"Red",
          "SIZE_ss":["XL"]},
        {
          "id":"12",
          "COLOR_s":"Blue",
          "SIZE_ss":["XL"]}]},
      {
        "id":"20",
        "_childDocuments_":[
        {
          "id":"21",
          "COLOR_s":"Red",
          "SIZE_ss":["M"]},
        {
          "id":"22",
          "COLOR_s":"Blue",
          "SIZE_ss":["XL",
            "XXL"]}]}]
  }}

<update>
  <delete><query>*:*</query></delete>
  <add>
    <doc>
      <field name="id">10</field>
      <field name="type_s">parent</field>
      <field name="BRAND_s">Nike</field>
      <doc>
        <field name="id">11</field>
        <field name="COLOR_s">Red</field>
        <field name="SIZE_ss">XL</field>
      </doc>
      <doc>
        <field name="id">12</field>
        <field name="COLOR_s">Blue</field>
        <field name="SIZE_ss">XL</field>
      </doc>
    </doc>
    <doc>
      <field name="id">20</field>
      <field name="type_s">parent</field>
      <field name="BRAND_s">Nike</field>
      <doc>
        <field name="id">21</field>
        <field name="COLOR_s">Red</field>
        <field name="SIZE_ss">M</field>
      </doc>
      <doc>
        <field name="id">22</field>
        <field name="COLOR_s">Blue</field>
        <field name="SIZE_ss">XL</field>
        <field name="SIZE_ss">XXL</field>
      </doc>
    </doc>
    <doc>
      <field name="id">30</field>
      <field name="type_s">parent</field>
      <field name="BRAND_s">Puma</field>
      <doc>
        <field name="id">31</field>
        <field name="COLOR_s">Red</field>
        <field name="SIZE_ss">XL</field>
      </doc>
      <doc>
        <field name="id">32</field>
        <field name="COLOR_s">Blue</field>
        <field name="SIZE_ss">M</field>
      </doc>
    </doc>
  </add>
  <commit/>
</update>



On Mon, Jun 22, 2015 at 2:04 PM, Maya G <ma...@gmail.com> wrote:

> I've tried your solution and encountered a problem.
>
> My child document has a multi-valued field.
>
> When I query the doc by its' guid, all of the field's values are returned.
> When I use the join block query only one value is returned for the
> multi-value field.
>
> Do you have any suggestions?
> Thank you,
> Maya
>
>
>
> --
> View this message in context:
> http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940p4213242.html
> Sent from the Solr - User mailing list archive at Nabble.com.
>



-- 
Sincerely yours
Mikhail Khludnev
Principal Engineer,
Grid Dynamics

<http://www.griddynamics.com>
<mk...@griddynamics.com>

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by Maya G <ma...@gmail.com>.
I've tried your solution and encountered a problem.

My child document has a multi-valued field. 

When I query the doc by its' guid, all of the field's values are returned.
When I use the join block query only one value is returned for the
multi-value field.

Do you have any suggestions?
Thank you,
Maya



--
View this message in context: http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940p4213242.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by Mikhail Khludnev <mk...@griddynamics.com>.
it should be something like

q": "{!parent which=$parent_filter scoreMode=Max}+{!lucene v=$child_filter}
+{!func v=$sort_func}",
"parent_filter": "content_type:parent"
"child_filter": "content_type:child"
"indent": "true"
"fl": "*,[child parentFilter=$parent_filter childFilter=$child_filter]"
"sort_func": "analyzeText(text)"
"sort": "score desc"

respond with debugQuery=on response on a problem

On Tue, Jun 9, 2015 at 2:32 PM, DorZion <Do...@gmail.com> wrote:

> Thanks for your reply.
>
> I couldn't make it work. Everything went fine when I applied the patch and
> compiled Solr, but I couldn't use the FunctionQuery with it.
>
> My FunctionQuery accesses the field "text" in the document, however the
> parent doesn't have that field, so the plugin raises an exception. When I
> suppress the exception and return the minimum value instead, nothing
> happens.
>
> *Example of my query:*
> "q": "*:*",
> "parent_filter": "content_type:parent"
> "child_filter": "content_type:child"
> "indent": "true"
> "fl": "*,[child parentFilter=$parent_filter childFilter=$child_filter]"
> "fq": "{!parent which=$parent_filter v=$child_filter scoreMode=Max}"
> "sort": "analyzeText(text) desc"
>
> *Another example for what I'm doing in the FunctionQuery plugin (This isn't
> the original code):*
>  public FunctionValues getValues(Map map, AtomicReaderContext
> atomicReaderContext) throws IOException {
>
>         final FunctionValues values =
> mValueSource.getValues(map,atomicReaderContext);
>
>         return new FunctionValues() {
>             @Override
>             public float floatVal(int i) {
>                 String value = values.strVal(i);
>
>                 if(value == null || value.isEmpty()) {
>                     return Float.NaN;
>                 }
>
>                 return value.length();
>             ...
>             ...
>             ...
>             Other functions that use the and returns the floatVal value
>
>
> Very grateful for your help,
>
> Dor
>
>
>
> Mikhail Khludnev wrote
> > pickup the patch https://issues.apache.org/jira/browse/SOLR-5882 and/or
> > chase committers.
> >
> > On Fri, Jun 5, 2015 at 10:35 AM, DorZion &lt;
>
> > Dorzion@
>
> > &gt; wrote:
> >
> >> Hey,
> >>
> >> I'm using Solr 5.0.0 and I'm trying to sort documents with
> >> FunctionQueries.
> >> The problem is that I'm trying to sort those documents with their child
> >> documents elements.
> >>
> >> Here is an example:
> >> I have three documents, one is parent, the others are child documents
> >> (_childDocuments_).
> >>
> >> {
> >> "id": "ArticleGuid",
> >> "title" : "Sorting Child Documents",
> >> "content_type": "article",
> >> "_childDocuments_": [
> >>   {
> >>     "id": "CommentGuid",
> >>     "content_type": "comment",
> >>     "text": "CommentText"
> >>   },
> >>   {
> >>     "id": "CommentGuid2",
> >>     "content_type": "comment",
> >>     "text": "CommentText"
> >>   }
> >> ]
> >> }
> >> *
> >> * This is just an example, the schema and the data aren't real.*
> >>
> >> I want to analyze & run an algorithm on the comments text, then sort the
> >> parent documents by it - *This would work with FunctionQuery, if I had
> >> one
> >> comment for each article and didn't use child documents*.
> >> I tried to use the LeafReadContext to get the children, but with no
> luck.
> >>
> >> Is there any way to reach the children through FunctionQuery, or even
> use
> >> something else to do what I want?
> >>
> >>
> >> Thanks in advance,
> >>
> >> Dor
> >>
> >>
> >>
> >> --
> >> View this message in context:
> >>
> http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940.html
> >> Sent from the Solr - User mailing list archive at Nabble.com.
> >>
> >
> >
> >
> > --
> > Sincerely yours
> > Mikhail Khludnev
> > Principal Engineer,
> > Grid Dynamics
> >
> > &lt;http://www.griddynamics.com&gt;
> > &lt;
>
> > mkhludnev@
>
> > &gt;
>
>
>
>
>
> --
> View this message in context:
> http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940p4210646.html
> Sent from the Solr - User mailing list archive at Nabble.com.
>



-- 
Sincerely yours
Mikhail Khludnev
Principal Engineer,
Grid Dynamics

<http://www.griddynamics.com>
<mk...@griddynamics.com>

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by DorZion <Do...@gmail.com>.
Thanks for your reply.

I couldn't make it work. Everything went fine when I applied the patch and
compiled Solr, but I couldn't use the FunctionQuery with it.

My FunctionQuery accesses the field "text" in the document, however the
parent doesn't have that field, so the plugin raises an exception. When I
suppress the exception and return the minimum value instead, nothing
happens.

*Example of my query:*
"q": "*:*",
"parent_filter": "content_type:parent"
"child_filter": "content_type:child"
"indent": "true"
"fl": "*,[child parentFilter=$parent_filter childFilter=$child_filter]"
"fq": "{!parent which=$parent_filter v=$child_filter scoreMode=Max}"
"sort": "analyzeText(text) desc"

*Another example for what I'm doing in the FunctionQuery plugin (This isn't
the original code):*
 public FunctionValues getValues(Map map, AtomicReaderContext
atomicReaderContext) throws IOException {
                
        final FunctionValues values =
mValueSource.getValues(map,atomicReaderContext);

        return new FunctionValues() {
            @Override
            public float floatVal(int i) {
                String value = values.strVal(i);

                if(value == null || value.isEmpty()) {
                    return Float.NaN;
                }

                return value.length();
            ...
            ...
            ...
            Other functions that use the and returns the floatVal value


Very grateful for your help,

Dor
 


Mikhail Khludnev wrote
> pickup the patch https://issues.apache.org/jira/browse/SOLR-5882 and/or
> chase committers.
> 
> On Fri, Jun 5, 2015 at 10:35 AM, DorZion &lt;

> Dorzion@

> &gt; wrote:
> 
>> Hey,
>>
>> I'm using Solr 5.0.0 and I'm trying to sort documents with
>> FunctionQueries.
>> The problem is that I'm trying to sort those documents with their child
>> documents elements.
>>
>> Here is an example:
>> I have three documents, one is parent, the others are child documents
>> (_childDocuments_).
>>
>> {
>> "id": "ArticleGuid",
>> "title" : "Sorting Child Documents",
>> "content_type": "article",
>> "_childDocuments_": [
>>   {
>>     "id": "CommentGuid",
>>     "content_type": "comment",
>>     "text": "CommentText"
>>   },
>>   {
>>     "id": "CommentGuid2",
>>     "content_type": "comment",
>>     "text": "CommentText"
>>   }
>> ]
>> }
>> *
>> * This is just an example, the schema and the data aren't real.*
>>
>> I want to analyze & run an algorithm on the comments text, then sort the
>> parent documents by it - *This would work with FunctionQuery, if I had
>> one
>> comment for each article and didn't use child documents*.
>> I tried to use the LeafReadContext to get the children, but with no luck.
>>
>> Is there any way to reach the children through FunctionQuery, or even use
>> something else to do what I want?
>>
>>
>> Thanks in advance,
>>
>> Dor
>>
>>
>>
>> --
>> View this message in context:
>> http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940.html
>> Sent from the Solr - User mailing list archive at Nabble.com.
>>
> 
> 
> 
> -- 
> Sincerely yours
> Mikhail Khludnev
> Principal Engineer,
> Grid Dynamics
> 
> &lt;http://www.griddynamics.com&gt;
> &lt;

> mkhludnev@

> &gt;





--
View this message in context: http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940p4210646.html
Sent from the Solr - User mailing list archive at Nabble.com.

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by Mikhail Khludnev <mk...@griddynamics.com>.
pickup the patch https://issues.apache.org/jira/browse/SOLR-5882 and/or
chase committers.

On Fri, Jun 5, 2015 at 10:35 AM, DorZion <Do...@gmail.com> wrote:

> Hey,
>
> I'm using Solr 5.0.0 and I'm trying to sort documents with FunctionQueries.
> The problem is that I'm trying to sort those documents with their child
> documents elements.
>
> Here is an example:
> I have three documents, one is parent, the others are child documents
> (_childDocuments_).
>
> {
> "id": "ArticleGuid",
> "title" : "Sorting Child Documents",
> "content_type": "article",
> "_childDocuments_": [
>   {
>     "id": "CommentGuid",
>     "content_type": "comment",
>     "text": "CommentText"
>   },
>   {
>     "id": "CommentGuid2",
>     "content_type": "comment",
>     "text": "CommentText"
>   }
> ]
> }
> *
> * This is just an example, the schema and the data aren't real.*
>
> I want to analyze & run an algorithm on the comments text, then sort the
> parent documents by it - *This would work with FunctionQuery, if I had one
> comment for each article and didn't use child documents*.
> I tried to use the LeafReadContext to get the children, but with no luck.
>
> Is there any way to reach the children through FunctionQuery, or even use
> something else to do what I want?
>
>
> Thanks in advance,
>
> Dor
>
>
>
> --
> View this message in context:
> http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940.html
> Sent from the Solr - User mailing list archive at Nabble.com.
>



-- 
Sincerely yours
Mikhail Khludnev
Principal Engineer,
Grid Dynamics

<http://www.griddynamics.com>
<mk...@griddynamics.com>

Re: Sorting documents by nested / child docs with FunctionQueries

Posted by DorZion <Do...@gmail.com>.
Now I'm trying to sort the children by normal field and not by function
values.

Is there any way to do so? 

Thanks,

Dor



--
View this message in context: http://lucene.472066.n3.nabble.com/Sorting-documents-by-nested-child-docs-with-FunctionQueries-tp4209940p4216169.html
Sent from the Solr - User mailing list archive at Nabble.com.