Nest不是自动映射而不是分析字段

时间:2016-06-01 22:19:53

标签: elasticsearch nest

我创建了一个映射实体:

[ElasticsearchType(Name = "cases")]
public class Case
{

    [String(Name = "case_name")]
    public string CaseName { get; set; }

    [String(Name = "md5", Index = FieldIndexOption.NotAnalyzed)]
    public string Md5 { get; set; }

}

然后:

client.Map<Case>(mp => mp.AutoMap());

但是不包括md5字段映射的索引类型:

GET _mapping
{
  "myindex": {
    "mappings": {
      "cases": {
        "properties": {
          "case_name": {
            "type": "string"
          },
          "md5": {
            "type": "string"
          }
        }
      }
    }
  }
}

我错过了什么?

1 个答案:

答案 0 :(得分:1)

在针对新创建的索引运行时,您的代码适用于我

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "default-index";
    var connectionSettings = new ConnectionSettings(pool)
            .DefaultIndex(defaultIndex)
            .PrettyJson()
            .DisableDirectStreaming()
            .OnRequestCompleted(response =>
                {
                    // log out the request
                    if (response.RequestBodyInBytes != null)
                    {
                        Console.WriteLine(
                            $"{response.HttpMethod} {response.Uri} \n" +
                            $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}");
                    }
                    else
                    {
                        Console.WriteLine($"{response.HttpMethod} {response.Uri}");
                    }

                    Console.WriteLine();
                    // log out the response
                    if (response.ResponseBodyInBytes != null)
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n\n" +
                                 $"{Encoding.UTF8.GetString(response.ResponseBodyInBytes)}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                    else
                    {
                        Console.WriteLine($"Status: {response.HttpStatusCode}\n" +
                                 $"{new string('-', 30)}\n");
                    }
                });

    var client = new ElasticClient(connectionSettings);

    if (client.IndexExists(defaultIndex).Exists)
        client.DeleteIndex(defaultIndex);

    client.CreateIndex(defaultIndex);   
    client.Map<Case>(mp => mp.AutoMap());
    client.GetMapping<Case>();
}

[ElasticsearchType(Name = "cases")]
public class Case
{
    [String(Name = "case_name")]
    public string CaseName { get; set; }

    [String(Name = "md5", Index = FieldIndexOption.NotAnalyzed)]
    public string Md5 { get; set; }
}

在控制台中产生以下内容

HEAD http://localhost:9200/default-index?pretty=true

Status: 200


------------------------------

DELETE http://localhost:9200/default-index?pretty=true

Status: 200

{
  "acknowledged" : true
}

------------------------------

PUT http://localhost:9200/default-index?pretty=true 
{}

Status: 200

{
  "acknowledged" : true
}

------------------------------

PUT http://localhost:9200/default-index/cases/_mapping?pretty=true 
{
  "properties": {
    "case_name": {
      "type": "string"
    },
    "md5": {
      "type": "string",
      "index": "not_analyzed"
    }
  }
}

Status: 200

{
  "acknowledged" : true
}

------------------------------

GET http://localhost:9200/default-index/_mapping/cases?pretty=true

Status: 200

{
  "default-index" : {
    "mappings" : {
      "cases" : {
        "properties" : {
          "case_name" : {
            "type" : "string"
          },
          "md5" : {
            "type" : "string",
            "index" : "not_analyzed"
          }
        }
      }
    }
  }
}

------------------------------