Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using NEST with Elastic Search for collections

I'm trying to get my hands dirty with Elastic Search via the NEST .Net api and running into a couple of problems. I suspect I've misunderstood something, or am modelling my docs incorrectly but would appreciate some help.

I have a document with collections in it. A similar trite example below :

public class Company
{
    public DateTime RegisteredOn {get;set;}
    public string Name {get;set;}

    [ElasticProperty(Type = FieldType.nested)]
    public List<Employee> Employees {get;set;}
}

public class Employee 
{
   public string FirstName {get;set;}
   public string LastName {get;set;}

   [ElasticProperty(Type = FieldType.nested)]
   public List<SalesFigure> SalesFigures {get;set}
}

public class SaleFigure
{
   public int AverageMonthlySaleValue {get;set;}
   public int AverageVolumeSold {get;set;}
}

I've created an index with some data in at each level of the hierarchy and before indexing have called client.MapFromAttributes<Company>();

The following works, but I'd like to understand how I'd find all companies with employees with a firstName of Bob, and or find all companies with employees who have a an average AverageMonthlySaleValue > $1100

client.Search<Company>(query => query.Index("companies").Type("company")
                                 .From(0)
                                 .Size(100)
                                 .Filter(x => x.Term(n => n.Name, "Microsoft")));

Nested queries/filters have been suggested as has suggestions that I ought to flatten my document which I can do, but I'm trying to create a model which better represents the real domain so am in a quandary.

Equally, I know that I'll also have to use facets at some point so want to structure everything correctly to support that.

Thanks Tim

like image 572
Tim Butterfield Avatar asked Nov 19 '25 17:11

Tim Butterfield


1 Answers

So it turns out there wasn't much wrong with the structure of my document. The example is trite and the real property I was querying on a collection was a string, not an int, so case sensitivity kicked in.

I had to change the query to use a lower case string value for comparison which worked. Something like the following worked.

client.Search<Company>(query => query.Index("companies")
               .Type("company")
               .From(0)
               .Size(100)
               .Filter(x => x.Term("company.employees.firstName", "microsoft")));

I've still to work out how to use a lamda in place of "company.employees.firstName" but it works for now.

like image 77
Tim Butterfield Avatar answered Nov 21 '25 08:11

Tim Butterfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!