2012-11-30 38 views
6

我總是彈性新,所以我的知識只是從elasticsearch網站,我需要幫助。 我的任務是將HTML格式的大行數據索引爲彈性搜索。我已經抓取我的數據並將其存儲到磁盤(200 000個html文件)。我的問題是什麼是索引所有html文件到elasticsearch最簡單的方法?我應該爲每個文件做一些手工操作,以使請求具有彈性嗎?例如像:如何將html文件的轉儲索引到elasticsearch?

curl -XPUT 'http://localhost:9200/registers/tomas/1' -d '{ 
    "user" : "tomasko", 
    "post_date" : "2009-11-15T14:12:12", 
    "field 1" : "field data" 
    "field 2" : "field 2 data" 
}' 

而第二個問題是,如果我要解析HTML文檔檢索JSON場1數據,如在示例代碼了嗎?

並最終索引後,我可以刪除所有的HTML文件?感謝所有。

回答

3

我會看看bulk api,它允許您發送多個文檔在一個請求中,以加快您的索引過程。您可以發送一批10,20或更多文件,具體取決於它們的大小。

取決於你想要索引什麼,你可能需要解析html,除非你想把整個html索引爲一個字段(在這種情況下你可能想用html strip char filter去掉html標籤索引文本)。

建立索引後,我建議確保映射是正確的,你可以找到你要找的東西。您可以使用彈性搜索存儲在底層的_source專業領域進行重新索引,但是如果您已經編寫了索引器代碼,則可能需要在需要時再次使用索引器代碼(當然使用相同的html文檔)。在實踐中,你從來沒有索引你的數據一次......所以要小心:)即使elasticsearch總是幫助你用_source字段),這只是查詢現有索引並將其所有文檔重新索引到另一個索引的問題。

0

@ javanna建議您看看Bulk API,肯定會引導您朝着正確的方向發展。如果您使用的是NEST,則可以將所有對象存儲在列表中,然後您可以序列化JSON對象以索引內容。

具體來說,如果您想在索引和存儲內容之前剝離html標籤,您可以使用映射器附件插件 - 在定義映射時,您可以將content_type分類爲「html 「。

映射器附件對許多事情很有用,特別是如果您處理多種文檔類型,但最值得注意的是 - 我相信只是使用它來剝離html標籤就足夠了(您無法使用html_strip字符過濾器)。

只是一個預警 - 沒有任何html標籤將被存儲。因此,如果您確實需要這些標籤,我會建議定義另一個字段來存儲原始內容。另一個注意事項:您無法爲映射器附件文檔指定多個字段,因此您需要將其存儲在映射器附件文檔之外。看到我下面的工作示例。

你需要導致這種映射:

{ 
    "html5-es" : { 
    "aliases" : { }, 
    "mappings" : { 
     "document" : { 
     "properties" : { 
      "delete" : { 
      "type" : "boolean" 
      }, 
      "file" : { 
      "type" : "attachment", 
      "fields" : { 
       "content" : { 
       "type" : "string", 
       "store" : true, 
       "term_vector" : "with_positions_offsets", 
       "analyzer" : "autocomplete" 
       }, 
       "author" : { 
       "type" : "string", 
       "store" : true, 
       "term_vector" : "with_positions_offsets" 
       }, 
       "title" : { 
       "type" : "string", 
       "store" : true, 
       "term_vector" : "with_positions_offsets", 
       "analyzer" : "autocomplete" 
       }, 
       "name" : { 
       "type" : "string" 
       }, 
       "date" : { 
       "type" : "date", 
       "format" : "strict_date_optional_time||epoch_millis" 
       }, 
       "keywords" : { 
       "type" : "string" 
       }, 
       "content_type" : { 
       "type" : "string" 
       }, 
      "content_length" : { 
       "type" : "integer" 
       }, 
       "language" : { 
       "type" : "string" 
       } 
      } 
      }, 
      "hash_id" : { 
      "type" : "string" 
      }, 
      "path" : { 
      "type" : "string" 
      }, 
      "raw_content" : { 
      "type" : "string", 
      "store" : true, 
      "term_vector" : "with_positions_offsets", 
      "analyzer" : "raw" 
      }, 
      "title" : { 
      "type" : "string" 
      } 
     } 
     } 
    }, 
    "settings" : { //insert your own settings here }, 
    "warmers" : { } 
    } 
} 

使得NEST,我將裝配內容爲這樣:

Attachment attachment = new Attachment(); 
attachment.Content = Convert.ToBase64String(File.ReadAllBytes("path/to/document")); 
attachment.ContentType = "html"; 

Document document = new Document(); 
document.File = attachment; 
document.RawContent = InsertRawContentFromString(originalText); 

我在測試感這 - 結果如下:

"file": { 
    "_content": "PGh0bWwgeG1sbnM6TWFkQ2FwPSJodHRwOi8vd3d3Lm1hZGNhcHNvZnR3YXJlLmNvbS9TY2hlbWFzL01hZENhcC54c2QiPg0KICA8aGVhZCAvPg0KICA8Ym9keT4NCiAgICA8aDE+VG9waWMxMDwvaDE+DQogICAgPHA+RGVsZXRlIHRoaXMgdGV4dCBhbmQgcmVwbGFjZSBpdCB3aXRoIHlvdXIgb3duIGNvbnRlbnQuIENoZWNrIHlvdXIgbWFpbGJveC48L3A+DQogICAgPHA+wqA8L3A+DQogICAgPHA+YXNkZjwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD4xMDwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD5MYXZlbmRlci48L3A+DQogICAgPHA+wqA8L3A+DQogICAgPHA+MTAvNiAxMjowMzwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD41IDA5PC9wPg0KICAgIDxwPsKgPC9wPg0KICAgIDxwPjExIDQ3PC9wPg0KICAgIDxwPsKgPC9wPg0KICAgIDxwPkhhbGxvd2VlbiBpcyBpbiBPY3RvYmVyLjwvcD4NCiAgICA8cD7CoDwvcD4NCiAgICA8cD5qb2c8L3A+DQogIDwvYm9keT4NCjwvaHRtbD4=", 
    "_content_length": 0, 
    "_content_type": "html", 
    "_date": "0001-01-01T00:00:00", 
    "_title": "Topic10" 
}, 
"delete": false, 
"raw_content": "<h1>Topic10</h1><p>Delete this text and replace it with your own content. Check your mailbox.</p><p> </p><p>asdf</p><p> </p><p>10</p><p> </p><p>Lavender.</p><p> </p><p>10/6 12:03</p><p> </p><p>5 09</p><p> </p><p>11 47</p><p> </p><p>Halloween is in October.</p><p> </p><p>jog</p>" 
}, 
"highlight": { 
"file.content": [ 
    "\n <em>Topic10</em>\n\n Delete this text and replace it with your own content. Check your mailbox.\n\n  \n\n asdf\n\n  \n\n 10\n\n  \n\n Lavender.\n\n  \n\n 10/6 12:03\n\n  \n\n 5 09\n\n  \n\n 11 47\n\n  \n\n Halloween is in October.\n\n  \n\n jog\n\n " 
    ] 
} 
相關問題