1
votes

I'm using Nest to access ElasticSearch in C#. I've set up a structure that specifies ElasticProperty attributes to define the fields that are to be stored/indexed/sorted/etc. It all works great.

Now I've got a new requirement to save and search against additional "dynamic" data...

This new data works like this: my data structure (Item) can have multiple associated string labels, sorted into different sections. So a single Item might have Section1.Label1, Section1.Label2, Section2.Label1, and Section3.Label4.

If I were storing this in c#, I would probably use a Dictionary<string, List<string>> to store it. But in ElasticSearch, I also need to be able to search against these labels like "Give me all items that have Section2.Label1". The labels won't need be analyzed (just existence or not).

Thanks for any leads you can offer to tell Nest how to store such a data field.

UPDATE #1: It look like I'm probably looking at some type of [ElasticProperty(Type = FieldType.Nested)] rather than a Dynamic Mapping. Should I just use a Dictionary<string, List<string>> that's marked as a Nested type?

1
For that i think you must use some kind of nested objectsdanvasiloiu

1 Answers

1
votes

It looks like this works...

    [ElasticProperty(Index = FieldIndexOption.NotAnalyzed, Store = true, Type = FieldType.Nested, OmitNorms = true, IncludeInAll = false)]
    public Dictionary<string, List<string>> MyLabels { get; set; }

On the ElasticSearch side, it's a Map containing an Array of String. I still have to work on the Search side to come up with a query that will determine the existence of a particular label within a particular mapping, but that should be straightforward (knock on wood).