2016-04-15 131 views
1

我是Elasticsearch和Nest中的新成员,并且在问题中被阻止。我想要做的是创建一个索引并索引带有嵌套字段的文档。Elasticsearch Nest 2.x索引嵌套对象

[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包含错误“对象映射[巢]不能改变从嵌套到非嵌套”

我的问题是:

什么是做索引的正确方法? 我在哪里可以找到有助于我进一步研究的文档?

+0

您可能感兴趣的索引映射(http:// localhost:9200/test/_mapping)? – Rob

回答

3

几个意见:

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

  • 由于NestedTestTypeTestType的嵌套类型,因此您不需要在索引中单独添加映射;的NestedTestType映射是TestType

  • 映射的一部分不指定为TestTypeId的值; NEST will infer the id of the document from the Id property这将是空的; Elasticsearch会很好,并为文档生成一个唯一的ID,将其存储在_id字段中,但该唯一标识不会针对_source中的Id属性进行设置,这意味着将不存在通过id检索此文档的简单方法NEST约定。我建议对Id设定的值,也映射字段not_analyzed

的原因错误是索引TestType时,您所指定的类型名称是test_type而不是明确指定testType或只是让NEST为你推断它。

当Elasticsearch看到未来的JSON文件,它不会将其与早前因为类型名称创建不匹配(testTypetest_type),从而试图映射nests为对象TestType映射关联。 但是,索引确实包含路径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") 
    );