[英]Elasticsearch Nest 2.x index nested object
我是Elasticsearch和Nest的新手,遇到了问题。 我想要做的是创建索引并使用嵌套字段索引doc。
[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包含错误“对象映射[嵌套]无法从嵌套更改为非嵌套”
我的问题是:
索引的正确方法是什么? 我在哪里可以找到有助于我进一步帮助的文档?
一些观察:
TestType
和NestedTestType
的类型名称将从CLR类型名称推断出来。 默认情况下,这些将是类型名称的驼峰式版本,即testType
和nestedTestType
。
由于NestedTestType
是NestedTestType
上的嵌套类型, TestType
您无需在索引中为其单独添加映射; NestedTestType
的映射是TestType
映射的TestType
您没有为TestType
的Id
指定值; NEST将从Id
属性推断文档的id,该属性为null; Elasticsearch对此很好,并为文档生成一个唯一的id,将其存储在_id
字段中,但是这个唯一的id不会针对_source
中的Id
属性设置,这意味着没有简单的方法可以通过id使用id检索这个文档NEST惯例。 我建议为Id
设置一个值,并将字段映射为not_analyzed
出错的原因是,在索引TestType
,您将类型名称指定为test_type
,而不是显式指定testType
或只是让NEST为您推断它。
当Elasticsearch看到json文档进入时,它不会将它与之前创建的TestType
的映射TestType
,因为类型名称不匹配( testType
与test_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")
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.