My documents have a quite deep structure with multilangual options. How would I go about creating a View for this document? Note: I want the “text” properties within the nested “depotProperties → LocalizedNames” to be searchable (queryable).
I have a collection of depots. The depots can be filtered by the user. The fields we want to be queryable/searchable are the various “depotProperties” (manufacturer, etc). I.e. we need to make the nested “text” fields indexed so we can query them. We don’t know how to set up a view for this…
private void RegisterIndexes()
{
_elementRepository.GetView()
.SetMap((doc, emit) =>
{
if (!doc.ContainsKey("type"))
return;
if (!doc["type"].Equals("Depot"))
return;
if (!doc.ContainsKey("localizedNames"))
return;
var localizedNames = doc["localizedNames"] as JArray;
if (localizedNames == null)
return;
foreach (var localizedName in localizedNames)
{
emit(localizedName["text"], null);
}
}, "1.10");
}
When I do a query on this (for example; setting the Start- and EndKey to “Manufacturer”) I get the correct documents.
However, when I chose to fetch/query all documents of the type “Depot”, I get one object per emitted, i.e. I get duplicates (“Manufacturer” and “Hersteller” returns in two different objects, even though they belong in the same document).
Is it because my View is faulty, or is this the correct behaviour?
I suppose that if we would try one row per document, then we can’t emit in a for-loop…
Also, one more question on the subject; we want to have one more field indexed. Should we create a new view for that purpose, or can we emit more in the same view? What is the suggested way?
Does this mean I can’t query a number of different fields in a single search query? I would like to include the nested localizedName text properties in the search as well.
You can include as much data as you want in the value you emit. But a query does a scan of a single index, which has to be sorted by a single criterion, with one key as the primary, and optional secondary, etc. keys (if you use an array.)