nest驱动IndexName问题

nest驱动IndexName问题

nest驱动IndexName问题

Elasticsearchcdzhoubin 发表了文章 • 1 个评论 • 2039 次浏览 • 2016-01-17 21:40 • 来自相关话题

nest驱动访问ES,按照官网文档,使用如下程序可以正常索引: static void Main()         {             var node = new Uri("http://localhost:9200&quot;);             var settings = new ConnectionSettings(                 node,                 defaultIndex: "my-application"             );             var client = new ElasticClient(settings);             var person = new Person             {                 Id = "1",                 Firstname = "Martijn",                 Lastname = "Laarman"             };             var index = client.Index(person);         } 调整为手动设置indexName时出错,示例代码如下: static void Main()         {             var node = new Uri("http://localhost:9200&quot;);             var settings = new ConnectionSettings(                 node,                 defaultIndex: "my-application"             );             settings.MapDefaultTypeIndices(d => d.Add(typeof(Person), "constIndex"));             var client = new ElasticClient(settings);             var person = new Person             {                 Id = "1",                 Firstname = "Martijn",                 Lastname = "Laarman"             };             var index = client.Index(person);         } 出错提示为: {StatusCode: 400,  Method: PUT,  Url: http://localhost:9200/constIndex/automobile/1,  Request: {   "firstname": "Martijn",   "lastname": "Laarman",   "id": "1" },  Response: <Response stream not captured or already read to completion by serializer, set ExposeRawResponse() on connectionsettings to force it to be set on>}    
using System;
using Nest;

namespace ConsoleApplication1
{
    class Program
    {
        private static IIndexResponse Index<T>(T person, string indexName) where T : class
        {
            var node = new Uri("http://localhost:9200&quot;);

            var settings = new ConnectionSettings(node,defaultIndex: indexName);
            var client = new ElasticClient(settings);
            return client.Index(person);
        }

        static void Main()
        {
            string indexNameError = typeof(Person).FullName
                .Substring(typeof(Person).FullName.LastIndexOf(".", StringComparison.Ordinal) + 1) + "Indexs";
            const string indexNameOk = "test";
            var person = new Person
            {
                Id = "1",
                Firstname = "Martijn",
                Lastname = "Laarman"
            };
            var ok = Index(person, indexNameOk);
            Console.WriteLine("Result:" + ok.IsValid);
            var error = Index(person, indexNameError);
            Console.WriteLine("Result:" + error.IsValid);
            Console.ReadKey();
        }
       
    }

   [Serializable]
    public class Person
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Id { get; set; }
    }
}
 

nest驱动IndexName问题

Elasticsearchcdzhoubin 发表了文章 • 1 个评论 • 2039 次浏览 • 2016-01-17 21:40 • 来自相关话题

nest驱动访问ES,按照官网文档,使用如下程序可以正常索引: static void Main()         {             var node = new Uri("http://localhost:9200&quot;);             var settings = new ConnectionSettings(                 node,                 defaultIndex: "my-application"             );             var client = new ElasticClient(settings);             var person = new Person             {                 Id = "1",                 Firstname = "Martijn",                 Lastname = "Laarman"             };             var index = client.Index(person);         } 调整为手动设置indexName时出错,示例代码如下: static void Main()         {             var node = new Uri("http://localhost:9200&quot;);             var settings = new ConnectionSettings(                 node,                 defaultIndex: "my-application"             );             settings.MapDefaultTypeIndices(d => d.Add(typeof(Person), "constIndex"));             var client = new ElasticClient(settings);             var person = new Person             {                 Id = "1",                 Firstname = "Martijn",                 Lastname = "Laarman"             };             var index = client.Index(person);         } 出错提示为: {StatusCode: 400,  Method: PUT,  Url: http://localhost:9200/constIndex/automobile/1,  Request: {   "firstname": "Martijn",   "lastname": "Laarman",   "id": "1" },  Response: <Response stream not captured or already read to completion by serializer, set ExposeRawResponse() on connectionsettings to force it to be set on>}    
using System;
using Nest;

namespace ConsoleApplication1
{
    class Program
    {
        private static IIndexResponse Index<T>(T person, string indexName) where T : class
        {
            var node = new Uri("http://localhost:9200&quot;);

            var settings = new ConnectionSettings(node,defaultIndex: indexName);
            var client = new ElasticClient(settings);
            return client.Index(person);
        }

        static void Main()
        {
            string indexNameError = typeof(Person).FullName
                .Substring(typeof(Person).FullName.LastIndexOf(".", StringComparison.Ordinal) + 1) + "Indexs";
            const string indexNameOk = "test";
            var person = new Person
            {
                Id = "1",
                Firstname = "Martijn",
                Lastname = "Laarman"
            };
            var ok = Index(person, indexNameOk);
            Console.WriteLine("Result:" + ok.IsValid);
            var error = Index(person, indexNameError);
            Console.WriteLine("Result:" + error.IsValid);
            Console.ReadKey();
        }
       
    }

   [Serializable]
    public class Person
    {
        public string Firstname { get; set; }
        public string Lastname { get; set; }
        public string Id { get; set; }
    }
}