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 Semiaddict <ne...@semiaddict.com> on 2013/09/24 00:23:53 UTC

Select all descendants in a relation index

Hello,

I am using Solr to index Drupal node relations (over 300k relations on over 500k nodes), where each relation consists of the following fields:
- id : the id of the relation
- source_id : the source (parent) node id
- targe_id : the targe (child) node id

I need to be able to find all descendants of a node with one query.
So far I've managed to get direct children using the join syntax of Solr4 such as (http://wiki.apache.org/solr/Join): 
/solr/collection/select?q={!join from=source_id to=target_id}source_id:12

Note that each node can have multiple parents and multiple children.

Is there a way to get all descendants of node 12 without having to create a loop in PHP to find all children, then all children of each child, etc ?
If not, is it possible to create a recursive query directly in Solr, or is there a better way to index tree structures ?

Any help or suggestion would be highly appreciated.

Thank you in advance,

Semiaddict

Re: Select all descendants in a relation index

Posted by Oussama Mubarak <ne...@semiaddict.com>.
Thank you very much Erick.

Would you know by any chance of a tutorial or book that explains how to 
use PathHierarchyTokenizerFactory ?
How does solr know how to generate the path ?

Most examples online look like the one below, and don't explain how the 
path is generated:

<fieldType name="descendent_path" class="solr.TextField">
   <analyzer type="index">
     <tokenizer class="solr.PathHierarchyTokenizerFactory" delimiter="/" />
   </analyzer>
   <analyzer type="query">
     <tokenizer class="solr.KeywordTokenizerFactory" />
   </analyzer>
</fieldType>

Thank you,

Semiaddict

Le 25/09/2013 11:56, Erick Erickson a écrit :
> Well, then index the path to that node and do wildcard queries.
> With a file path example, index (maybe string type)
> /dir1/dir2/dir3/file
>
> Finding all descendants of "dir2" is simple, just search on
> /dir1/dir2/*
>
> Also see http://wiki.apache.org/solr/HierarchicalFaceting
>
> for other approaches.
>
>
> Best,
> Erick
>
> On Tue, Sep 24, 2013 at 10:37 AM, Oussama Mubarak <ne...@semiaddict.com> wrote:
>> Thank you Erick.
>>
>> I actually do need it to extend to grandchildren as stated in "I need to be
>> able to find all descendants of a node with one query".
>> I already have an index that allows me to find the direct children of a
>> node, what I need is to be able to get all descendants of a node (children,
>> grandchildren... etc).
>>
>> I have submitted this questions on stackoverflow where I put in more details
>> :
>> http://stackoverflow.com/questions/18984183/join-query-in-apache-solr-how-to-get-all-levels-in-hierarchical-data
>>
>> Semiaddict
>>
>>
>> Le 24/09/2013 16:08, Erick Erickson a écrit :
>>
>> Sure, index the parent node id (perhaps multiple) with each child
>> and add &fq=parent_id:12.
>>
>> you can do the reverse and index each node with it's child node IDs
>> to to ask the inverse question.
>>
>> This won't extend to grandchildren/parents, but you haven't stated that you
>> need to do this.
>>
>> Best,
>> Erick
>>
>> On Mon, Sep 23, 2013 at 6:23 PM, Semiaddict <ne...@semiaddict.com> wrote:
>>
>> Hello,
>>
>> I am using Solr to index Drupal node relations (over 300k relations on over
>> 500k nodes), where each relation consists of the following fields:
>> - id : the id of the relation
>> - source_id : the source (parent) node id
>> - targe_id : the targe (child) node id
>>
>> I need to be able to find all descendants of a node with one query.
>> So far I've managed to get direct children using the join syntax of Solr4
>> such as (http://wiki.apache.org/solr/Join):
>> /solr/collection/select?q={!join from=source_id to=target_id}source_id:12
>>
>> Note that each node can have multiple parents and multiple children.
>>
>> Is there a way to get all descendants of node 12 without having to create a
>> loop in PHP to find all children, then all children of each child, etc ?
>> If not, is it possible to create a recursive query directly in Solr, or is
>> there a better way to index tree structures ?
>>
>> Any help or suggestion would be highly appreciated.
>>
>> Thank you in advance,
>>
>> Semiaddict
>>
>>


Re: Select all descendants in a relation index

Posted by Oussama Mubarak <ne...@semiaddict.com>.
Thank you Erick.

I actually do need it to extend to grandchildren as stated in "I need to 
be able to find *all descendants* of a node with one query".
I already have an index that allows me to find the direct children of a 
node, what I need is to be able to get all descendants of a node 
(children, grandchildren... etc).

I have submitted this questions on stackoverflow where I put in more 
details : 
http://stackoverflow.com/questions/18984183/join-query-in-apache-solr-how-to-get-all-levels-in-hierarchical-data

Semiaddict


Le 24/09/2013 16:08, Erick Erickson a écrit :
> Sure, index the parent node id (perhaps multiple) with each child
> and add &fq=parent_id:12.
>
> you can do the reverse and index each node with it's child node IDs
> to to ask the inverse question.
>
> This won't extend to grandchildren/parents, but you haven't stated that you
> need to do this.
>
> Best,
> Erick
>
> On Mon, Sep 23, 2013 at 6:23 PM, Semiaddict <ne...@semiaddict.com> wrote:
>> Hello,
>>
>> I am using Solr to index Drupal node relations (over 300k relations on over 500k nodes), where each relation consists of the following fields:
>> - id : the id of the relation
>> - source_id : the source (parent) node id
>> - targe_id : the targe (child) node id
>>
>> I need to be able to find all descendants of a node with one query.
>> So far I've managed to get direct children using the join syntax of Solr4 such as (http://wiki.apache.org/solr/Join):
>> /solr/collection/select?q={!join from=source_id to=target_id}source_id:12
>>
>> Note that each node can have multiple parents and multiple children.
>>
>> Is there a way to get all descendants of node 12 without having to create a loop in PHP to find all children, then all children of each child, etc ?
>> If not, is it possible to create a recursive query directly in Solr, or is there a better way to index tree structures ?
>>
>> Any help or suggestion would be highly appreciated.
>>
>> Thank you in advance,
>>
>> Semiaddict


Re: Select all descendants in a relation index

Posted by Erick Erickson <er...@gmail.com>.
Sure, index the parent node id (perhaps multiple) with each child
and add &fq=parent_id:12.

you can do the reverse and index each node with it's child node IDs
to to ask the inverse question.

This won't extend to grandchildren/parents, but you haven't stated that you
need to do this.

Best,
Erick

On Mon, Sep 23, 2013 at 6:23 PM, Semiaddict <ne...@semiaddict.com> wrote:
> Hello,
>
> I am using Solr to index Drupal node relations (over 300k relations on over 500k nodes), where each relation consists of the following fields:
> - id : the id of the relation
> - source_id : the source (parent) node id
> - targe_id : the targe (child) node id
>
> I need to be able to find all descendants of a node with one query.
> So far I've managed to get direct children using the join syntax of Solr4 such as (http://wiki.apache.org/solr/Join):
> /solr/collection/select?q={!join from=source_id to=target_id}source_id:12
>
> Note that each node can have multiple parents and multiple children.
>
> Is there a way to get all descendants of node 12 without having to create a loop in PHP to find all children, then all children of each child, etc ?
> If not, is it possible to create a recursive query directly in Solr, or is there a better way to index tree structures ?
>
> Any help or suggestion would be highly appreciated.
>
> Thank you in advance,
>
> Semiaddict