ElasticSearch:为什么索引中的所有文本字段都具有关键字类型?

时间:2018-12-03 03:13:27

标签: c# elasticsearch nest

我使用带有属性的类配对的NEST推送新文档。 这是我定义类的方法:

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; }
    }

这是我的推送方式:

var response = _esClient.Index(model, idx => idx.Index("patients_esindex"));

但是随后我的索引元数据看起来带有关键字类型。

{
    "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一起使用时,我才能通过文本搜索找到所需的文档

我做错了什么?

2 个答案:

答案 0 :(得分:1)

从ES 5.0开始,字符串字段已分为两种新类型:文本(用于全文搜索)和关键字(用于关键字搜索)。

https://www.elastic.co/blog/strings-are-dead-long-live-strings

答案 1 :(得分:0)

With Attribute mapping, you must call AutoMap() when creating the index,用于从属性映射到索引中的类型。

如果已经创建了索引,则也可以将.Map<T>().AutoMap()一起使用来为索引中的类型创建映射,但这只能在索引任何文档之前完成(默认情况下) ,Elasticsearch将根据索引的第一个文档推断映射。如果该类型已经存在映射,则您将需要删除索引并重新开始,或者将这些文档重新索引到包含预期映射的新索引中。