本文介绍了如何使用NEST 2.x将一个c#对象列表存储到ElasticSearch中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用xamarin.forms开发一个跨平台的应用程序,我正在寻找一种将对象列表直接存储到ElasticSearch中的方式,以便随后可以根据列表的对象搜索结果。我的场景是这样的:

  public class Box {

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

public List< Category>类别{get;组;
}


public class Category {

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

public string name {get;组; }
}

我的目标是能够搜索所有包含特定的类别。



我已经尝试映射一切,就像在,但是如果我这样做,当我存储一个框时,它只存储第一个类别。



有没有办法做到这一点,还是NEST不可能?



任何提示都是非常欢迎!



谢谢

解决方案

AutoMap使用文档中的代码:



如果索引不存在:



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



,然后调用以下内容:



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



或者不使用async时:



client.CreateIndex(descriptor);



如果索引已经存在



然后忘记创建上面的CreateIndexDescriptor部分,只需调用:



等待client.MapAsync< Box> ;(m => m.Index(existingindexname)AutoMap())。ConfigureAwait(false);



或者不使用async时: p>

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



一旦你成功创建了一个类型的映射,你可以索引文档。



是否可能您首先在一个框中只有一个类别,并将其映射到索引(在将其列为列表之前)?因为那你必须手动编辑映射我猜,例如在Sense。



我不知道您的索引中是否已经有重要数据,但也可以删除整个索引(映射也将被删除),然后重试。但是,您将丢失已经在整个索引中编入索引的所有文档。


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

解决方案

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.

这篇关于如何使用NEST 2.x将一个c#对象列表存储到ElasticSearch中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:04