Setting not_analyzed for a property in Nest 5.5.0

萝らか妹 提交于 2019-12-14 03:46:01

问题


I have tried to set the "not_analyzed" index type via Nest 5.5.0 and I have no idea how to do it.

My init:

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

And the Project class:

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

This way of init creates some kind of weird mapping after I invoke the index/_mapping REST via Postman. There is the normal "mappings" JSON section, and just below "createindexdescriptor" with almost the same data.

"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:


To set a not analyzed string field within Elasticsearch 5.0+, you should use the keyword type, and pass the mapping either at index creation time with CreateIndex(), or before the first document is sent to the index using Map<T>(). In your case, I think you're looking for something like

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

I think the Id property should also be marked as a keyword type too.



来源:https://stackoverflow.com/questions/45352029/setting-not-analyzed-for-a-property-in-nest-5-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!