Because of the modification Ayende made, I was able to extract the data that I wanted. I am looking for a more elegant (more efficient) solution to my problem.
I have come up with an arbitrary structure that should model my situation. The reason that I use a Dictionary in the Recipe class is because the "Ingredients" that are attached to the Recipe, may contain over a hundred entries, but I only need to access one or two on a given request.
Given the following structure:
public class Ingredient
{
public int Id { get; set; }
public string Name { get; set; }
public int AmountInStock { get; set; }
}
public class IngredientInfo
{
public string UnitOfMeasure { get; set; }
public double Quantity { get; set; }
}
public class Recipe
{
public int Id { get; set; }
public string Name { get; set; }
public string Instructions { get; set; }
public IDictionary<int, IngredientInfo> Ingredients { get; set; }
}
I can construct a LuceneQuery:
session.Advanced.LuceneQuery<Recipe>()
.WhereEquals("Ingredients._" + key + ".Quantity", value.ToString())
.ToList();
In my use case, the key and quantity values are chosen by the User at runtime.
Thanks to the dynamic querying abilities it generates a dynamic index on the server used in returning the results from the above query:
from doc in docs.Recipes
select new { Ingredients_2Quantity = doc.Ingredients._2.Quantity }
This functions as you would expect, but since there may be hundreds of different Ingredients and I do not know which ingredients the user of the application will be querying on, it seems inefficient to create a dynamic index for each permutation of parameters the user may want.
What I have been attempting to do is generate a pre-computed Index _like_ the following:
from doc in docs.Recipes
from ingredient in doc.Ingredients
select new {id = ingredient.Key, quantity = ingredient.Value.Quantity}
I know Lucene doesn't know about dictionary's Keys or Values so my thought was that if I could enumerate over the "key" in JSON it would look similar to:
from doc in docs.Recipes
from ingredient in doc.Ingredients
from ingredientInfo in ingredient
select new {ingredientId = ingredient, quantity = ingredientInfo.Quantity}
I _am_ open to changing my document structure if a good alternative can be found. That being said, in my specific use case, there may be hundreds of IngredientInfo Items in a Recipe. That is why I want to use a dictionary instead of an array (for the runtime benefit of not having to enumerate over the collection every time i want to access an IngredientInfo item).
Does anyone have any thoughts on how I might accomplish this?