Spatial Query with extra criteria

26 views
Skip to first unread message

AndyMcGoldrick

unread,
Aug 2, 2011, 6:34:23 AM8/2/11
to ravendb
Hi,

I was looking for a bit of advice.

I receive a Username, Longitude and Latitude and wish to query for all
Contractor entities that are within a radius of the Longitude and
Latitude and their Skills match the Interests of the UserDetails
document that corresponds to the Username.

My query is currently of the form:
var locationQuery =

Session.Advanced.LuceneQuery<Contractor>("Locations/ByLocation")
.WithinRadiusOf(radius: 150, latitude:
55.80820, longitude: -4.36170);

var locationResults = locationQuery.ToList();

And my Index looks like

public class Locations_ByLocation :
AbstractIndexCreationTask<Models.Promotion>
{
public Locations_ByLocation()
{
Map = locations => from r in locations
select new { _ =
SpatialIndex.Generate(r.Latitude, r.Longitude) };
}
}


I have the following document structures:

public class UserDetails
{
public string Id {get; set;}
public string Name { get; set; }
public string MobileNumber { get; set; }
public string Sex { get; set; }
public string Age { get; set; }
public string Email { get; set; }
public string Username { get; set; }
public string Country { get; set; }
public string Postcode { get; set; }
public List<Interest> Interests { get; set; }
}

public class Company
{
public string UserId { get; set; }
public string Id { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string ContactPhone { get; set; }
public string ContactEmail { get; set; }
public string Website { get; set; }
public Location ContactAddress { get; set; }
public byte[] Logo { get; set; }
}

public class Team
{
public string Id { get; set; }
public string CompanyId { get; set; }
public string Name { get; set; }
public Location Address { get; set; }
public List<Contractor> Promotions { get; set; }
}

public class Contractor
{
public string Id { get; set; }
public string CompanyId { get; set; }
public string Headline { get; set; }
public string Description { get; set; }
public string SiteId { get; set; }
public List<string> Category { get; set; }
public string Price { get; set; }
public string ExpiryDate { get; set; }
public string StartDate { get; set; }

// denormalised from the Team entity
public double Longitude { get; set; }
public double Latitude { get; set; }
public Location Address { get; set; }

// denormalised from the Company entity
public byte[] Logo { get; set; }
public string Website { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public string Vendor { get; set; }
}


Thanks

Andy

Ayende Rahien

unread,
Aug 2, 2011, 6:51:11 AM8/2/11
to rav...@googlegroups.com
I don't understand your model.
I am not sure that I am following your domain.
What is the association between UserDetails and Contractor? 

AndyMcGoldrick

unread,
Aug 2, 2011, 7:02:30 AM8/2/11
to ravendb
Hi,

Yeah I wasn't too clear, sorry.

There is no real assosciation.

The UserDetails represents the users of the system, where they can
setup their properties and in particular which Skills they are
interested in, this is in the List<string> Interests (there was a typo
in the original post)

They can then log intot he system and initiate a search that will use
their username, to retrieve their Interests and location, specified as
a Longitude and Latitude pair to identify contractors that are located
within a radius of their location and who's Skills match one or more
of the user's Interests.

Andy

On Aug 2, 11:51 am, Ayende Rahien <aye...@ayende.com> wrote:
> I don't understand your model.
> I am not sure that I am following your domain.
> What is the association between UserDetails and Contractor?
>

Ayende Rahien

unread,
Aug 2, 2011, 7:06:55 AM8/2/11
to rav...@googlegroups.com
Okay,
So what you want is something like:


var user = session.Query<UserDetails>().Where(x=>x.UserName = username).First();

Now you want to user user.Interests to match it up to what exactly in contractor?

AndyMcGoldrick

unread,
Aug 2, 2011, 7:47:21 AM8/2/11
to ravendb
Yeah that's it, that's what I was wanting to do.

In SQL world an IN statement and possibly a JOIN to avoid 2 queries?


Thanks

Andy

On Aug 2, 12:06 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Okay,
> So what you want is something like:
>
> var user = session.Query<UserDetails>().Where(x=>x.UserName =
> username).First();
>
> Now you want to user user.Interests to match it up to what exactly in
> contractor?
>

Ayende Rahien

unread,
Aug 2, 2011, 7:48:53 AM8/2/11
to rav...@googlegroups.com
We have support for In, yes. But I still don't understand what you are querying against

AndyMcGoldrick

unread,
Aug 2, 2011, 7:56:51 AM8/2/11
to ravendb
Sorry my explanation is a bit poor, is this clearer?

I want to retrieve the UserDetails and then based on the List<string>
Interests find all the Contractors who are within a radius of the
supplied Longitude and Latitude with one of more of the UserDetails
List<string> Interests being present in the Contractor List<string>
Skills.

Andy

On Aug 2, 12:48 pm, Ayende Rahien <aye...@ayende.com> wrote:
> We have support for In, yes. But I still don't understand what you are
> querying against
>

Ayende Rahien

unread,
Aug 2, 2011, 10:41:30 AM8/2/11
to rav...@googlegroups.com
Create an index like:

from c in contractors
from interest in c.Interests
select new { interest, _ SpatialIndex.Generate(c.Long, c.Lat) }

AndyMcGoldrick

unread,
Aug 3, 2011, 5:24:01 AM8/3/11
to ravendb
Excellent - that does exactly what I wanted.

Thanks

On Aug 2, 3:41 pm, Ayende Rahien <aye...@ayende.com> wrote:
> Create an index like:
>
> from c in contractors
> from interest in c.Interests
> select new { interest, _ SpatialIndex.Generate(c.Long, c.Lat) }
>
Reply all
Reply to author
Forward
0 new messages