在Nest 5.5.0中为属性设置not_analyzed

时间:2017-07-27 13:19:44

标签: c# elasticsearch mapping nest elasticsearch-analyzers

我试图设置" not_analyzed"通过Nest 5.5.0索引类型,我不知道该怎么做。

我的初学者:

var map = new CreateIndexDescriptor(INDEX_NAME)
     .Mappings(ms => ms.Map<Project>(m => m.AutoMap()));

var connectionSettings = new ConnectionSettings().DefaultIndex(INDEX_NAME);
_client = new ElasticClient(connectionSettings);

_client.Index(map);

项目类:

[ElasticsearchType(Name = "project")]
public class Project
{
    public Guid Id { get; set; }
    [Text(Analyzer = "not_analyzed")]
    public string OwnerIdCode { get; set; }
}

这种init方式在我通过Postman调用索引/ _mapping REST之后创建了某种奇怪的映射。有正常的&#34;映射&#34; JSON部分,正好在&#34; createindexdescriptor&#34;几乎相同的数据。

"examinations4": {
    "mappings": {
        "project": {
            (...)
        },
        "createindexdescriptor": {
            "properties": {
                "mappings": {
                    "properties": {
                        "project": {
                            "properties": {
                                "properties": {
                                    "properties": {
                                        "id": {
                                            "properties": {
                                                "type": {
                                                    "type": "text",
                                                    "fields": {
                                                        "keyword": {
                                                            "type": "keyword",
                                                            "ignore_above": 256
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        "ownerIdCode": {
                                            "properties": {
                                                "analyzer": {
                                                    "type": "text",
                                                    "fields": {
                                                        "keyword": {
                                                            "type": "keyword",
                                                            "ignore_above": 256
                                                        }
                                                    }
                                                },
                                                "type": {
                                                    "type": "text",
                                                    "fields": {
                                                        "keyword": {
                                                            "type": "keyword",
                                                            "ignore_above": 256
                                                        }
 (...)

1 个答案:

答案 0 :(得分:6)

要在Elasticsearch 5.0+中设置未分析的字符串字段,您应该使用keyword type,并在索引创建时使用CreateIndex()或在将第一个文档发送到索引使用Map<T>()。在你的情况下,我认为你正在寻找像

这样的东西
void Main()
{
    var connectionSettings = new ConnectionSettings()
        .DefaultIndex("default-index");

    var client = new ElasticClient(connectionSettings);

    client.CreateIndex("projects", c => c
        .Mappings(m => m
            .Map<Project>(mm => mm
                .AutoMap()
            )
        )
    );
}

[ElasticsearchType(Name = "project")]
public class Project
{
    [Keyword]
    public Guid Id { get; set; }

    [Keyword]
    public string OwnerIdCode { get; set; }
}

我认为Id属性也应该被标记为关键字类型。