pashute
pashute

Reputation: 4053

Indexing inner class in ravendb

How do I define an index and query on it with non Lucene regular queries?

I have a class Foo stored in RavenDB. Each Foo has a single Bar defined inside it.

The query I want is:

bool fooExists;
try
{
   var foo = session.Query<Foo>("Foos/BarName")
                    .Where(x => x.Bar.Name == name);
   fooExists = foo != null && foo.Any<Foo>();
}

The Foo class is defined:

public int ID { get; set; }
public Bar Bar {get; set; }

Bar is defined

public int BarID { get; set; }
public string Name { get; set; }
public string MoreInfo { get; set; }

The JSON:

{
  "ID":1
  "Bar" { "BarID":11, "Name":"One", "MoreInfo":"Eleven" }
}
{
  "ID":2
  "Bar" { "BarID":12, "Name":"Two", "MoreInfo":"Twelve" }
}

The index: Name Foos/BarName

from foo in docs.Foos
select new
{
    foo.Bar.Name        
} 

This code is crashing when called. The exception reads:Missing Bar_Name field in index

Upvotes: 0

Views: 31

Answers (1)

pashute
pashute

Reputation: 4053

OK, the answer was simple, but only after try catch and some experimenting You create the indexed field's name with an underscore and everything works correctly.

So edit the index and set it thus:

from foo in docs.Foos
select new
{
    Bar_Name = foo.Bar.Name        
}    

Upvotes: 1

Related Questions