【问题标题】:ElasticSearch: Why all my text fields in the index have keyword type?ElasticSearch:为什么我在索引中的所有文本字段都有关键字类型?
【发布时间】:2018-12-04 07:42:51
【问题描述】:

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

我做错了什么?

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    从 ES 5.0 开始,字符串字段分为两种新类型:text,应该用于全文搜索,keyword,应该用于关键字搜索。

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

    【讨论】:

    • 我理解这一点,但请查看我的 NEST 课程。我已经定义了文本类型的属性。主要问题为什么它们成为 ES 中的关键字?我应该如何更改我的课程以将它们索引为文本?
    • 发送数据到index前需要定义index的映射。这样您就可以更好地控制字段的数据类型。
    • @NishantSaini 我尝试过这种方式,但每次在索引期间都会收到下一个错误:Rejecting mapping update to [patients_esindex] as the final mapping would have more than 1 type 不幸的是我无法解决此错误
    • 在 elasticsearch 6.x 中,您不能有多个映射类型。如果您想更新映射中字段的数据类型,您可以做的就是删除并重新创建索引。 警告: 这样做会丢失必须重新索引的当前数据。
    • @NishantSaini 我知道了,但是如果我使用映射创建索引,我就无法使用_esClient.Index(model, idx =&gt; idx.Index("patients_esindex")); 从 c# 代码添加新文档@ 似乎我的 c# 代码中肯定遗漏了一些东西跨度>
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多