nest驱动访问ES,按照官网文档,使用如下程序可以正常索引:
static void Main()
{
var node = new Uri("http://localhost:9200");
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");
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>}
 
 
 
                                
                                
                                
                                
                                static void Main()
{
var node = new Uri("http://localhost:9200");
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");
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");
            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; }
    }
}
                                [尊重社区原创,转载请保留或注明出处]
本文地址:http://searchkit.cn/article/55
                                本文地址:http://searchkit.cn/article/55
1 个评论
                                            已经解决,indexName不支持大写                                        
                                     
                                        