lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Lukáš Vlček <lukas.vl...@gmail.com>
Subject Re: Querying across object relationships
Date Thu, 30 Jul 2009 20:38:25 GMT
Don,
may be you could try this:

   @Searchable   @SearchableDynamicMetaData(name="noAttending",
converter="groovy", expression=" ... here iterate over all attendances where
attendance = N and output all course names")
   @SearchableDynamicMetaData(name="yesAttending", converter="groovy",
expression=" ... here iterate over all attendances where attendance = Y and
output all course names")
   class Student {

       @SearchableProperty(accessor = 'property')
       String name

       static hasMany = [attendances: Attendance]

       @SearchableId(accessor = 'property')
       Long id

       @SearchableComponent
       Set<Attendance> getAttendances() {
           return attendances
       }
   }

In essence this will add two new fields for each student. Each filed will
contain a list of all cources which the student does attend/ does not
attend.
I think that using groovy you should be able to get suitable expression (may
be something like: data.attendances.findAll( it.mandatory == "Y").collect(
it.course.name ).join(' ')). Then you can query like: +yesAttending:cooking

Anyway, you should consider moving this conversation to Compass formu.

Lukas

http://blog.lukas-vlcek.com/


On Thu, Jul 30, 2009 at 10:00 PM, Lukáš Vlček <lukas.vlcek@gmail.com> wrote:

> Don,
> in order to use such query you have to keep mandatory and courseName
> relation in your index. In Compass you could use dynamic metadata (
> http://www.compass-project.org/docs/2.2.0/reference/html/core-osem.html#core-osem-dynamic).
> This way you can add additional fileds into your document. You can create an
> artificial field which is combination of mandatory and courseName. But may
> be re-thinking your object model would be better in this case.
>
> Lukas
>
> http://blog.lukas-vlcek.com/
>
>
> On Thu, Jul 30, 2009 at 9:42 PM, Steven A Rowe <sarowe@syr.edu> wrote:
>
>> Hi Donal,
>>
>> I looked at the XML index dump you provided, and I can see that there is
>> only one document in the index.  This document matches your query.  I've
>> pasted it below, without the "$/*"-named fields I'm assuming Compass adds to
>> manage Lucene document -> Grails object mapping, and with just the "name"
>> attribute on the field elements:
>>
>>  <doc id='1'>
>>    <field name='courseName'>
>>      <val>cooking</val>
>>      <val>art</val>
>>    </field>
>>    <field name='mandatory'>
>>      <val>N</val>
>>      <val>Y</val>
>>    </field>
>>    <field name='name'>
>>     <val>Bob</val>
>>    </field>
>>  </doc>
>>
>> Compass's Lucene document to Grails object mapping is your problem here.
>>
>> In Lucene-land, the query (+courseName:cooking +mandatory:Y) matches the
>> above document, because the document contains those values in those fields.
>>
>> So with that query, based on the Lucene document structure, you seem to be
>> asking the question: "Which student attends a cooking course and also
>> attends a mandatory course?".  Bob is a match.
>>
>> Steve
>>
>> > -----Original Message-----
>> > From: Donal Murtagh [mailto:domurtag@yahoo.co.uk]
>> > Sent: Thursday, July 30, 2009 3:10 PM
>> > To: java-user@lucene.apache.org
>> > Subject: Re: Querying across object relationships
>> >
>> > Basically the classes I'm indexing have the following relationships:
>> >
>> > Student 1------* Attendance 1------* Course
>> >
>> > The
>> > only root class is Student, i.e. only instances of this class can be
>> > returned from a search. I have a Student object graph that could be
>> > represented in JSON as follows:
>> >
>> > {
>> >   name: Bob,
>> >   attendances: [
>> >     {mandatory: N, course: {name: cooking}},
>> >     {mandatory: Y, course: {name: art}}]
>> > }
>> >
>> > When I search for an instance of Student using the query:
>> >
>> >   "+courseName:cooking +mandatory:Y"
>> >
>> > Bob
>> > is returned because, because he attends a course named "cooking" and he
>> > attends a mandatory course (named "art).. But what I really want to
>> > search for is students that attend a mandatory cooking course. It
>> > doesn't appear to be possible to do this based on the responses
>> > provided here:
>> > http://stackoverflow.com/questions/1202422/lucene-query-
>> > syntax/1203186#1203186
>> >
>> > I
>> > opened the Student index in Luke, exported it to XML and have appended
>> > the results here:
>> > http://pastebin.com/m6e5bbcf3
>> >
>> > I don't really know how to interpret this
>> > myself, but thanks in advance for any further help you can provide.
>> >
>> > - Don
>> >
>> >
>> >
>> >
>>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message