Reputation: 81
In Raven I am saving json documents, I wish to make an index that makes an object array in this json searchable. An example from this is the following json structure.
"OrderProperties": [
{
"Key": "UserProfileType",
"Value": "ClubMember"
},
{
"Key": "FirstTimeReferer",
"Value": "some value"
}
]
I have tried to make an index that would work for it in raven studio. First I tried to do:
from doc in docs.WebOrderModels
select new {
OrderProperties = doc.OrderProperties
}
Which did not work so I tried with the following
from doc in docs.WebOrderModels
select new {
OrderProperties_Key = doc.OrderProperties.Key,
OrderProperties_Value = doc.OrderProperties.Value,
}
However this still leaves the objects un-indexed so I cannot filter a search based on this. Is there a way where I can create an index that makes the key, value pairs from json searchable?
Upvotes: 2
Views: 545
Reputation: 8934
I think you're after Dynamic fields in index where the idea is to generate new custom index terms for dictionaries and nested structures.
For your example, the generated index terms (aka dynamic fields) would be:
{
"OrderProperties_UserProfileType": "ClubMember",
"OrderProperties_FirstTimeReferer": "some value"
}
that can be accessed
RQL
as from index 'Y' where OrderProperties_UserProfileType = 'ClubMember'
(looks quite obvious),C#
as s.Query<X,Y>().Where(p => p.OrderProperties["UserProfileType"].Equals("ClubMember"))
(use it as a dictionary).Here is a blog post on the topic - Power of Dynamic fields for indexing dictionaries and collections in RavenDB. It has plenty of examples and you can find even more at YABT project on GitHub.
Upvotes: 0
Reputation: 81
I am posting this answer so that it might be able to help others.
The index I ended up creating looks like this:
from doc in docs.WebOrderModels
from prop in doc.OrderProperties
where prop.Key == "ShippingMethodAlias"
select new {
Value = prop.Value
}
The where statement is there to reduce the fan out as much as possible.
Upvotes: 2