C# ElasticSearch:为什么索引中的所有文本字段都有关键字类型?
我使用嵌套和带有属性的类来推送新文档。 下面是我如何定义类的:C# ElasticSearch:为什么索引中的所有文本字段都有关键字类型?,c#,elasticsearch,nest,C#,elasticsearch,Nest,我使用嵌套和带有属性的类来推送新文档。 下面是我如何定义类的: public class PatientNestModel { [Text] public string FirstName { get; set; } [Text] public string LastName { get; set; } [Text] public string MiddleName { get; set; }
public class PatientNestModel
{
[Text]
public string FirstName { get; set; }
[Text]
public string LastName { get; set; }
[Text]
public string MiddleName { get; set; }
[Date(Format = "dd-MM-yyyy")]
public DateTime BirthdayDate { get; set; }
[Keyword]
public string Gender { get; set; }
[Text]
public string Phone { get; set; }
[Nested]
public List<AdditionalContact> AdditionalContacts { get; set; }
[Boolean]
public bool Active { get; set; }
}
但我的索引元数据看起来是关键字类型
{
"state": "open",
"settings": {
"index": {
"creation_date": "1543806292300",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "3_J5ck_CTaCLEdhIbCC0ZQ",
"version": {
"created": "6030199"
},
"provided_name": "patients_esindex"
}
},
"mappings": {
"patientnestmodel": {
"properties": {
"firstName": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"lastName": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"gender": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"birthdayDate": {
"type": "date"
},
"phone": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"active": {
"type": "boolean"
},
"middleName": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
},
"aliases": [],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1
},
"in_sync_allocations": {
"0": [
"DCbu6-HvQT2ziCzhFZKU6A"
],
"1": [
"9SGADbBfSWuH7AanJUGgRA"
],
"2": [
"dPmhURTzTVWFV4z6Fh8ctw"
],
"3": [
"RHX67o0QQsueD6G67IXAkg"
],
"4": [
"aoBxi-i8Q1aVSeq1tT69Lw"
]
}
}
但是,只有在使用带有.keyword的术语时,我才能通过文本搜索找到所需的文档
我做错了什么?从ES 5.0开始,字符串字段分为两种新类型:文本(用于全文搜索)和关键字(用于关键字搜索) ,用于将属性映射应用于索引中的类型
如果已经创建了索引,您还可以使用.Map with.AutoMap为索引中的某个类型创建映射,但这只能在为任何文档编制索引之前完成。默认情况下,Elasticsearch将从索引的第一个文档推断映射。如果该类型已存在映射,则需要删除索引并重新开始,或者将这些文档重新索引到包含预期映射的新索引中。我理解这一点,但请查看我的嵌套类。我已经用文本类型定义了属性。主要问题是,为什么它们会成为ES中的关键词?我应该如何更改类以将它们作为文本索引?在将数据发送到索引之前,需要定义索引的映射。通过这种方式,您可以更好地控制字段的数据类型。@NishantSaini我尝试过这种方式,但每次在索引过程中都会收到下一个错误:拒绝将更新映射到[patients_esindex]由于最终映射将有多个类型,不幸的是,我无法在elasticsearch 6.x中解决此错误。您不能有多个映射类型。如果要更新映射中字段的数据类型,您所能做的就是删除并重新创建索引。警告:这样做会丢失当前数据,您必须再次对其重新编制索引。@NishantSaini我知道了,但如果我使用映射创建索引,则无法使用_esClient.Indexmodel,idx=>idx.Indexpatients\u esindex从c代码中添加新文档;似乎我的c代码中确实遗漏了一些东西
{
"state": "open",
"settings": {
"index": {
"creation_date": "1543806292300",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "3_J5ck_CTaCLEdhIbCC0ZQ",
"version": {
"created": "6030199"
},
"provided_name": "patients_esindex"
}
},
"mappings": {
"patientnestmodel": {
"properties": {
"firstName": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"lastName": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"gender": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"birthdayDate": {
"type": "date"
},
"phone": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"active": {
"type": "boolean"
},
"middleName": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
},
"aliases": [],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1,
"3": 1,
"4": 1
},
"in_sync_allocations": {
"0": [
"DCbu6-HvQT2ziCzhFZKU6A"
],
"1": [
"9SGADbBfSWuH7AanJUGgRA"
],
"2": [
"dPmhURTzTVWFV4z6Fh8ctw"
],
"3": [
"RHX67o0QQsueD6G67IXAkg"
],
"4": [
"aoBxi-i8Q1aVSeq1tT69Lw"
]
}
}