c# - 如何去: Indexing a child for elasticsearch with c# and elasticsearch.网?

标签 c# .net visual-studio elasticsearch nest

我的 elasticsearch 数据库有以下映射:

{
    "mappings": {
        "customer": {
            "properties": {
                "name": {"type": "string"},
                "lastname": {"type": "string"},
                "age": {"type": "date"},
                "hometown": {"type": "string"},
                "street": {"type": "string"},
                "nr": {"type": "integer"}
          }
        },
        "purchase": {
            "_parent": {"type": "customer"},
            "properties": {
                "time": {"type": "long"},
                "productnr1": {"type": "integer"},
                "productnr2": {"type": "integer"},
                "productnr3": {"type": "integer"},
                "totalcost": {"type": "double"}
            }
        }
    }
}

现在我想在 visual studio 中使用客户端 (elasticsearch.net) 为我的 child (购买)编制索引。我可以毫无问题地索引和升级我的 parent (客户)。但我没有找到一种方法来索引 child 。

我得到了以下不完整的工作代码:

using Newtonsoft.Json;
using Elasticsearch.Net;

namespace programname
{ 
    static class program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]

        static void Main()
        {
            Class_GetData Class_GetData = new Class_GetData();

            //connecting to elasticsearch (**this is working**)
            var node = new Uri("http://localhost:9200");
            var config = new Elasticsearch.Net.Connection.ConnectionConfiguration(node);
            var client = new ElasticsearchClient(config);

            //some variables
            string username =  Class_GetData.Getname();
            string userlastname = Class_GetData.Getlastname();
            string userhometown = Class_GetData.Gethometown();
            string userstreet = Class_GetData.Getstreet()
            string userage = Class_GetData.Getage()
            int userhomenr = Class_GetData.Getnr();

            int userpruductnr1 = Class_GetData.Getproductnr1();
            int userpruductnr2 = Class_GetData.Getproductnr2();  
            int userpruductnr3 = Class_GetData.Getproductnr3();             
            double usertotalcost = Class_GetData.Gettotalcost();    

            var today = DateTime.Today;             
            var date = today.Year + "-" + today.Month + "-" + today.Day;

            var id = username + "_" + userlastname + "_" + date;

            //check if parent exist (this is also working)
            ElasticsearchResponse<DynamicDictionary> response = client.Get("test", "customer", id);
            dynamic found;
            response.Response.TryGetValue("found", out found);

            if (!found.HasValue)
            {
                return;
            }

            if (!found.Value)
            {
                var customer = new
                {
                name = username,
                lastname = userlastname,
                age = userage,
                hometown = userhometown,
                street = userstreet,
                nr = userhomenr,
                };

                client.Index("test", "customer", id, customer);
            }

            //**maybe this is already false?**
            var purchaseproperties = new
            {
                time = DateTime.Now.Ticks,
                productnr1 = userpruductnr1,
                productnr2 = userpruductnr2,
                productnr3 = userpruductnr3,
                totalcost = usertotalcost,
            };

            //**this is the main problem... child wont create**
            client.Index("test/customer/", "purchase", purchaseproperties); 

            //working line:
            client.Index("test", "purchase", purchaseproperties, d => d.Parent(_parentId));

所以现在我希望这段代码是 self 解释的,有人可以帮助我。我真的试图让它成为最简单的方法,并且我尝试了很多其他不起作用的索引行。我也希望有一个解决方案可以在不使用 visual studio 的 nest-client 的情况下将 child 索引到 parent 。

最佳答案

我相信您在索引子文档时缺少父 ID 参数:

client.Index("test", "purchase", purchaseproperties, d => d.Parent(_parentId));

另请注意,我已将 Index(..) 方法中的第一个参数从 test/customer 更改为 test,您必须在此参数中仅指定索引名称,而不指定索引类型。

希望对您有所帮助。

关于c# - 如何去: Indexing a child for elasticsearch with c# and elasticsearch.网?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33092024/

相关文章:

c# - 在 foreach 元素内添加空格

asp.net - 我应该在 2016 年为 .Net 使用哪个 ORM 来与 SQL 服务器通信?

c# - 为什么在等待任务返回方法时出现 InvalidCastException?

c# - 为Elasticsearch日期字段提供空值

ASP.NET 项目大小

c# - 如何从另一个类库引用的类库中自动复制嵌入资源?

c# - 用于重新格式化文本的免费 Visual-studio 插件

c# - 方法创建困境

c# - 通用命名空间命名建议

c# - 像: palette[ val & 3 ]?这样C的二维数组访问的C#模拟是什么