簡體   English   中英

Elasticsearch Nest 2.x索引嵌套對象

[英]Elasticsearch Nest 2.x index nested object

我是Elasticsearch和Nest的新手,遇到了問題。 我想要做的是創建索引並使用嵌套字段索引doc。

    [ElasticsearchType]
public class TestType
{
    [Nest.String(Store = true, Index = FieldIndexOption.Analyzed )]
    public string  Text { get; set; }

    [Nested(IncludeInAll = true)]
    public List<NestedTestType> Nests { get; set; } = new List<NestedTestType>();

    public string Id { get; set; }      
}

[ElasticsearchType]
public class NestedTestType
{
    [Nest.String(Store = true, Index = FieldIndexOption.Analyzed)]
    public string Value { get; set; }

    [Nest.String(Store = false)]
    public string NotStoredValue { get; set; }
}

它在功能中

            var connectionPool = new Elasticsearch.Net.SniffingConnectionPool(poolUris);
        var settings = new ConnectionSettings(connectionPool);
        client = new ElasticClient(settings);

        string testIndexName = "test";
        var createIndeReponse = client.CreateIndex(testIndexName);
        var mappingResponse = client.Map<TestType>(m => m.Index(testIndexName).AutoMap());
       mappingResponse = client.Map<NestedTestType>(m => m.Index(testIndexName).AutoMap());

        TestType testData = new TestType() { Text = "Hello world" };
        testData.Nests.Add( new NestedTestType() { Value = "In the list", NotStoredValue = "Not stored"} );

        IndexRequest<TestType> indexRequest = new IndexRequest<TestType>(testIndexName, "test_type");
        indexRequest.Document = testData;
        IIndexResponse iir = client.Index(indexRequest);

但是,最后一行中的iir包含錯誤“對象映射[嵌套]無法從嵌套更改為非嵌套”

我的問題是:

索引的正確方法是什么? 我在哪里可以找到有助於我進一步幫助的文檔?

一些觀察:

  • TestTypeNestedTestType的類型名稱將從CLR類型名稱推斷出來。 默認情況下,這些將是類型名稱的駝峰式版本,即testTypenestedTestType

  • 由於NestedTestTypeNestedTestType上的嵌套類型, TestType您無需在索引中為其單獨添加映射; NestedTestType的映射是TestType映射的TestType

  • 您沒有為TestTypeId指定值; NEST將從Id屬性推斷文檔的id,該屬性為null; Elasticsearch對此很好,並為文檔生成一個唯一的id,將其存儲在_id字段中,但是這個唯一的id不會針對_source中的Id屬性設置,這意味着沒有簡單的方法可以通過id使用id檢索這個文檔NEST慣例。 我建議為Id設置一個值,並將字段映射為not_analyzed

出錯的原因是,在索引TestType ,您將類型名稱指定為test_type ,而不是顯式指定testType或只是讓NEST為您推斷它。

當Elasticsearch看到json文檔進入時,它不會將它與之前創建的TestType的映射TestType ,因為類型名稱不匹配( testTypetest_type ),因此嘗試將nests映射為對象。 但是 ,索引確實包含已經存在於路徑nests下的對象的嵌套映射,這會引起錯誤。

要解決,我們可以做到

var connectionPool = new Elasticsearch.Net.SniffingConnectionPool(poolUris);
string testIndexName = "test";

var settings = new ConnectionSettings(connectionPool)
    // specify test to be the default index, meaning if no index name
    // is explicitly passed to a request or inferred from the type,
    // this will be the default
    .DefaultIndex(testIndexName);

var client = new ElasticClient(settings);

// create the index and add the mapping at the same time
var createIndeReponse = client.CreateIndex(testIndexName, c => c
    .Mappings(m => m
        .Map<TestType>(mm => mm.AutoMap())
    )
);

TestType testData = new TestType { Text = "Hello world", Id = "1" };
testData.Nests.Add(new NestedTestType { Value = "In the list", NotStoredValue = "Not stored" });

IIndexResponse iir = client.Index(testData);

如果要指定TestType應映射為類型名稱test_type ,則可以在連接設置上使用MapDefaultTypeNames

var settings = new ConnectionSettings(connectionPool)
    .DefaultIndex(testIndexName)
    .MapDefaultTypeNames(d => d
        .Add(typeof(TestType), "test_type")
    );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM