使用不带属性的GUID ID进行弹性搜索 [英] Elastic search with GUID ID without attributes

查看:118
本文介绍了使用不带属性的GUID ID进行弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在寻求从关系数据库切换到弹性搜索,我正在尝试建立一些基本代码并与Nest一起运行.我们已经有一些对象,这些对象使用guid作为ID,我想将其保存到弹性搜索索引中.

我不想添加任何特定的属性,因为该类用于不同的应用程序中,并且我不想向Nest添加不必要的依赖项.

现在我的代码如下:

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node) 
settings.DefaultIndex = "test";
var client = new ElasticClient(settings);

var testItem = new TestType { Id = Guid.NewGuid(), Name = "Test", Value = "10" };

var response = client.Index(testItem);

TestType为:

public class TestType
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Value { get; set; }
}

但是我收到如下错误:

ServerError:400Type:mapper_parsing_exception原因:无法执行 parse [id]"CausedBy:" Type:number_format_exception原因:对于 输入字符串:"c9c0ed42-86cd-4a94-bc86-a6112f4c9188""

我认为我需要指定一个映射来告诉服务器ID是一个字符串,但是如果不使用属性,我找不到任何有关如何执行此操作的示例或文档.

解决方案

假设您使用的是Elasticsearch 2.x和NEST 2.x(例如,撰写本文时,最新的是Elasticsearch 2.3.5和NEST 2.4.3. ),那么 NEST会自动推断默认情况下,来自Id属性的POCO的ID.如果是GUID id,则将其另存为字符串在Elasticsearch中.

这是一个让你前进的例子

void Main()
{
    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node)
        // default index to use if one is not specified on the request
        // or is not set up to be inferred from the POCO type
        .DefaultIndex("tests");

    var client = new ElasticClient(settings);

    // create the index, and explicitly provide a mapping for TestType
    client.CreateIndex("tests", c => c
        .Mappings(m => m
            .Map<TestType>(t => t
                .AutoMap()
                .Properties(p => p
                    // don't analyze ids when indexing,
                    // so they are indexed verbatim
                    .String(s => s
                        .Name(n => n.Id)
                        .NotAnalyzed()
                    )
                )
            )
        )
    );

    var testItem = new TestType { Id = Guid.NewGuid(), Name = "Test", Value = "10" };

    // now index our TestType instance
    var response = client.Index(testItem);
}

public class TestType
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Value { get; set; }
}

看看有关更多示例的自动映射文档,该示例如何明确映射POCO以控制规范分析仪 multi_fields 等.

We are looking to switch from a relational database to elastic search and I am trying to get some basic code up and running with Nest. We have existing objects which use guids for ids that I would like to save into an elastic search index.

I don't want to add any specific attributes as the class is used in different applications and I don't want to add unnecessary dependencies to Nest.

Right now my code looks like this:

var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(node) 
settings.DefaultIndex = "test";
var client = new ElasticClient(settings);

var testItem = new TestType { Id = Guid.NewGuid(), Name = "Test", Value = "10" };

var response = client.Index(testItem);

With TestType as:

public class TestType
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Value { get; set; }
}

However I get an error like:

ServerError: 400Type: mapper_parsing_exception Reason: "failed to parse [id]" CausedBy: "Type: number_format_exception Reason: "For input string: "c9c0ed42-86cd-4a94-bc86-a6112f4c9188""

I think I need to specify a mapping that tells the server the Id is a string, but I can't find any examples or documentation on how I do this without using the attributes.

解决方案

Assuming you're using Elasticsearch 2.x and NEST 2.x (e.g. latest of both at time of writing is Elasticsearch 2.3.5 and NEST 2.4.3), then NEST will automatically infer the id of a POCO by default from the Id property. In the case of a GUID id, this will be saved as a string in Elasticsearch.

Here's an example to get you going

void Main()
{
    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node)
        // default index to use if one is not specified on the request
        // or is not set up to be inferred from the POCO type
        .DefaultIndex("tests");

    var client = new ElasticClient(settings);

    // create the index, and explicitly provide a mapping for TestType
    client.CreateIndex("tests", c => c
        .Mappings(m => m
            .Map<TestType>(t => t
                .AutoMap()
                .Properties(p => p
                    // don't analyze ids when indexing,
                    // so they are indexed verbatim
                    .String(s => s
                        .Name(n => n.Id)
                        .NotAnalyzed()
                    )
                )
            )
        )
    );

    var testItem = new TestType { Id = Guid.NewGuid(), Name = "Test", Value = "10" };

    // now index our TestType instance
    var response = client.Index(testItem);
}

public class TestType
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Value { get; set; }
}

Take a look at the Automapping documentation for more examples of how to explicitly map a POCO for controlling norms, analyzers, multi_fields, etc.

这篇关于使用不带属性的GUID ID进行弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆