1

I'm developing a cross-platform app with xamarin.forms and I'm trying to look for a way to store a List of Objects directly into ElasticSearch so I can later search for results based on the objects of the lists. My scenario is the folloring:

public class Box {

    [String(Index = FieldIndexOption.NotAnalyzed)]
    public string id { get; set; }

    public List<Category> categories { get; set; }
}


public class Category {

    [String(Index = FieldIndexOption.NotAnalyzed)]
    public string id { get; set; }

    public string name { get; set; }
}

My aim is to be able to search for all the boxes that have a specific category.

I have tried to map everything properly like it says in the documentation but if I do it like that, when I store a box, it only stores the first category.

Is there actually a way to do it or is it just not possible with NEST?

Any tips are very welcome!

Thanks

1 Answer 1

1

It should just work fine with AutoMap using the code in the documentation:

If the index does not exist:

var descriptor = new CreateIndexDescriptor("indexyouwant") .Mappings(ms => ms .Map<Box>(m => m.AutoMap()) );

and then call something like:

await client.CreateIndexAsync(descriptor).ConfigureAwait(false);

or, when not using async:

client.CreateIndex(descriptor);

If the index already exists

Then forget about creating the CreateIndexDescriptor part above and just call:

await client.MapAsync<Box>(m => m.Index("existingindexname").AutoMap()).ConfigureAwait(false);

or, when not using async:

client.Map<Box>(m => m.Index("existingindexname").AutoMap());

Once you succesfully created a mapping for a type, you can index the documents.

Is it possible that you first had just one category in a box and mapped that to the index (Before you made it a List)? Because then you have to manually edit the mapping I guess, for example in Sense.

I don't know if you already have important data in your index but you could also delete the whole index (the mapping will be deleted too) and try it again. But then you'll lose all the documents you already indexed at the whole index.

Sign up to request clarification or add additional context in comments.

3 Comments

I deleted the index and did all this as the documentation says but, when I tried to store the List it only stored the first object of the list
Can you please show how your mapping looks like at the moment and the code where you index a Box?
After you've created index with the mapping you should be able to index documents with the following code: client.Index<Box>(refToObj, i => i.Index("index").Type("type").Id("id")); (for example)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.