【问题标题】:How to index dump of html files to elasticsearch?如何将html文件的转储索引到elasticsearch?
【发布时间】:2012-11-18 19:16:18
【问题描述】:

我是弹性新手,所以我的知识仅来自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 文档吗?谢谢大家。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      具体来说,如果你想在索引和存储内容之前去掉 html 标签,你可以使用映射器附件插件——当你定义映射时,你可以将 content_type 分类为“html.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  "
          ]
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2017-06-29
        • 1970-01-01
        • 2012-10-22
        • 2013-04-02
        • 2019-06-03
        • 1970-01-01
        相关资源
        最近更新 更多