如何将html文件的转储索引到elasticsearch?

时间:2012-11-30 14:45:34

标签: elasticsearch

我完全是弹性的,所以我的知识只来自elasticsearch网站,我需要帮助。 我的任务是将html格式的大行数据编入弹性搜索。我已经抓取了我的数据并将其存储到磁盘上(200 000个html文件)。我的问题是将所有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文档吗?谢谢大家。

2 个答案:

答案 0 :(得分:4)

我会查看bulk api,它允许您在单个请求中发送多个文档,以加快索引过程。您可以发送10,20或更多批文档,具体取决于它们的大小。

根据您要索引的内容,您可能需要解析html,除非您想将整个html索引为单个字段(在这种情况下,您可能希望使用html strip char filter来删除html来自索引文本的标签)。

索引后我建议确保映射是正确的,你可以找到你想要的东西。您始终可以使用elasticsearch存储的_source特殊字段重新编制索引,但如果您已经编写了索引器代码,则可能需要在需要时再次使用它来重新索引(当然使用相同的html文档)。在实践中,你永远不会将数据编入索引...所以要小心:)尽管elasticsearch总是帮助你使用_source字段),这只是查询现有索引并将其所有文档重新索引到另一个索引上。

答案 1 :(得分:1)

@ javanna建议查看Bulk API肯定会引导您朝着正确的方向前进。如果您使用的是NEST,则可以将所有对象存储在一个列表中,然后可以序列化JSON对象以对内容编制索引。

具体来说,如果要在索引和存储内容之前删除html标记,可以使用映射器附件插件 - 在定义映射时,可以将content_type分类为&#34 ; HTML"

映射器附件对很多东西很有用,特别是如果你处理多种文档类型,但最值得注意的是 - 我相信只是为了剥离html标签而使用它就足够了(你不能用html_strip char做的事情)过滤器)。

只是预警 - 不会存储任何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);

我在Sense中测试了这个 - 结果如下:

"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  "
    ]
}