Enterprise:Elastic App Search - Web 爬虫器实践

842 阅读4分钟

让内容搜索可通过多种形式实现。Elastic App Search 已经允许用户通过上传或粘贴 JSON 以及通过 API 终端来采集内容。使用 Elastic 企业搜索 7.11,用户现在可以通过功能强大的网络爬虫来采集内容,该爬虫能够从可公开访问的网站中检索信息,从而可以轻松地在你的 App Search 引擎中搜索内容。与 App Search 上的任何采集方法一样,这种模式是在采集时推断出来的,只需单击一下即可进行近乎实时地更新。通过单击(无需写代码),用户就可以定制网络爬虫规则,以便在排除规则指示网络爬虫避免某些页面、内容和术语的同时指定入口点。

Elastic scrawler

 

在今天的练习中,我们将详细描述如何部署 Elastic Stack 以实现对指定网站的爬虫。请注意:App Search web crawler 只适用于 7.11 及以后的版本。

 

安装

安装 Elasticsearch

我们可参考我之前的文章 “如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch” 来安装 Elasticsearch

安装 Kibana

我们接下来安装 Kibana。我们可以参考我之前的文章 “如何在 Linux,MacOS 及 Windows 上安装 Elastic 栈中的 Kibana” 来进行我们的安装。

Java安装

你需要安装 Java。版本在 Java 8 或者 Java 11

App search 安装

我们在地址 www.elastic.co/downloads/a… 找到我们需要的版本进行下载。并按照相应的指令来进行按照。如果你想针对你以前的版本进行安装的话,请参阅地址 www.elastic.co/downloads/p…

如果我们不对 Elasticsearch 或 app-search 做任何的配置的话,我们会看到如下的错误信息:

 

配置 Elasticsearch

按照 www.elastic.co/downloads/e… 页面的要求,我们需要针对 Elasticsearch 配置安全访问。我们可以参考之前的文章 “Elasticsearch:设置 Elastic 账户安全” 来配置安全。在这里需要特别指出的是:我们在 config/elasticsearch.yml 文件中需要多添加一个配置:xpack.security.authc.api_key.enabled: true,也即:

config/elasticsearch.yml

xpack.security.enabled: true
xpack.security.authc.api_key.enabled: true

在修改完上述的配置后,我们在一个 terminal 中启动 elasticsearch:

$ bin/elasticsearch

然后,在另外一个 terminal 中打入如下的命令来设置密码:

$ ./bin/elasticsearch-setup-passwords interactive

为了方便,我们把所有的密码都设置为 password。

 

配置 Elastic Enterprise Search

我们在 Enterprise Search 的安装目录下,找到 config/enterpirse-search.yml  文件,并添加如下的配置:

config/enterpirse-search.yml

ent_search.auth.source: standard
elasticsearch.username: elastic
elasticsearch.password: ELASTIC_USER_PASSWORD
allow_es_settings_modification: true

在上面,你需要修改上面的密码为你自己的 Elasticsearch 密码。在上面,我设置的密码为 password。按照要求,我们必须设置至少一个加密的键值。在上面我们第一次运行 enterprise-search 时,在返回的错误里有一个是 :

Invalid config file (/Users/liuxg/elastic1/enterprise-search-7.11.0/config/enterprise-search.yml):
The setting '#/secret_management/encryption_keys' is not valid
No secret management encryption keys were provided.
Your secrets cannot be stored unencrypted.
You can use the following generated encryption key in your config file to store new encrypted secrets:

secret_management.encryption_keys: [99c52330f78f2b669ebacb58ed65c6289e1d7b18f779175b0ea715f6bf14451c]

我们把上面的这个 key 添加到 config/enterprise-search.yml 文件中:

 

启动  enterprise-search

在启动的时候,我们可以为 enterprise-search 用户设置一个密码,我们可以这样启动:

$ ENT_SEARCH_DEFAULT_PASSWORD=passwordexample bin/enterprise-search

针对我的情况,我把密码设置为 password:

$ ENT_SEARCH_DEFAULT_PASSWORD=password bin/enterprise-search

经过一段时间的运行,我们可以看到:

在上面,我们可以看到 enterprise-search 用户的密码是 password。这个就是我们在命令行中定义的密码。我们也可以看到如下的信息:

它表明我们的 enterprise-search 已经成功运行,并可以在端口地址 http://localhost:3002 进行访问。如果我们能够看到如下的画面,则表明我们的 enterprise search 的安装是成功的:

 

配置网站爬虫

在上的页面中,点击 Continue to Login 按钮:

我们把之前设置的用户名及密码输入进去。针对我的配置:enterprise_search/password。点击 Log In 按钮:

上面显示它将爬地址以地址 www.elastic.co/ 网页以及在网页包含的所有的链接:

同时由于 www.elastic.co/jobs 不在首页被链接,我们单独为它添加了一个 entry point。由于一些考虑,我们也同时定制了一些爬虫规则。在上面,我们不希望爬 www.elastic.co/jobs 里的所有网页。

一旦定义完毕,我们点击右上角的 Start a Crawl 按钮:

上面的右上角显示正在爬。我们可以点击 Cancel Crawl 来停止这个动作。我们可以一直等直到爬完所有的 entry points。

等爬完整个页面,我们可以对文章进行搜索:

关于如何设置同义词,调节相关度,Curations 等,请参考我之前的文章 “Enterprise:Elastic App Search 入门”。