Elasticsearch NEST 2如何正确映射和使用嵌套类和批量索引

Ben*_*ose 6 nested elasticsearch nest

我需要回答三个主要问题。

  1. 如何正确映射和存储嵌套地图?
  2. 如何搜索文档的嵌套部分?
  3. 您如何批量索引?

我正在使用Nest版本2,并且一直在查看可以在此处找到的新文档。该文档在创建代码的某些部分时很有用,但不幸的是没有说明它们如何组合在一起。

这是我要映射的课程。

[ElasticsearchType(Name = "elasticsearchproduct", IdProperty = "ID")]
public class esProduct
{
    public int ID { get; set; }
    [Nested]
    public List<PriceList> PriceList { get; set; }
}

[ElasticsearchType(Name = "PriceList")]
public class PriceList
{
    public int ID { get; set; }
    public decimal Price { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和我的映射代码

var node = new Uri(HOST);
        var settings = new ConnectionSettings(node).DefaultIndex("my-application");

        var client = new ElasticClient(settings);
        var map = new CreateIndexDescriptor("my-application")
                        .Mappings(ms => ms
                            .Map<esProduct>(m => m
                                .AutoMap()
                                .Properties(ps => ps
                                    .Nested<PriceList>(n => n
                                        .Name(c => c.PriceList)
                                        .AutoMap()
                                    )
                                )
                            )
                        );

        var response = client.Index(map);
Run Code Online (Sandbox Code Playgroud)

这是我得到的答复:

Valid NEST response built from a succesful low level call on POST: /my-application/createindexdescriptor
Run Code Online (Sandbox Code Playgroud)

这样看来可行。下一个索引。

foreach (DataRow dr in ProductTest.Tables[0].Rows)
{
    int id = Convert.ToInt32(dr["ID"].ToString());
    List<PriceList> PriceList = new List<PriceList>();
    DataRow[] resultPrice = ProductPriceTest.Tables[0].Select("ID = " + id);

    foreach (DataRow drPrice in resultPrice)
    {
        PriceList.Add(new PriceList
        {
            ID = Convert.ToInt32(drPrice["ID"].ToString()),
            Price = Convert.ToDecimal(drPrice["Price"].ToString())    
        }

        esProduct product = new esProduct
        {
            ProductDetailID = id,
            PriceList = PriceList
        };

       var updateResponse = client.Update<esProduct>(DocumentPath<esProduct>.Id(id), descriptor => descriptor
                                    .Doc(product)
                                    .RetryOnConflict(3)
                                    .Refresh()
                );

       var index = client.Index(product);
    }
}
Run Code Online (Sandbox Code Playgroud)

同样,这似乎可行,但是当我进行搜索时,它确实按预期运行。

var searchResults = client.Search<esProduct>(s => s
                            .From(0)
                            .Size(10)
                                .Query(q => q
                                       .Nested(n => n
                                           .Path(p => p.PriceList)
                                            .Query(qq => qq
                                                .Term(t => t.PriceList.First().Price, 100)
                                                )
                                            )
                                      ));
Run Code Online (Sandbox Code Playgroud)

它确实返回结果,但是我期待

.Term(t => t.PriceList.First().Price, 100)
Run Code Online (Sandbox Code Playgroud)

看起来像

.Term(t => t.Price, 100)
Run Code Online (Sandbox Code Playgroud)

并且知道正在搜索嵌套的PriceList类,不是这种情况吗?

在新版本2文档中,我找不到批量索引部分。我尝试使用此代码

var descriptor = new BulkDescriptor();

***Inside foreach loop***

descriptor.Index<esProduct>(op => op
                            .Document(product)
                            .Id(id)
                            );
***Outside foreach loop***

var result = client.Bulk(descriptor);
Run Code Online (Sandbox Code Playgroud)

确实会返回成功响应,但是当我搜索时没有任何结果。

任何帮助,将不胜感激。

更新

在对@Russ进行更多调查后,建议我认为错误一定与我对带有嵌套对象的类的批量索引有关。

当我使用

var index = client.Index(product);
Run Code Online (Sandbox Code Playgroud)

为我可以使用的每个产品编制索引

var searchResults = client.Search<esProduct>(s => s
                    .From(0)
                    .Size(10)
                        .Query(q => q
                        .Nested(n => n
                            .Path(p => p.PriceList)
                            .Query(qq => qq
                                    .Term(t => t.PriceList.First().Price, 100)
                                )
                            )
                        )
                     );
Run Code Online (Sandbox Code Playgroud)

搜索并返回结果,但是当我批量索引时,它不再起作用,而是

var searchResults = client.Search<esProduct>(s => s
                    .From(0)
                    .Size(10)
                    .Query(q => q
                            .Term(t => t.PriceList.First().Price, 100)
                          )
                     );
Run Code Online (Sandbox Code Playgroud)

将有效,代码b不适用于单个索引方法。有人知道为什么会这样吗?

更新2

来自@Russ的建议表明,我已查看了映射。

我用来索引的代码是

var map = new CreateIndexDescriptor(defaultIndex)
                        .Mappings(ms => ms
                            .Map<esProduct>(m => m
                                .AutoMap()
                                .Properties(ps => ps
                                    .Nested<PriceList>(n => n
                                        .Name(c => c.PriceList)
                                        .AutoMap()
                                    )
                                )
                            )
                        );

        var response = client.Index(map);
Run Code Online (Sandbox Code Playgroud)

哪个正在发布

http://HOST/fresh-application2/createindexdescriptor {"mappings":{"elasticsearchproduct":{"properties":{"ID":{"type":"integer"},"priceList":{"type":"nested","properties":{"ID":{"type":"integer"},"Price":{"type":"double"}}}}}}}
Run Code Online (Sandbox Code Playgroud)

并调用HTTP:// HOST /新鲜应用2 / _all / _mapping漂亮吗?我得到

{
  "fresh-application2" : {
    "mappings" : {
      "createindexdescriptor" : {
        "properties" : {
          "mappings" : {
            "properties" : {
              "elasticsearchproduct" : {
                "properties" : {
                  "properties" : {
                    "properties" : {
                      "priceList" : {
                        "properties" : {
                          "properties" : {
                            "properties" : {
                              "ID" : {
                                "properties" : {
                                  "type" : {
                                    "type" : "string"
                                  }
                                }
                              },
                              "Price" : {
                                "properties" : {
                                  "type" : {
                                    "type" : "string"
                                  }
                                }
                              }
                            }
                          },
                          "type" : {
                            "type" : "string"
                          }
                        }
                      },
                      "ID" : {
                        "properties" : {
                          "type" : {
                            "type" : "string"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

fresh-application2返回的映射根本没有提到嵌套类型,我猜这是问题所在。

我的工作嵌套查询的映射看起来像这样

{
  "my-application2" : {
    "mappings" : {
      "elasticsearchproduct" : {
        "properties" : {
          "priceList" : {
            "type" : "nested",
            "properties" : {
              "ID" : {
                "type" : "integer"
              },
              "Price" : {
                "type" : "double"
              }
            }
          },
          "ID" : {
            "type" : "integer"
          },
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这具有返回的嵌套类型。我认为不返回嵌套类型的是当我开始使用.AutoMap()时,我使用正确吗?

更新

我已经解决了我的映射问题。我已将映射代码更改为

var responseMap = client.Map<esProduct>(ms => ms
                            .AutoMap()
                            .Properties(ps => ps
                                .Nested<PriceList>(n => n
                                    .Name(c => c.PriceList)
                                .AutoMap()
                                )
                            )
                        );
Run Code Online (Sandbox Code Playgroud)

Rus*_*Cam 2

在您进行开发时,我建议您注销对 Elasticsearch 的请求和响应,以便您可以看到使用 NEST 时发送的内容;这将使您更容易与主要的 Elasticsearch 文档相关,并确保请求和响应的正文符合您的期望(例如,对于映射、查询等有用)。

您所拥有的映射看起来不错,尽管您可以放弃这些属性,因为您使用的是流畅映射;将它们放在那里没有什么坏处,但在这种情况下它们很大程度上是多余的( 的类型名称是esProduct唯一适用的部分),因为.Properties()将覆盖从调用 . 中应用的推断或基于属性的映射.AutoMap()

在索引部分,您更新esProduct然后立即再次索引同一文档;我不确定这里的意图是什么,但更新调用对我来说看起来是多余的;索引调用将在更新后立即覆盖索引中给定 id 的文档(并且在刷新间隔后将在搜索结果中可见)。on .RetryOnConflict(3)the update将使用乐观并发控制来执行更新(这实际上是对集群内部文档的get then index操作,如果文档版本在getindex之间发生变化,则会尝试 3 次)。如果您用更新替换整个文档,即不是部分更新,那么冲突重试并不是真正必要的(并且根据前面的说明,示例中的更新调用看起来完全没有必要,因为索引调用将覆盖索引中具有给定 id 的文档)。

查询看起来正确nested您指定嵌套类型的路径,然后对嵌套类型上的字段的查询也将包含该路径。我将更新 NEST 嵌套查询使用文档以更好地演示

批量调用看起来不错;如果您需要索引大量文档,您可能希望批量发送文档,例如一次批量索引 500 个文档。一次批量调用发送多少个数据取决于多种因素,包括文档大小、分析方式、集群的性能,因此需要进行试验才能获得适合您情况的良好批量调用。

我会检查以确保您找到了正确的索引,该索引包含您期望的文档计数,并找到一个您知道其值为PriceList.Price100 的文档,并查看为其建立索引的内容。当您开始跑步时,使用Sense执行此操作可能会更快。