簡體   English   中英

EEST的ElasticSearch索引/插入失敗

[英]ElasticSearch Index/Insert with NEST fail

我正在嘗試將一些JSON數據插入到彈性搜索中進行測試。

這是代碼:

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

    var client = new ElasticClient(settings);

    var myJson = @"{ ""hello"" : ""world"" }";
    var response = client.Index(myJson, i => i.Index("FormId")
                        .Type("resp")
                        .Id((int)r.id)
                        .Refresh()
                        );

沒有插入任何內容,我從ES收到以下錯誤:{在PUT上對無效的低級別調用構建的無效NEST響應:/ FormId / resp / 1?refresh = true}

我試圖找到一些例子,但都使用預定義的數據結構,而我想使用非結構化數據的JSON數據。

以上錯誤消息來自NEST。 彈性回復(並在日志中寫入)以下消息:MapperParsingException [無法解析]; nested- NotXContentException [壓縮器檢測只能在某些xcontent字節或壓縮的xcontent字節上調用];

無法解析{“”你好“”:“”世界“”} ????

一些觀察:

  • 索引名稱必須小寫
  • 雖然可以在配置中關閉此功能,但在將文檔編入索引時將自動創建索引。 如果您還想控制文檔的映射,最好先創建索引。
  • 使用匿名類型來表示您希望發送的json(您可以使用低級客戶端發送json字符串,即client.LowLevel如果您願意,但使用匿名類型可能更容易)。
  • 響應中的.DebugInformation應該包含請求失敗原因的所有詳細信息

這是一個演示如何入門的示例

void Main()
{
    var node = new Uri("http://localhost:9200");
    var settings = new ConnectionSettings(node)
    // lower case index name
    .DefaultIndex("formid");

    var client = new ElasticClient(settings);

    // use an anonymous type
    var myJson = new { hello = "world" };

    // create the index if it doesn't exist
    if (!client.IndexExists("formid").Exists)
    {
        client.CreateIndex("formid");
    }

    var indexResponse = client.Index(myJson, i => i
        .Index("formid")
        .Type("resp")
        .Id(1)
        .Refresh()
    );
}

現在,如果我們向http://localhost:9200/formid/resp/1發出GET請求,我們將返回文檔

{
   "_index": "formid",
   "_type": "resp",
   "_id": "1",
   "_version": 1,
   "found": true,
   "_source": {
      "hello": "world"
   }
}

暫無
暫無

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

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