Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting not_analyzed for a property in Nest 5.5.0

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
                                                        }
 (...)
like image 709
mikes Avatar asked Jul 27 '17 13:07

mikes


1 Answers

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.

like image 53
Russ Cam Avatar answered Nov 04 '22 03:11

Russ Cam