重庆做企业网站,个人网站 网站教程,美术学院网站建设,电子商务网站建设与管理笔试作者#xff1a;来自 Elastic Jeff Vestal 了解如何使用开放爬虫与 semantic text 字段结合来轻松抓取网站并使其可进行语义搜索。 Elastic Open Crawler 演练 我们在这里要做什么#xff1f;
Elastic Open Crawler 是 Elastic 托管爬虫的后继者。
Semantic text 是 Elasti…作者来自 Elastic Jeff Vestal 了解如何使用开放爬虫与 semantic text 字段结合来轻松抓取网站并使其可进行语义搜索。 Elastic Open Crawler 演练 我们在这里要做什么
Elastic Open Crawler 是 Elastic 托管爬虫的后继者。
Semantic text 是 Elastic 的简单入门数据类型用于启动和运行语义搜索。
两者结合就像是搜索领域的 “新锐组合”。
接下来我们将学习如何轻松配置并运行 Open Crawler 来爬取一个网站以 Search Labs 的博客为例自动分块并使用 ELSER 为这些网页内容博客文本生成稀疏向量嵌入并运行一些示例搜索确保一切正常运行。 选择你自己的冒险
如果你想在免费的、按需的研讨会环境中完成这个 —— 请点击此处。如果你更愿意观看此演练的视频 ——请点击此链接。如果你不熟悉 Open Crawler请在测试版公告博客中阅读。 或者在我们的讨论论坛上阅读一篇很棒的文章 —— 2024 年 12 月 12 日[EN] 像超级英雄一样浏览网页内容如果你喜欢阅读博客请继续阅读 Elasticsearch 项目或集群
你首先需要的是 Elasticsearch 集群或无服务器项目。如果你没有没关系你可以在 cloud.elastic.co 注册免费试用 创建映射模板。
如果你不想自定义任何映射Open Crawler 将使用合理的默认值将其在网络上抓取的所有数据索引到 Elasticsearch 中。但是在我们的例子中我们希望增强默认映射以便我们的一些文本可以为其生成稀疏向量。
我们将为索引中的两个关键字段创建一个新的索引模板。其余字段默认设置。
创建一个名为 body_semantic 的 semantic_text 字段 这将 从博客正文生成块使用 ELSER 从块生成稀疏向量注意语义文本字段需要 inference API它告诉 Elasticsearch 如何在提取和搜索时生成嵌入。由于我们在此示例中使用无服务器因此我们将在 Elasticsearch 中使用默认的 ELSER 推理端点。 如果你不使用 serverless 或想要设置自己的推理端点ELSER 的文档页面有一个创建新推理端点的示例。为 “body” 字段添加映射以包含 “copy_to” 参数从而将正文复制到我们的语义文本字段。 body 字段通常会自动映射到文本。 PUT 索引模板
PUT _index_template/search-labs-template
{index_patterns: [search-labs-blogs,search-labs-blogs*],template: {settings: {index: {default_pipeline: parse_author_and_publish_date}},mappings: {properties: {first_author: {type: keyword},publish_date: {type: date},body: {type: text,copy_to: body_semantic},body_semantic: {type: semantic_text,inference_id: elser-endpoint},last_crawled_at: {type: date}}}}
} 创建空索引
PUT search-labs-blogs 创建摄取管道
爬虫程序在抓取网页时可以提取一些信息。但是当你需要额外的解析时你可以配置摄取管道。
我们将配置一个管道来提取以下信息
从 author 字段中提取发布日期并将其存储在新的 posted_date 字段中从 list_of_authors 中提取第一作者并将其存储在新的 first_author 字段中删除 raw_author 和 raw_publish_date PUT 摄取管道
PUT _ingest/pipeline/parse_author_and_publish_date
{processors: [{script: {source: // If raw_author is null or empty array, set default unknownif (ctx.raw_author null || ctx.raw_author.size() 0) {ctx.list_of_authors [];ctx.first_author Unknown;} else {// raw_author is already an array from crawlerctx.list_of_authors ctx.raw_author;ctx.first_author ctx.raw_author[0]; // The first element}}},{script: {source: // If raw_publish_date is null or empty array, set default to January 1, 1970if (ctx.raw_publish_date null || ctx.raw_publish_date.trim().length() 0) {ctx.raw_publish_date January 1, 1970;} else {ctx.raw_publish_date ctx.raw_publish_date.trim();}}},{date: {field: raw_publish_date,target_field: publish_date,formats: [MMMM d, yyyy],timezone: UTC}},{remove: {field: raw_publish_date,ignore_missing: true}},{remove: {field: raw_author,ignore_missing: true}}]
}部署 ELSER
如果你使用的是 Elastic 的 serverless 项目则可以使用默认的 ELSER 推理端点 (_inference/.elser-2-elasticsearch)。如果这样做你需要更新索引模板中 body_semantic 的映射。
我们将创建一个新的推理端点为其提供更多资源这样非 serverless 的读者也可以享受乐趣 PUT 新的推理端点
PUT _inference/sparse_embedding/elser-endpoint
{service: elser,service_settings: {num_allocations: 32,num_threads: 1}
} Docker
这里只是简单说明一下。你必须在要运行爬虫的计算机或服务器上安装并运行 Docker。
查看 Docker 的入门指南以获取有关启动和运行的帮助。 Open Crawler 下载 Docker 镜像
你可以使用 Elastic 的官方镜像下载并启动 Open Crawler Docker 镜像。
docker run -i -d \
--name crawler \
docker.elastic.co/integrations/crawler:0.2.0 配置爬虫
我们将爬取 Elastic Search Labs 博客。Search Labs 有很多出色的搜索、ML 和 GenAI 内容。但它也链接到 elastic.co 的其他部分。我们将配置爬虫以限制我们的爬取确保仅索引博客。 Crawler.yaml
创建一个新的 crawler.yaml 文件并粘贴以下代码allow 规则用于 /search-labs/blog URL 模式下的所有内容包括一个 “拒绝所有” 的规则用于拦截所有其他 URL。使用提取规则提取作者的姓名并将其分配给字段 “authors”。 有关提取规则示例的更多详细信息请查看有关测试版的博客。在此示例中我们使用正则表达式模式作为拒绝规则。
将以下代码粘贴到 crawler.yml 中
crawler.yml
domains:- url: https://www.elastic.coseed_urls:- https://www.elastic.co/search-labs/blog/crawl_rules:- policy: allowtype: beginspattern: /search-labs/blog- policy: denytype: regexpattern: .*extraction_rulesets:- url_filters:- type: beginspattern: /search-labs/blog/rules:- action: extractfield_name: raw_authorselector: .Byline_authorNames__bCmvc a[href*/search-labs/author/]join_as: arraysource: html- action: extractfield_name: raw_publish_dateselector: time.article-published-dateattribute: datetimejoin_as: stringsource: htmloutput_sink: elasticsearch
output_index: search-labs-blogssitemap_discovery_disabled: true
binary_content_extraction_enabled: falseelasticsearch:host: http://kubernetes-vmusername: elasticpassword: changemepipeline: parse_author_and_publish_datepipeline_enabled: truelog_level: debug 注如果我们想使用自签名的 Elasticsearch 集群来演示那么我们需要添加 fingerprint。你可以使用如下的命令来获得 fingerprint openssl x509 -fingerprint -sha256 -in config/certs/http_ca.crt ## The SHA256 CA cert fingerprint used to verify SSL connection to Elasticsearch.
## SSL usage is configured by the presence of https in elasticsearch.host
#elasticsearch.ca_fingerprint: null 将配置文件复制到正在运行的 docker 容器
运行下面的复制命令
docker cp crawler.yml crawler:app/config/crawler.yml 启动抓取作业
我们现在可以抓取一些网页了运行以下命令即可启动它。
docker exec -it crawler bin/crawler crawl config/crawler.yml 注意你最初可能会在爬虫日志中看到超时。默认情况下ELSER 部署会缩减为 0 个分配以减少空闲时的成本。部署需要一分钟才能扩展。 转到文档
返回 Kibana 中的控制台并输入以下搜索
GET search-labs-blogs/_search
{retriever: {standard: {query: {semantic: {field: body_semantic,query: How do I quantize vectors?}}}},_source: false,fields: [title,body]
}
我收到的前五个标题是
Better Binary Quantization vs. Product Quantization - Search LabsScalar quantization 101 - Search LabsRaBitQ binary quantization 101 - Search LabsBetter Binary Quantization (BBQ) in Lucene and Elasticsearch - Search LabsUnderstanding Int4 scalar quantization in Lucene - Search Labs
所有查找可以帮助回答我的问题的博客。 Discover
你还可以跳转到 “Discover” 以查看文档表。
设置方法
单击 “ Data View” 选择器单击 “Create a data view”在索引模式框中输入“search-labs-blogs”在 “Timestamp” 字段中选择 “publish_date”单击 “Save data view to Kibana”
你可能需要更改时间选择器以设置更宽的范围。
单击时间选择器通常默认为“Last 15 minutes”选择 “Last 1 year”
你可以单击左列字段名称旁边的 制作一个格式良好的表格如下所示。 Discover 视图显示了从 Open Crawler 索引的 search labs 博客 按需工作坊
你已经坚持到最后了为什么不在一个真实的按需工作坊环境中亲自动手实践以上内容呢点击此处立即参与。 想要获得 Elastic 认证了解下一期 Elasticsearch 工程师培训何时举行
Elasticsearch 包含许多新功能可帮助您针对自己的用例构建最佳搜索解决方案。深入了解我们的示例笔记本以了解更多信息开始免费云试用或立即在本地机器上试用 Elastic。 原文Semantic search using the Open Crawler and Semantic Text - Elasticsearch Labs