【问题标题】:Elasticsearch Nest 2.x index nested objectElasticsearch Nest 2.x 索引嵌套对象
【发布时间】:2016-04-15 01:30:20
【问题描述】:

我是 Elasticsearch 和 Nest 的新手,遇到了问题。我想要做的是创建一个索引并索引一个带有嵌套字段的文档。

    [ElasticsearchType]
public class TestType
{
    [Nest.String(Store = true, Index = FieldIndexOption.Analyzed )]
    public string  Text { get; set; }

    [Nested(IncludeInAll = true)]
    public List<NestedTestType> Nests { get; set; } = new List<NestedTestType>();

    public string Id { get; set; }      
}

[ElasticsearchType]
public class NestedTestType
{
    [Nest.String(Store = true, Index = FieldIndexOption.Analyzed)]
    public string Value { get; set; }

    [Nest.String(Store = false)]
    public string NotStoredValue { get; set; }
}

在函数中是

            var connectionPool = new Elasticsearch.Net.SniffingConnectionPool(poolUris);
        var settings = new ConnectionSettings(connectionPool);
        client = new ElasticClient(settings);

        string testIndexName = "test";
        var createIndeReponse = client.CreateIndex(testIndexName);
        var mappingResponse = client.Map<TestType>(m => m.Index(testIndexName).AutoMap());
       mappingResponse = client.Map<NestedTestType>(m => m.Index(testIndexName).AutoMap());

        TestType testData = new TestType() { Text = "Hello world" };
        testData.Nests.Add( new NestedTestType() { Value = "In the list", NotStoredValue = "Not stored"} );

        IndexRequest<TestType> indexRequest = new IndexRequest<TestType>(testIndexName, "test_type");
        indexRequest.Document = testData;
        IIndexResponse iir = client.Index(indexRequest);

但是,最后一行的 iir 包含错误“对象映射 [嵌套] 无法从嵌套更改为非嵌套”

我的问题是:

建立索引的正确方法是什么? 我在哪里可以找到对我有进一步帮助的文档?

【问题讨论】:

标签: elasticsearch nest


【解决方案1】:

一些观察:

  • TestTypeNestedTestType 的类型名称将从 CLR 类型名称推断。默认情况下,这些将是类型名称的驼峰式版本,即分别为 testTypenestedTestType

  • 由于NestedTestTypeTestType上的嵌套类型,所以不需要在索引中单独为其添加映射; NestedTestType 的映射是TestType 的映射的一部分

  • 您没有为TestTypeId 指定值; NEST will infer the id of the document from the Id property 将为空; Elasticsearch 可以很好地处理这个并为文档生成一个唯一的 id,将其存储在 _id 字段中,但是这个唯一的 id 不会针对 _source 中的 Id 属性设置,这意味着没有简单的方法使用 NEST 约定按 id 检索此文档。我建议为Id 设置一个值,并将该字段映射为not_analyzed

错误的原因是在索引TestType 时,您将类型名称指定为test_type,而不是显式指定testType 或让NEST 为您推断它。

当 Elasticsearch 看到传入的 json 文档时,它不会将其与之前创建的 TestType 映射关联,因为类型名称不匹配(testTypetest_type)因此尝试将nests 映射为对象。 但是,索引确实包含路径 nests 下的对象的嵌套映射,这会导致错误。

要解决,我们可以做

var connectionPool = new Elasticsearch.Net.SniffingConnectionPool(poolUris);
string testIndexName = "test";

var settings = new ConnectionSettings(connectionPool)
    // specify test to be the default index, meaning if no index name
    // is explicitly passed to a request or inferred from the type,
    // this will be the default
    .DefaultIndex(testIndexName);

var client = new ElasticClient(settings);

// create the index and add the mapping at the same time
var createIndeReponse = client.CreateIndex(testIndexName, c => c
    .Mappings(m => m
        .Map<TestType>(mm => mm.AutoMap())
    )
);

TestType testData = new TestType { Text = "Hello world", Id = "1" };
testData.Nests.Add(new NestedTestType { Value = "In the list", NotStoredValue = "Not stored" });

IIndexResponse iir = client.Index(testData);

如果你想指定TestType应该映射为类型名称test_type,你可以在连接设置上使用MapDefaultTypeNames

var settings = new ConnectionSettings(connectionPool)
    .DefaultIndex(testIndexName)
    .MapDefaultTypeNames(d => d
        .Add(typeof(TestType), "test_type")
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多