c# - ElasticSearch-Nest v7.x "Ignore = true" 不处理字段

c# - ElasticSearch-Nest v7.x "Ignore = true" not working on fields

我正在尝试对 POCO class 中的某些字段进行索引并将某些属性装饰为 "Ignore = true",这些字段不应被索引但应被存储。我希望这些字段出现在搜索结果中,但不应该是索引。

我正在尝试对几个应该被索引的字段进行映射,并忽略所有其他具有 "Ignore = true" 作为 Nest Library 提供的装饰器的字段。

这里是POCO的例子class。

[PropertyName("doi")]
public string Doi { get; set; }

[PropertyName("displayName")]
public string DisplayName { get; set; }

[PropertyName("itemType")]
public string ItemType { get; set; }

[PropertyName("fileReference")]
public string FileReference { get; set; }

[PropertyName("textFirstPage", Ignore = true)]
public string TextFirstPage { get; set; }

[PropertyName("textLastPage", Ignore = true)]
public string TextLastPage { get; set; }

[PropertyName("citationTitle", Ignore = true)]
public string CitationTitle { get; set; }

[PropertyName("translatedPublicationTitle", Ignore = true)]
public string TranslatedPublicationTitle { get; set; }

[PropertyName("alternatePublicationTitle", Ignore = true)]
public string AlternatePublicationTitle { get; set; }

[PropertyName("publicationSubTitle", Ignore = true)]
public string PublicationSubTitle { get; set; }

但是当我尝试查看索引的映射时,POCO class 中提到的所有字段都出现在映射中。

{
    "cweeindex" : {
        "mapping": {
            "properties" : {
                "doi": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "displayName": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fileReference": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "itemType": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                 "citationTitle": {
                    "type": "boolean"
                },
                "publicationSubTitle": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "textFirstPage": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "textLastPage": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "translatedPublicationSubTitle": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "translatedPublicationTitle": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
            }   
        }
    }
}

更新!! 映射的嵌套代码如下

var createIndexResponse = _connectionToEs.EsClient().Indices.Create("cweeindex", c => c
                                                    .Map<EsStandardContract>(m => m.AutoMap())
                                                );

拜托,让我做错什么!!提前致谢。

这看起来像是从 6.x 到 7.x 的序列化变化带来的回归。我已经打开an issue地址。

目前,您可以使用 Nest.IgnoreAttribute。例如

[PropertyName("doi")]
public string Doi { get; set; }

[PropertyName("displayName")]
public string DisplayName { get; set; }

[PropertyName("itemType")]
public string ItemType { get; set; }

[PropertyName("fileReference")]
public string FileReference { get; set; }

[Ignore]
public string TextFirstPage { get; set; }

[Ignore]
public string TextLastPage { get; set; }

[Ignore]
public string CitationTitle { get; set; }

[Ignore]
public string TranslatedPublicationTitle { get; set; }

[Ignore]
public string AlternatePublicationTitle { get; set; }

[Ignore]
public string PublicationSubTitle { get; set; }

感谢您的帮助。

由于 [Ignore] 装饰器不允许在映射中添加字段,因此它在索引时也忽略了属性。我想要的是有一个包含某些字段的映射,但是在索引时创建的文档应该包含一些其他有价值的字段,这些字段将在输出中获取结果(更像是投影字段)时使用。我不希望动态包含这些字段,所以我创建了一个映射 Class 对象,该对象具有要包含在索引映射中的所有属性并设置为 dynamic=false.

然后在索引时我使用另一个 class 对象,它具有映射中包含的所有属性以及一些其他属性。这些其他属性将在获取 results/output 时使用,但我不希望这些属性可搜索或可过滤(意思是,不想索引)。

这是一个映射class

 public class EsMappingContract
{
    [PropertyName("doi")]
    public string Doi { get; set; }

    [PropertyName("displayName")]
    public string DisplayName { get; set; }

    [PropertyName("itemType")]
    public string ItemType { get; set; }

    [PropertyName("fileReference")]
    public string FileReference { get; set; }
}

这是索引Class

public class EsIndexingContract
{
    [PropertyName("doi")]
    public string Doi { get; set; }

    [PropertyName("displayName")]
    public string DisplayName { get; set; }

    [PropertyName("itemType")]
    public string ItemType { get; set; }

    [PropertyName("fileReference")]
    public string FileReference { get; set; }

    [PropertyName("citationTitle", Ignore = true)]
    public string CitationTitle { get; set; }

    [PropertyName("translatedPublicationTitle", Ignore = true)]
    public string TranslatedPublicationTitle { get; set; }

    [PropertyName("alternatePublicationTitle", Ignore = true)]
    public string AlternatePublicationTitle { get; set; }

    [PropertyName("publicationSubTitle", Ignore = true)]
    public string PublicationSubTitle { get; set; }
}

我不知道 [Ignore] 也会在索引时忽略属性,因此我不得不采用这种方法。我会喜欢其他一些装饰器,它们会指示 ES 在创建映射时忽略,但如果属性有价值,则不应在索引时忽略。

再次感谢您的帮助!!希望这对以后的人有所帮助。