Creating a Lucene index on values in a collection

418 views
Skip to first unread message

tcoonfield

unread,
Sep 22, 2011, 9:48:15 AM9/22/11
to ravendb
I'm fairly new to RavenDB and Lucene, so hoping that someone can help
me out with how to create this index.

Our core Product object has a collection of Attributes, which are
essentially a dynamic set of key/value pairs with information on the
product -- and the set of keys in the list varies from one product to
the next. I need to figure out how to create an Lucene index on the
values in that collection.

A simple version of the document (and in actuality, you may be looking
at more like 50 AttributeValue objects and a series of other
collections besides):

{
"ProductId": 4,
"SiteId": 8,
"AttributeValues": {
"$type":
"System.Collections.Generic.List`1[[ABC.XYZ.AttributeValue,
ABC.Core]], mscorlib",
"$values": [
{
"AttributeId": 1,
"AttributeName": "Description",
"Value": "My Product Description",
"Id": null
},
{
"AttributeId": 2,
"AttributeName": "Is On Sale",
"Value": "No",
"Id": null
},
{
"AttributeId": 4,
"AttributeName": "Color",
"Value": "Green",
"Id": null
}
]
}
}

Easy enough to create an index on, say, the product id, but can't
quite figure out the collection.

I did find this: http://codeofrob.com/archive/2010/12/21/ravendb-document-design-with-collections.aspx

which opened up the idea of making AttributeValue a separate document
with a product id property and getting the complete object in two
separate pulls, but while that would certainly make the indexing far
more trivial, I'm not comfortable creating a document from something
that is most definitely not an aggregate root.

Thoughts?

Tim

Ayende Rahien

unread,
Sep 22, 2011, 9:54:53 AM9/22/11
to rav...@googlegroups.com

tcoonfield

unread,
Sep 23, 2011, 11:02:16 AM9/23/11
to ravendb
Just what I needed. Thanks.

On Sep 22, 8:54 am, Ayende Rahien <aye...@ayende.com> wrote:
> See the discussion here:http://groups.google.com/group/ravendb/browse_thread/thread/9035b347c...
> >http://codeofrob.com/archive/2010/12/21/ravendb-document-design-with-...

tcoonfield

unread,
Sep 23, 2011, 12:48:00 PM9/23/11
to ravendb
So a followup...

I have created a working AbstractIndexCreationTask as follows:

public class SiteProduct_ByAttributeValues :
AbstractIndexCreationTask<SiteProduct>
{
public SiteProduct_ByAttributeValues()
{
Map = products => from product in products
select new
{
_ =
Project(product.AttributeValues,

attributeValue =>
new
Field(attributeValue.AttributeName, attributeValue.Value,
Field.Store.NO, Field.Index.ANALYZED))
};
}
}

That works very well, builds the index. But next I'm trying to wire
up a custom analyzer...or even a different Lucene analyzer. Say,
StandardAnalyzer. Analyzers.Add is there, but since the index is off
of a projection instead of the Product object itself, I'm not sure
what my lambda expression for the field name should look like.

So I'm at:

Analyzers.Add(x => ???, "CustomAnalyzer");

Ayende Rahien

unread,
Sep 23, 2011, 1:02:18 PM9/23/11
to rav...@googlegroups.com
No near the code now, but we have a way to specify analyzers per field per document, so you can extend the way ravendb works at that level, look at the classes in the Plugins namespace it is one of those
Reply all
Reply to author
Forward
0 new messages