Py学习  »  docker

使用 Docker 和 Elasticsearch 构建一个全文搜索应用程序 | Linux 中国

Linux中国 • 5 年前 • 425 次点击  
我们将使用 Docker 去配置我们自己的项目环境和依赖。这使我写这个教程快速又简单。
-- Patrick Triest



致谢
编译自 | https://blog.patricktriest.com/text-search-docker-elasticsearch/ 
 作者 | Patrick Triest
 译者 | qhwdw 🌟 🌟 🌟 🌟 🌟 共计翻译:105 篇 贡献时间:177 天

如何在超过 500 万篇文章的 Wikipedia 上找到与你研究相关的文章?

如何在超过 20 亿用户的 Facebook 中找到你的朋友(并且还拼错了名字)?

谷歌如何在整个因特网上搜索你的模糊的、充满拼写错误的查询?

在本教程中,我们将带你探索如何配置我们自己的全文搜索应用程序(与上述问题中的系统相比,它的复杂度要小很多)。我们的示例应用程序将提供一个 UI 和 API 去从 100 部经典文学(比如,《彼得·潘》 、  《弗兰肯斯坦》 和  《金银岛》)中搜索完整的文本。

你可以在这里(https://search.patricktriest.com)预览该教程应用的完整版本。

preview webapp

这个应用程序的源代码是 100% 开源的,可以在 GitHub 仓库上找到它们 —— https://github.com/triestpa/guttenberg-search 。

在应用程序中添加一个快速灵活的全文搜索可能是个挑战。大多数的主流数据库,比如,PostgreSQL[3] 和 MongoDB[4],由于受其查询和索引结构的限制只能提供一个非常基础的文本搜索功能。为实现高质量的全文搜索,通常的最佳选择是单独的数据存储。Elasticsearch[5] 是一个开源数据存储的领导者,它专门为执行灵活而快速的全文搜索进行了优化。

我们将使用 Docker[6] 去配置我们自己的项目环境和依赖。Docker 是一个容器化引擎,它被 Uber[7]Spotify[8]ADP[9] 以及 Paypal[10] 使用。构建容器化应用的一个主要优势是,项目的设置在 Windows、macOS、以及 Linux 上都是相同的 —— 这使我写这个教程快速又简单。如果你还没有使用过 Docker,不用担心,我们接下来将经历完整的项目配置。

我也会使用 Node.js[11] (使用 Koa[12] 框架)和 Vue.js[13],用它们分别去构建我们自己的搜索 API 和前端 Web 应用程序。

1 - Elasticsearch 是什么?

全文搜索在现代应用程序中是一个有大量需求的特性。搜索也可能是最难的一项特性 —— 许多流行的网站的搜索功能都不合格,要么返回结果太慢,要么找不到精确的结果。通常,这种情况是被底层的数据库所局限:大多数标准的关系型数据库局限于基本的 CONTAINS 或 LIKESQL 查询上,它仅提供最基本的字符串匹配功能。

我们的搜索应用程序将具备:

☉ 快速 - 搜索结果将快速返回,为用户提供一个良好的体验。
☉ 灵活 - 我们希望能够去修改搜索如何执行的方式,这是为了便于在不同的数据库和用户场景下进行优化。
☉ 容错 - 如果所搜索的内容有拼写错误,我们将仍然会返回相关的结果,而这个结果可能正是用户希望去搜索的结果。
☉ 全文 - 我们不想限制我们的搜索只能与指定的关键字或者标签相匹配 —— 我们希望它可以搜索在我们的数据存储中的任何东西(包括大的文本字段)。

Elastic Search Logo

为了构建一个功能强大的搜索功能,通常最理想的方法是使用一个为全文搜索任务优化过的数据存储。在这里我们使用 Elasticsearch[5],Elasticsearch 是一个开源的内存中的数据存储,它是用 Java 写的,最初是在 Apache Lucene[14] 库上构建的。

这里有一些来自 Elastic 官方网站[15] 上的 Elasticsearch 真实使用案例。

◈ Wikipedia 使用 Elasticsearch 去提供带高亮搜索片断的全文搜索功能,并且提供按类型搜索和 “did-you-mean” 建议。
◈ Guardian 使用 Elasticsearch 把社交网络数据和访客日志相结合,为编辑去提供新文章的公众意见的实时反馈。
◈ Stack Overflow 将全文搜索和地理查询相结合,并使用 “类似” 的方法去找到相关的查询和回答。
◈ GitHub 使用 Elasticsearch 对 1300 亿行代码进行查询。

与 “普通的” 数据库相比,Elasticsearch 有什么不一样的地方?

Elasticsearch 之所以能够提供快速灵活的全文搜索,秘密在于它使用反转索引inverted index 。

“索引” 是数据库中的一种数据结构,它能够以超快的速度进行数据查询和检索操作。数据库通过存储与表中行相关联的字段来生成索引。在一种可搜索的数据结构(一般是 B 树[16])中排序索引,在优化过的查询中,数据库能够达到接近线性的时间(比如,“使用 ID=5 查找行”)。

Relational Index

我们可以将数据库索引想像成一个图书馆中老式的卡片式目录 —— 只要你知道书的作者和书名,它就会告诉你书的准确位置。为加速特定字段上的查询速度,数据库表一般有多个索引(比如,在 name 列上的索引可以加速指定名字的查询)。

反转索引本质上是不一样的。每行(或文档)的内容是分开的,并且每个独立的条目(在本案例中是单词)反向指向到包含它的任何文档上。

Inverted Index

这种反转索引数据结构可以使我们非常快地查询到,所有出现 “football” 的文档。通过使用大量优化过的内存中的反转索引,Elasticsearch 可以让我们在存储的数据上,执行一些非常强大的和自定义的全文搜索。

2 - 项目设置

2.0 - Docker

我们在这个项目上使用 Docker[6] 管理环境和依赖。Docker 是个容器引擎,它允许应用程序运行在一个独立的环境中,不会受到来自主机操作系统和本地开发环境的影响。现在,许多公司将它们的大规模 Web 应用程序主要运行在容器架构上。这样将提升灵活性和容器化应用程序组件的可组构性。

Docker Logo

对我来说,使用 Docker 的优势是,它对本教程的作者非常方便,它的本地环境设置量最小,并且跨 Windows、macOS 和 Linux 系统的一致性很好。我们只需要在 Docker 配置文件中定义这些依赖关系,而不是按安装说明分别去安装 Node.js、Elasticsearch 和 Nginx,然后,就可以使用这个配置文件在任何其它地方运行我们的应用程序。而且,因为每个应用程序组件都运行在它自己的独立容器中,它们受本地机器上的其它 “垃圾” 干扰的可能性非常小,因此,在调试问题时,像“它在我这里可以工作!”这类的问题将非常少。

2.1 - 安装 Docker & Docker-Compose

这个项目只依赖 Docker[6] 和 docker-compose[17],docker-compose 是 Docker 官方支持的一个工具,它用来将定义的多个容器配置 组装  成单一的应用程序栈。

◈ 安装 Docker - https://docs.docker.com/engine/installation/
◈ 安装 Docker Compose - https://docs.docker.com/compose/install/

2.2 - 设置项目主目录

为项目创建一个主目录(名为 guttenberg_search)。我们的项目将工作在主目录的以下两个子目录中。

◈ /public - 保存前端 Vue.js Web 应用程序。
◈ /server - 服务器端 Node.js 源代码。

2.3 - 添加 Docker-Compose 配置

接下来,我们将创建一个 docker-compose.yml 文件来定义我们的应用程序栈中的每个容器。

☉ gs-api - 后端应用程序逻辑使用的 Node.js 容器
☉ gs-frontend - 前端 Web 应用程序使用的 Ngnix 容器。
☉ gs-search - 保存和搜索数据的 Elasticsearch 容器。
  1. version: '3'

  2. services:

  3.  api: # Node.js App

  4.    container_name: gs- api

  5.    build: .

  6.    ports:

  7.      - "3000:3000" # Expose API port

  8.      - "9229:9229" # Expose Node process debug port (disable in production)

  9.    environment: # Set ENV vars

  10.     - NODE_ENV=local

  11.     - ES_HOST=elasticsearch

  12.     - PORT=3000

  13.    volumes: # Attach local book data directory

  14.      - ./books:/usr/src/app/books

  15.  frontend: # Nginx Server For Frontend App

  16.    container_name: gs-frontend

  17.    image: nginx

  18.    volumes: # Serve local "public" dir

  19.      - ./public:/usr/share/nginx/html

  20.    ports:

  21.      - "8080:80" # Forward site to localhost:8080

  22.  elasticsearch: # Elasticsearch Instance

  23.    container_name: gs-search

  24.    image: docker.elastic.co/elasticsearch/elasticsearch:6.1.1

  25.    volumes: # Persist ES data in seperate "esdata" volume

  26.      - esdata:/usr/share/elasticsearch/data

  27.    environment:

  28.      - bootstrap.memory_lock=true

  29.      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"

  30.      - discovery.type=single-node

  31.    ports: # Expose Elasticsearch ports

  32.      - "9300:9300"

  33.      - "9200:9200"

  34. volumes: # Define seperate volume for Elasticsearch data

  35.  esdata:

这个文件定义了我们全部的应用程序栈 —— 不需要在你的本地系统上安装 Elasticsearch、Node 和 Nginx。每个容器都将端口转发到宿主机系统(localhost)上,以便于我们在宿主机上去访问和调试 Node API、Elasticsearch 实例和前端 Web 应用程序。

2.4 - 添加 Dockerfile

对于 Nginx 和 Elasticsearch,我们使用了官方预构建的镜像,而 Node.js 应用程序需要我们自己去构建。

在应用程序的根目录下定义一个简单的 Dockerfile 配置文件。

  1. # Use Node v8.9.0 LTS

  2. FROM node:carbon

  3. # Setup app working directory

  4. WORKDIR /usr/src/app

  5. # Copy package.json and package-lock.json

  6. COPY package*.json ./

  7. # Install app dependencies

  8. RUN npm install

  9. # Copy sourcecode

  10. COPY . .

  11. # Start app

  12. CMD [ "npm", "start" ]

这个 Docker 配置扩展了官方的 Node.js 镜像、拷贝我们的应用程序源代码、以及在容器内安装 NPM 依赖。

我们也增加了一个 .dockerignore 文件,以防止我们不需要的文件拷贝到容器中。

  1. node_modules/

  2. npm-debug.log

  3. books/

  4. public/

请注意:我们之所以不拷贝 node_modules 目录到我们的容器中 —— 是因为我们要在容器构建过程里面运行 npm install。从宿主机系统拷贝 node_modules 到容器里面可能会引起错误,因为一些包需要为某些操作系统专门构建。比如说,在 macOS 上安装 bcrypt  包,然后尝试将这个模块直接拷贝到一个 Ubuntu 容器上将不能工作,因为 bcyrpt 需要为每个操作系统构建一个特定的二进制文件。

2.5 - 添加基本文件

为了测试我们的配置,我们需要添加一些占位符文件到应用程序目录中。

在 public/index.html 文件中添加如下内容。

  1. Hello World From The Frontend Container

接下来,在 server/app.js 中添加 Node.js 占位符文件。

  1. const Koa = require('koa')

  2. const app = new Koa()

  3. app.use(async (ctx, next) => {

  4.  ctx.body = 'Hello World From the Backend Container'

  5. })

  6. const port = process.env.PORT || 3000

  7. app.listen(port, err => {

  8.  if (err) console.error(err)

  9.  console.log(`App Listening on Port ${port}`)

  10. })

最后,添加我们的 package.json  Node 应用配置。

  1. {

  2.  "name": "guttenberg-search",

  3.  "version": "0.0.1",

  4.  "description": "Source code for Elasticsearch tutorial using 100 classic open source books.",

  5.  "scripts": {

  6.    "start": "node --inspect=0.0.0.0:9229 server/app.js"

  7.  },

  8.  "repository": {

  9.    "type": "git",

  10.    "url": "git+https://github.com/triestpa/guttenberg-search.git"

  11.  },

  12.  "author": "patrick.triest@gmail.com",

  13.  "license": "MIT",

  14.  "bugs": {

  15.    "url": "https://github.com/triestpa/guttenberg-search/issues"

  16.  },

  17.  "homepage": "https://github.com/triestpa/guttenberg-search#readme",

  18.  "dependencies": {

  19.    "elasticsearch": "13.3.1",

  20.    "joi" : "13.0.1",

  21.    "koa": "2.4.1",

  22.    "koa-joi-validate": "0.5.1",

  23.    "koa-router": "7.2.1"

  24.  }

  25. }

这个文件定义了应用程序启动命令和 Node.js 包依赖。

注意:不要运行 npm install —— 当它构建时,依赖会在容器内安装。

2.6 - 测试它的输出

现在一切新绪,我们来测试应用程序的每个组件的输出。从应用程序的主目录运行 docker-compose build,它将构建我们的 Node.js 应用程序容器。

docker build output

接下来,运行 docker-compose up 去启动整个应用程序栈。

docker compose output

这一步可能需要几分钟时间,因为 Docker 要为每个容器去下载基础镜像。以后再次运行,启动应用程序会非常快,因为所需要的镜像已经下载完成了。

在你的浏览器中尝试访问 localhost:8080 —— 你将看到简单的 “Hello World” Web 页面。

frontend sample output

访问 localhost:3000 去验证我们的 Node 服务器,它将返回 “Hello World” 信息。

backend sample output

最后,访问 localhost:9200 去检查 Elasticsearch 运行状态。它将返回类似如下的内容。

  1. {

  2.  "name" : "SLTcfpI",

  3.  "cluster_name" : "docker-cluster",

  4.  "cluster_uuid" : "iId8e0ZeS_mgh9ALlWQ7-w",

  5.  "version" : {

  6.    "number" : "6.1.1",

  7.    "build_hash" : "bd92e7f",

  8.    "build_date" : "2017-12-17T20:23:25.338Z",

  9.    "build_snapshot" : false,

  10.    "lucene_version" : "7.1.0",

  11.    "minimum_wire_compatibility_version" : "5.6.0",

  12.    "minimum_index_compatibility_version" : "5.0.0"

  13.  },

  14.  "tagline" : "You Know, for Search"

  15. }

如果三个 URL 都显示成功,祝贺你!整个容器栈已经正常运行了,接下来我们进入最有趣的部分。

3 - 连接到 Elasticsearch

我们要做的第一件事情是,让我们的应用程序连接到我们本地的 Elasticsearch 实例上。

3.0 - 添加 ES 连接模块

在新文件 server/connection.js 中添加如下的 Elasticsearch 初始化代码。

  1. const elasticsearch = require('elasticsearch')

  2. // Core ES variables for this project

  3. const index = 'library'

  4. const type = 'novel'

  5. const port = 9200

  6. const host = process.env.ES_HOST || 'localhost'

  7. const client = new elasticsearch.Client({ host: { host, port } })

  8. /** Check the ES connection status */

  9. async function checkConnection () {

  10.  let isConnected = false

  11.  while (!isConnected) {

  12.    console.log('Connecting to ES')

  13.    try {

  14.      const health = await client.cluster.health({})

  15.      console.log(health)

  16.      isConnected = true

  17.    } catch (err) {

  18.      console.log('Connection Failed, Retrying...', err)

  19.    }

  20.  }

  21. }

  22. checkConnection()

现在,我们重新构建我们的 Node 应用程序,我们将使用 docker-compose build 来做一些改变。接下来,运行 docker-compose up -d 去启动应用程序栈,它将以守护进程的方式在后台运行。

应用程序启动之后,在命令行中运行 docker exec gs-api "node" "server/connection.js",以便于在容器内运行我们的脚本。你将看到类似如下的系统输出信息。

  1. { cluster_name: 'docker-cluster',

  2.  status: 'yellow',

  3.  timed_out: false,

  4.  number_of_nodes: 1,

  5.  number_of_data_nodes: 1,

  6.  active_primary_shards: 1,

  7.  active_shards: 1,

  8.  relocating_shards: 0,

  9.  initializing_shards: 0,

  10.  unassigned_shards: 1,

  11.  delayed_unassigned_shards: 0,

  12.  number_of_pending_tasks: 0,

  13.  number_of_in_flight_fetch: 0,

  14.  task_max_waiting_in_queue_millis: 0,

  15.  active_shards_percent_as_number: 50 }

继续之前,我们先删除最下面的 checkConnection() 调用,因为,我们最终的应用程序将调用外部的连接模块。

3.1 - 添加函数去重置索引

在 server/connection.js 中的 checkConnection 下面添加如下的函数,以便于重置 Elasticsearch 索引。

  1. /** Clear the index, recreate it, and add mappings */

  2. async function resetIndex (index) {

  3.  if (await client.indices.exists({ index })) {

  4.    await client.indices.delete({ index })

  5.  }

  6.  await client.indices.create({ index })

  7.  await putBookMapping()

  8. }

3.2 - 添加图书模式

接下来,我们将为图书的数据模式添加一个 “映射”。在 server/connection.js 中的 resetIndex 函数下面添加如下的函数。

  1. /** Add book section schema mapping to ES */

  2. async function putBookMapping () {

  3.  const schema = {

  4.    title: { type: 'keyword' },

  5.    author: { type: 'keyword' },

  6.    location: { type: 'integer' },

  7.    text: { type: 'text' }

  8.  }

  9.  return client.indices.putMapping({ index, type, body: { properties: schema } })

  10. }

这是为 book 索引定义了一个映射。Elasticsearch 中的 index 大概类似于 SQL 的 table  或者 MongoDB 的  collection。我们通过添加映射来为存储的文档指定每个字段和它的数据类型。Elasticsearch 是无模式的,因此,从技术角度来看,我们是不需要添加映射的,但是,这样做,我们可以更好地控制如何处理数据。

比如,我们给 title 和 author 字段分配 keyword 类型,给 text 字段分配 text 类型。之所以这样做的原因是,搜索引擎可以区别处理这些字符串字段 —— 在搜索的时候,搜索引擎将在 text 字段中搜索可能的匹配项,而对于 keyword 类型字段,将对它们进行全文匹配。这看上去差别很小,但是它们对在不同的搜索上的速度和行为的影响非常大。

在文件的底部,导出对外发布的属性和函数,这样我们的应用程序中的其它模块就可以访问它们了。

  1. module.exports = {

  2.  client, index, type, checkConnection, resetIndex

  3. }

4 - 加载原始数据

我们将使用来自 古登堡项目[20] 的数据 ——  它致力于为公共提供免费的线上电子书。在这个项目中,我们将使用 100 本经典图书来充实我们的图书馆,包括《福尔摩斯探案集》、《金银岛》、《基督山复仇记》、《环游世界八十天》、《罗密欧与朱丽叶》 和《奥德赛》。

Book Covers

4.1 - 下载图书文件

我将这 100 本书打包成一个文件,你可以从这里下载它 —— https://cdn.patricktriest.com/data/books.zip

将这个文件解压到你的项目的 books/ 目录中。

你可以使用以下的命令来完成(需要在命令行下使用 wget[22] 和 The Unarchiver[23])。

  1. wget https://cdn.patricktriest.com/data/books.zip

  2. unar books.zip

4.2 - 预览一本书

尝试打开其中的一本书的文件,假设打开的是 219-0.txt。你将注意到它开头是一个公开访问的协议,接下来是一些标识这本书的书名、作者、发行日期、语言和字符编码的行。

  1. Title: Heart of Darkness

  2. Author: Joseph Conrad

  3. Release Date: February 1995 [EBook #219]

  4. Last Updated: September 7, 2016

  5. Language: English

  6. Character set encoding: UTF-8

在 *** START OF THIS PROJECT GUTENBERG EBOOK HEART OF DARKNESS *** 这些行后面,是这本书的正式内容。

如果你滚动到本书的底部,你将看到类似  *** END OF THIS PROJECT GUTENBERG EBOOK HEART OF DARKNESS *** 信息,接下来是本书更详细的协议版本。

下一步,我们将使用程序从文件头部来解析书的元数据,提取 *** START OF 和 ***END OF 之间的内容。

4.3 - 读取数据目录

我们将写一个脚本来读取每本书的内容,并将这些数据添加到 Elasticsearch。我们将定义一个新的 Javascript 文件 server/load_data.js 来执行这些操作。

首先,我们将从 books/ 目录中获取每个文件的列表。

在 server/load_data.js 中添加下列内容。

  1. const fs = require('fs')

  2. const path = require('path')

  3. const esConnection = require('./connection')

  4. /** Clear ES index, parse and index all files from the books directory */

  5. async function readAndInsertBooks () {

  6.  try {

  7.    // Clear previous ES index

  8.    await esConnection.resetIndex()

  9.     // Read books directory

  10.    let files = fs.readdirSync('./books').filter(file => file.slice(-4) === '.txt')

  11.    console.log(`Found ${files.length} Files`)

  12.    // Read each book file, and index each paragraph in elasticsearch

  13.    for (let file of files) {

  14.      console.log(`Reading File - ${file}`)

  15.      const filePath = path.join('./books', file)

  16.      const { title, author, paragraphs } = parseBookFile(filePath)

  17.      await insertBookData(title, author, paragraphs)

  18.    }

  19.  } catch (err) {

  20.    console.error(err)

  21.  }

  22. }

  23. readAndInsertBooks()

我们将使用一个快捷命令来重构我们的 Node.js 应用程序,并更新运行的容器。

运行 docker-compose up -d --build 去更新应用程序。这是运行  docker-compose build 和 docker-compose up -d 的快捷命令。

docker build output

为了在容器中运行我们的 load_data 脚本,我们运行 docker exec gs-api "node" "server/load_data.js" 。你将看到 Elasticsearch 的状态输出 Found 100 Books

这之后,脚本发生了错误退出,原因是我们调用了一个没有定义的辅助函数(parseBookFile)。

docker exec output

4.4 - 读取数据文件

接下来,我们读取元数据和每本书的内容。

在 server/load_data.js 中定义新函数。

  1. /** Read an individual book text file, and extract the title, author, and paragraphs */

  2. function parseBookFile (filePath) {

  3.  // Read text file

  4.  const book = fs.readFileSync(filePath, 'utf8')

  5.  // Find book title and author

  6.  const title = book.match(/^Title:\s(.+)$/m)[1]

  7.  const authorMatch = book.match(/^Author:\s(.+)$/m)

  8.  const author = (!authorMatch || authorMatch[1].trim() === '') ? 'Unknown Author' : authorMatch[1 ]

  9.  console.log(`Reading Book - ${title} By ${author}`)

  10.  // Find Guttenberg metadata header and footer

  11.  const startOfBookMatch = book.match(/^\*{3}\s*START OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$/m)

  12.  const startOfBookIndex = startOfBookMatch.index + startOfBookMatch[0].length

  13.  const endOfBookIndex = book.match(/^\*{3}\s*END OF (THIS|THE) PROJECT GUTENBERG EBOOK.+\*{3}$/m).index

  14.  // Clean book text and split into array of paragraphs

  15.  const paragraphs = book

  16.    .slice(startOfBookIndex, endOfBookIndex) // Remove Guttenberg header and footer

  17.    .split (/\n\s+\n/g) // Split each paragraph into it's own array entry

  18.    .map(line => line.replace(/\r\n/g, ' ').trim()) // Remove paragraph line breaks and whitespace

  19.    .map(line => line.replace(/_/g, '')) // Guttenberg uses "_" to signify italics.  We'll remove it, since it makes the raw text look messy.

  20.    .filter((line) => (line && line.length !== '')) // Remove empty lines

  21.  console.log(`Parsed ${paragraphs.length} Paragraphs\n`)

  22.  return { title, author, paragraphs }

  23. }

这个函数执行几个重要的任务。

☉ 从文件系统中读取书的文本。
☉ 使用正则表达式(关于正则表达式,请参阅  这篇文章[24] )解析书名和作者。
☉ 通过匹配 “古登堡项目” 的头部和尾部,识别书的正文内容。
☉ 提取书的内容文本。
☉ 分割每个段落到它的数组中。
☉ 清理文本并删除空白行。

它的返回值,我们将构建一个对象,这个对象包含书名、作者、以及书中各段落的数组。

再次运行 docker-compose up -d --build 和 docker exec gs-api "node" "server/load_data.js",你将看到输出同之前一样,在输出的末尾有三个额外的行。

docker exec output

成功!我们的脚本从文本文件中成功解析出了书名和作者。脚本再次以错误结束,因为到现在为止,我们还没有定义辅助函数。

4.5 - 在 ES 中索引数据文件

最后一步,我们将批量上传每个段落的数组到 Elasticsearch 索引中。

在 load_data.js 中添加新的 insertBookData 函数。

  1. /** Bulk index the book data in Elasticsearch */

  2. async function insertBookData (title, author, paragraphs) {

  3.  let bulkOps = [] // Array to store bulk operations

  4.  // Add an index operation for each section in the book

  5.  for (let i = 0; i < paragraphs.length; i++) {

  6.    // Describe action

  7.    bulkOps.push({ index: { _index: esConnection.index, _type: esConnection.type } })

  8.    // Add document

  9.    bulkOps.push({

  10.      author,

  11.      title,

  12.      location: i,

  13.      text: paragraphs[i]

  14.    })

  15.    if (i > 0 && i % 500 === 0) { // Do bulk insert in 500 paragraph batches

  16.      await esConnection.client.bulk({ body: bulkOps })

  17.      bulkOps = []

  18.      console.log(`Indexed Paragraphs ${i - 499} - ${i}`)

  19.    }

  20.  }

  21.  // Insert remainder of bulk ops array

  22.  await esConnection.client.bulk({ body: bulkOps })

  23.  console.log(`Indexed Paragraphs ${paragraphs.length - (bulkOps.length / 2)} - ${paragraphs.length}\n\n\n`)

  24. }

这个函数将使用书名、作者和附加元数据的段落位置来索引书中的每个段落。我们通过批量操作来插入段落,它比逐个段落插入要快的多。

我们分批索引段落,而不是一次性插入全部,是为运行这个应用程序的内存稍有点小(1.7 GB)的服务器  search.patricktriest.com 上做的一个重要优化。如果你的机器内存还行(4 GB 以上),你或许不用分批上传。

运行 docker-compose up -d --build 和 docker exec gs-api "node" "server/load_data.js" 一次或多次 —— 现在你将看到前面解析的 100 本书的完整输出,并插入到了 Elasticsearch。这可能需要几分钟时间,甚至更长。

data loading output

5 - 搜索

现在,Elasticsearch 中已经有了 100 本书了(大约有 230000 个段落),现在我们尝试搜索查询。

5.0 - 简单的 HTTP 查询

首先,我们使用 Elasticsearch 的 HTTP API 对它进行直接查询。

在你的浏览器上访问这个 URL - http://localhost:9200/library/_search?q=text:Java&pretty

在这里,我们将执行一个极简的全文搜索,在我们的图书馆的书中查找 “Java” 这个词。

你将看到类似于下面的一个 JSON 格式的响应。

  1. {

  2.  "took" : 11,

  3.  "timed_out" : false,

  4.  "_shards" : {

  5.    "total" : 5,

  6.    "successful" : 5,

  7.    "skipped" : 0,

  8.    "failed" : 0

  9.  },

  10.  "hits" : {

  11.    "total" : 13,

  12.    "max_score" : 14.259304,

  13.    "hits" : [

  14.      {

  15.        "_index" : "library",

  16.        "_type" : "novel",

  17.        "_id" : "p_GwFWEBaZvLlaAUdQgV",

  18.        "_score" : 14.259304,

  19.        "_source" : {

  20.          "author" : "Charles Darwin",

  21.          "title" : "On the Origin of Species",

  22.          "location" : 1080,

  23.          "text" : "Java, plants of, 375."

  24.        }

  25.      },

  26.      {

  27.        "_index" : "library",

  28.        "_type" : "novel",

  29.        "_id" : "wfKwFWEBaZvLlaAUkjfk",

  30.        "_score" : 10.186235,

  31.        "_source" : {

  32.          "author" : "Edgar Allan Poe",

  33.          "title" : "The Works of Edgar Allan Poe",

  34.          "location" : 827,

  35.          "text" : "After many years spent in foreign travel, I sailed in the year 18-- , from the port of Batavia, in the rich and populous island of Java, on a voyage to the Archipelago of the Sunda islands. I went as passenger--having no other inducement than a kind of nervous restlessness which haunted me as a fiend."

  36.        }

  37.      },

  38.      ...

  39.    ]

  40.  }

  41. }

用 Elasticseach 的 HTTP 接口可以测试我们插入的数据是否成功,但是如果直接将这个 API 暴露给 Web 应用程序将有极大的风险。这个 API 将会暴露管理功能(比如直接添加和删除文档),最理想的情况是完全不要对外暴露它。而是写一个简单的 Node.js API 去接收来自客户端的请求,然后(在我们的本地网络中)生成一个正确的查询发送给 Elasticsearch。

5.1 - 查询脚本

我们现在尝试从我们写的 Node.js 脚本中查询 Elasticsearch。

创建一个新文件,server/search.js

  1. const { client, index, type } = require('./connection')

  2. module.exports = {

  3.  /** Query ES index for the provided term */

  4.  queryTerm (term , offset = 0) {

  5.    const body = {

  6.      from: offset,

  7.      query: { match: {

  8.        text: {

  9.          query: term,

  10.          operator: 'and',

  11.          fuzziness: 'auto'

  12.        } } },

  13.      highlight: { fields: { text: {} } }

  14.    }

  15.    return client.search({ index, type, body })

  16.  }

  17. }

我们的搜索模块定义一个简单的 search 函数,它将使用输入的词 match 查询。

这是查询的字段分解 -

◈ from - 允许我们分页查询结果。默认每个查询返回 10 个结果,因此,指定 from: 10 将允许我们取回 10-20 的结果。
◈ query - 这里我们指定要查询的词。
◈ operator - 我们可以修改搜索行为;在本案例中,我们使用 and 操作去对查询中包含所有字元(要查询的词)的结果来确定优先顺序。
◈ fuzziness - 对拼写错误的容错调整,auto 的默认为 fuzziness: 2。模糊值越高,结果越需要更多校正。比如,fuzziness: 1 将允许以 Patricc 为关键字的查询中返回与 Patrick 匹配的结果。
◈ highlights - 为结果返回一个额外的字段,这个字段包含 HTML,以显示精确的文本字集和查询中匹配的关键词。

你可以去浏览 Elastic Full-Text Query DSL[25],学习如何随意调整这些参数,以进一步自定义搜索查询。

6 - API

为了能够从前端应用程序中访问我们的搜索功能,我们来写一个快速的 HTTP API。

6.0 - API 服务器

用以下的内容替换现有的 server/app.js 文件。

  1. const Koa = require('koa')

  2. const Router = require ('koa-router')

  3. const joi = require('joi')

  4. const validate = require('koa-joi-validate')

  5. const search = require('./search')

  6. const app = new Koa()

  7. const router = new Router()

  8. // Log each request to the console

  9. app.use(async (ctx, next) => {

  10.  const start = Date.now()

  11.  await next()

  12.  const ms = Date.now() - start

  13.  console.log(`${ctx.method} ${ctx.url} - ${ms}`)

  14. })

  15. // Log percolated errors to the console

  16. app.on('error', err => {

  17.  console.error('Server Error', err)

  18. })

  19. // Set permissive CORS header

  20. app.use(async (ctx, next) => {

  21.  ctx.set('Access-Control-Allow-Origin', '*')

  22.  return next ()

  23. })

  24. // ADD ENDPOINTS HERE

  25. const port = process.env.PORT || 3000

  26. app

  27.  .use(router.routes())

  28.  .use(router.allowedMethods())

  29.  .listen(port, err => {

  30.    if (err) throw err

  31.    console.log(`App Listening on Port ${port}`)

  32.  })

这些代码将为 Koa.js[12] Node API 服务器导入服务器依赖,设置简单的日志,以及错误处理。

6.1 - 使用查询连接端点

接下来,我们将在服务器上添加一个端点,以便于发布我们的 Elasticsearch 查询功能。

在  server/app.js 文件的 // ADD ENDPOINTS HERE  下面插入下列的代码。

  1. /**

  2. * GET /search

  3. * Search for a term in the library

  4. */

  5. router.get('/search', async (ctx, next) => {

  6.    const { term, offset } = ctx.request.query

  7.    ctx.body = await search.queryTerm(term, offset)

  8.  }

  9. )

使用 docker-compose up -d --build 重启动应用程序。之后在你的浏览器中尝试调用这个搜索端点。比如,http://localhost:3000/search?term=java 这个请求将搜索整个图书馆中提到 “Java” 的内容。

结果与前面直接调用 Elasticsearch HTTP 界面的结果非常类似。

  1. {

  2.    "took": 242,

  3.    "timed_out": false,

  4.    "_shards": {

  5.        "total": 5,

  6.        "successful": 5,

  7.        "skipped": 0,

  8.        "failed": 0

  9.    },

  10.    "hits": {

  11.        "total": 93,

  12.        "max_score": 13.356944,

  13.        "hits": [{

  14.            "_index": "library",

  15.            "_type": "novel",

  16.            "_id": "eHYHJmEBpQg9B4622421",

  17.            "_score": 13.356944,

  18.            "_source": {

  19.                "author": "Charles Darwin",

  20.                "title": "On the Origin of Species",

  21.                "location": 1080,

  22.                "text": "Java, plants of, 375."

  23.            },

  24.            "highlight": {

  25.                "text": ["Java, plants of, 375."]

  26.            }

  27.        }, {

  28.            "_index": "library",

  29.            "_type": "novel",

  30.            "_id": "2HUHJmEBpQg9B462xdNg",

  31.            "_score": 9.030668,

  32.            "_source": {

  33.                "author": "Unknown Author",

  34.                "title": "The King James Bible",

  35.                "location": 186,

  36.                "text": "10:4 And the sons of Javan; Elishah, and Tarshish, Kittim, and Dodanim."

  37.            },

  38.            "highlight": {

  39.                "text": ["10:4 And the sons of Javan; Elishah, and Tarshish, Kittim, and Dodanim."]

  40.            }

  41.        }

  42.        ...

  43.      ]

  44.   }

  45. }

6.2 - 输入校验

这个端点现在还很脆弱 —— 我们没有对请求参数做任何的校验,因此,如果是无效的或者错误的值将使服务器出错。

我们将添加一些使用 Joi[26] 和 Koa-Joi-Validate[27] 库的中间件,以对输入做校验。

  1. /**

  2. * GET /search

  3. * Search for a term in the library

  4. * Query Params -

  5. * term: string under 60 characters

  6. * offset: positive integer

  7. */

  8. router.get('/search',

  9.  validate({

  10.    query: {

  11.      term: joi.string().max(60).required(),

  12.      offset: joi.number().integer().min(0).default(0)

  13.    }

  14.  }),

  15.  async (ctx, next) => {

  16.    const { term, offset } = ctx.request.query

  17.    ctx.body = await search.queryTerm(term, offset)

  18.  }

  19. )

现在,重启服务器,如果你使用一个没有搜索关键字的请求(http://localhost:3000/search),你将返回一个带相关消息的 HTTP 400 错误,比如像 Invalid URL Query - child "term" fails because ["term" is required]

如果想从 Node 应用程序中查看实时日志,你可以运行 docker-compose logs -f api

7 - 前端应用程序

现在我们的 /search 端点已经就绪,我们来连接到一个简单的 Web 应用程序来测试这个 API。

7.0 - Vue.js 应用程序

我们将使用 Vue.js 去协调我们的前端。

添加一个新文件 /public/app.js,去控制我们的 Vue.js 应用程序代码。

  1. const vm = new Vue ({

  2.  el: '#vue-instance',

  3.  data () {

  4.    return {

  5.      baseUrl: 'http://localhost:3000', // API url

  6.      searchTerm: 'Hello World', // Default search term

  7.      searchDebounce: null, // Timeout for search bar debounce

  8.      searchResults: [], // Displayed search results

  9.      numHits: null, // Total search results found

  10.      searchOffset: 0, // Search result pagination offset

  11.      selectedParagraph: null, // Selected paragraph object

  12.      bookOffset: 0, // Offset for book paragraphs being displayed

  13.      paragraphs: [] // Paragraphs being displayed in book preview window

  14.    }

  15.  },

  16.  async created () {

  17.    this.searchResults = await this.search() // Search for default term

  18.  },

  19.  methods: {

  20.    /** Debounce search input by 100 ms */

  21.    onSearchInput () {

  22.      clearTimeout(this.searchDebounce)

  23.      this.searchDebounce = setTimeout(async () => {

  24.        this.searchOffset = 0

  25.        this.searchResults = await this.search()

  26.      }, 100)

  27.    },

  28.    /** Call API to search for inputted term */

  29.    async search () {

  30.      const response = await axios.get(`${this.baseUrl}/search`, { params: { term: this.searchTerm, offset: this.searchOffset } })

  31.      this.numHits = response.data.hits.total

  32.      return response.data.hits.hits

  33.    },

  34.    /** Get next page of search results */

  35.    async nextResultsPage () {

  36.      if (this.numHits > 10) {

  37.        this.searchOffset += 10

  38.        if (this.searchOffset + 10 > this.numHits) { this.searchOffset = this.numHits - 10}

  39.        this.searchResults = await this.search()

  40.        document.documentElement.scrollTop = 0

  41.      }

  42.    },

  43.    /** Get previous page of search results */

  44.    async prevResultsPage () {

  45.      this.searchOffset -= 10

  46.      if (this.searchOffset < 0) { this.searchOffset = 0 }

  47.      this.searchResults = await this.search()

  48.      document.documentElement.scrollTop = 0

  49.    }

  50.  }

  51. })

这个应用程序非常简单 —— 我们只定义了一些共享的数据属性,以及添加了检索和分页搜索结果的方法。为防止每次按键一次都调用 API,搜索输入有一个 100 毫秒的除颤功能。

解释 Vue.js 是如何工作的已经超出了本教程的范围,如果你使用过 Angular 或者 React,其实一些也不可怕。如果你完全不熟悉 Vue,想快速了解它的功能,我建议你从官方的快速指南入手 —— https://vuejs.org/v2/guide/

7.1 - HTML

使用以下的内容替换 /public/index.html 文件中的占位符,以便于加载我们的 Vue.js 应用程序和设计一个基本的搜索界面。

  1. lang="en">

  2.   charset="utf-8">

  3.  Elastic Library

  4.   name="description" content="Literary Classic Search Engine.">

  5.   name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

  6.   href="https://cdnjs.cloudflare.com/ajax/libs/normalize/7.0.0/normalize.min.css" rel="stylesheet" type="text/css" />

  7.   href="https://cdn.muicss.com/mui-0.9.20/css/mui.min.css" rel="stylesheet" type="text/css" />

  8.   href="https://fonts.googleapis.com/css?family=EB+Garamond:400,700|Open+Sans" rel="stylesheet">

  9.   href="styles.css" rel="stylesheet" />

  10. class="app-container" id="vue-instance">
  11.    

  12.    

    class="mui-panel">
  13.      

    class="mui-textfield">
  14.         v-model="searchTerm" type="text" v-on:keyup="onSearchInput()">

  15.        Search

  16.      

  •    

  •    

  •    

    class="mui-panel">
  •      

    class="mui--text-headline">{{ numHits }} Hits

  •      

    class="mui--text-subhead">Displaying Results {{ searchOffset }} - {{ searchOffset + 9 }}

  •    

  •    

  •    

    class="mui-panel pagination-panel">
  •        

  •        

  •    

  •    

  •    

    class="search-results" ref="searchResults">
  •      

    class="mui-panel" v-for="hit in searchResults" v-on:click="showBookModal(hit)">
  •        

    class="mui--text-title" v-html="hit.highlight.text[0]">

  •        

    class="mui-divider" >

  •        

    class="mui--text-subhead">{{ hit._source.title }} - {{ hit._source.author }}

  •        

    class="mui--text-body2">Location {{ hit._source.location }}

  •      

  •    

  •    

  •    

    class="mui-panel pagination-panel">
  •        

  •        

  •    

  •    

  • 7.2 - CSS

    添加一个新文件 /public/styles.css,使用一些自定义的 UI 样式。

    1. body { font-family: 'EB Garamond', serif; }

    2. .mui-textfield > input, .mui-btn, .mui--text-subhead, .mui -panel > .mui--text-headline {

    3.  font-family: 'Open Sans', sans-serif;

    4. }

    5. .all-caps { text-transform: uppercase; }

    6. .app-container { padding: 16px; }

    7. .search-results em { font-weight: bold; }

    8. .book-modal > button { width: 100%; }

    9. .search-results .mui-divider { margin: 14px 0; }

    10. .search-results {

    11.  display: flex;

    12.  flex-direction: row;

    13.  flex-wrap: wrap;

    14.  justify-content: space-around;

    15. }

    16. .search-results > div {

    17.  flex-basis: 45%;

    18.  box-sizing: border-box;

    19.  cursor: pointer;

    20. }

    21. @media (max-width: 600px) {

    22.  .search-results > div { flex-basis: 100%; }

    23. }

    24. .paragraphs-container {

    25.  max-width: 800px;

    26.  margin: 0 auto;

    27.  margin-bottom: 48px;

    28. }

    29. .paragraphs-container .mui--text-body1, .paragraphs-container .mui--text-body2 {

    30.  font-size: 1.8rem;

    31.  line-height: 35px;

    32. }

    33. .book-modal {

    34.  width: 100%;

    35.  height: 100%;

    36.  padding: 40px 10%;

    37.  box-sizing: border-box;

    38.  margin: 0 auto;

    39.  background-color: white;

    40.  overflow-y: scroll;

    41.  position: fixed;

    42.  top: 0;

    43.  left: 0;

    44. }

    45. .pagination-panel {

    46.  display: flex;

    47.  justify-content: space-between;

    48. }

    49. .title-row {

    50.  display: flex;

    51.  justify-content: space-between;

    52.  align-items: flex-end;

    53. }

    54. @media (max-width: 600px) {

    55.  .title-row{

    56.    flex-direction: column;

    57.    text-align: center;

    58.    align-items: center

    59.  }

    60. }

    61. .locations-label {

    62.  text-align: center;

    63.  margin: 8px;

    64. }

    65. .modal-footer {

    66.  position: fixed;

    67.  bottom: 0;

    68.  left: 0;

    69.  width: 100%;

    70.  display: flex;

    71.  justify-content: space-around;

    72.  background: white;

    73. }

    7.3 - 尝试输出

    在你的浏览器中打开 localhost:8080,你将看到一个简单的带结果分页功能的搜索界面。在顶部的搜索框中尝试输入不同的关键字来查看它们的搜索情况。

    preview webapp

    你没有必要重新运行 docker-compose up 命令以使更改生效。本地的 public 目录是装载在我们的 Nginx 文件服务器容器中,因此,在本地系统中前端的变化将在容器化应用程序中自动反映出来。

    如果你尝试点击任何搜索结果,什么反应也没有 —— 因为我们还没有为这个应用程序添加进一步的相关功能。

    8 - 分页预览

    如果能点击每个搜索结果,然后查看到来自书中的内容,那将是非常棒的体验。

    8.0 - 添加 Elasticsearch 查询

    首先,我们需要定义一个简单的查询去从给定的书中获取段落范围。

    在 server/search.js 文件中添加如下的函数到 module.exports 块中。

    1. /** Get the specified range of paragraphs from a book */

    2. getParagraphs (bookTitle, startLocation, endLocation) {

    3.  const filter = [

    4.    { term: { title: bookTitle } },

    5.    { range: { location: { gte: startLocation, lte: endLocation } } }

    6.  ]

    7.  const body = {

    8.    size: endLocation - startLocation,

    9.    sort: { location: 'asc' },

    10.    query: { bool: { filter } }

    11.  }

    12.  return client.search({ index, type, body })

    13. }

    这个新函数将返回给定的书的开始位置和结束位置之间的一个排序后的段落数组。

    8.1 - 添加 API 端点

    现在,我们将这个函数链接到 API 端点。

    添加下列内容到 server/app.js 文件中最初的 /search 端点下面。

    1. /**

    2. * GET /paragraphs

    3. * Get a range of paragraphs from the specified book

    4. * Query Params -

    5. * bookTitle: string under 256 characters

    6. * start: positive integer

    7. * end: positive integer greater than start

    8. */

    9. router.get('/paragraphs',

    10.  validate({

    11.    query: {

    12.      bookTitle: joi.string().max(256).required(),

    13.      start: joi.number().integer().min(0).default(0),

    14.      end: joi.number().integer().greater(joi.ref('start')).default(10)

    15.    }

    16.  }),

    17.  async (ctx, next) => {

    18.    const { bookTitle, start, end } = ctx.request.query

    19.    ctx.body = await search.getParagraphs(bookTitle, start, end)

    20.  }

    21. )

    8.2 - 添加 UI 功能

    现在,我们的新端点已经就绪,我们为应用程序添加一些从书中查询和显示全部页面的前端功能。

    在 /public/app.js 文件的 methods 块中添加如下的函数。

    1.    /** Call the API to get current page of paragraphs */

    2.    async getParagraphs (bookTitle, offset) {

    3.      try {

    4.        this.bookOffset = offset

    5.        const start = this.bookOffset

    6.        const end = this.bookOffset + 10

    7.        const response = await axios.get(`${this.baseUrl}/paragraphs`, { params: { bookTitle, start, end } })

    8.        return response.data.hits.hits

    9.      } catch (err) {

    10.        console.error(err)

    11.      }

    12.    },

    13.    /** Get next page (next 10 paragraphs) of selected book */

    14.    async nextBookPage () {

    15.      this.$refs.bookModal.scrollTop = 0

    16.      this.paragraphs = await this.getParagraphs(this.selectedParagraph._source.title, this.bookOffset + 10)

    17.    },

    18.    /** Get previous page (previous 10 paragraphs) of selected book */

    19.    async prevBookPage () {

    20.      this.$refs.bookModal.scrollTop = 0

    21.      this.paragraphs = await this.getParagraphs(this.selectedParagraph._source.title, this.bookOffset - 10)

    22.    },

    23.    /** Display paragraphs from selected book in modal window */

    24.    async showBookModal (searchHit) {

    25.      try {

    26.        document.body.style.overflow = 'hidden'

    27.        this.selectedParagraph = searchHit

    28.        this.paragraphs = await this.getParagraphs(searchHit._source.title, searchHit._source.location - 5)

    29.      } catch (err) {

    30.        console.error(err)

    31.      }

    32.    },

    33.     /** Close the book detail modal */

    34.    closeBookModal () {

    35.      document.body.style.overflow = 'auto'

    36.      this.selectedParagraph = null

    37.    }

    这五个函数提供了通过页码从书中下载和分页(每次十个段落)的逻辑。

    现在,我们需要添加一个 UI 去显示书的页面。在 /public/index.html 的  注释下面添加如下的内容。

    1.    

    2.    

      v-if="selectedParagraph" ref="bookModal" class="book-modal">
    3.      

      class="paragraphs-container">
    4.        

    5.        

      class="title-row">
    6.          

      class= "mui--text-display2 all-caps">{{ selectedParagraph._source.title }}

    7.          

      class="mui--text-display1">{{ selectedParagraph._source.author }}

    8.        

    9.        

    10.        

      class="mui-divider">

    11.        

      class="mui--text-subhead locations-label">Locations {{ bookOffset - 5 }} to {{ bookOffset + 5 }}

    12.        

      class="mui-divider">

    13.        

    14.        

    15.        

      v-for="paragraph in paragraphs">
    16.          

      v-if="paragraph._source.location === selectedParagraph._source.location" class="mui--text-body2">
    17.            {{ paragraph._source.text }}

    18.          

    19.          

      v-else class="mui--text-body1">
    20.            {{ paragraph._source.text }}

    21.          

    22.          

    23.        

    24.      

    25.      

    26.      

      class="modal-footer">
    27.        

    28.        

    29.        

    30.      

    31.    

    再次重启应用程序服务器(docker-compose up -d --build),然后打开 localhost:8080。当你再次点击搜索结果时,你将能看到关键字附近的段落。如果你感兴趣,你现在甚至可以看这本书的剩余部分。

    preview webapp book page

    祝贺你!你现在已经完成了本教程的应用程序。

    你可以去比较你的本地结果与托管在这里的完整示例 —— https://search.patricktriest.com/[1]

    9 - Elasticsearch 的缺点

    9.0 - 耗费资源

    Elasticsearch 是计算密集型的。官方建议[29] 运行 ES 的机器最好有 64 GB 的内存,强烈反对在低于 8 GB 内存的机器上运行它。Elasticsearch 是一个 内存中 数据库,这样使它的查询速度非常快,但这也非常占用系统内存。在生产系统中使用时,他们强烈建议在一个集群中运行多个 Elasticsearch 节点[30],以实现高可用、自动分区和一个节点失败时的数据冗余。

    我们的这个教程中的应用程序运行在一个 $15/月 的 GCP 计算实例中( search.patricktriest.com[1]),它只有 1.7 GB 的内存,它勉强能运行这个 Elasticsearch 节点;有时候在进行初始的数据加载过程中,整个机器就 ”假死机“ 了。在我的经验中,Elasticsearch 比传统的那些数据库,比如,PostgreSQL 和 MongoDB 耗费的资源要多很多,这样会使托管主机的成本增加很多。

    9.1 - 与数据库的同步

    对于大多数应用程序,将数据全部保存在 Elasticsearch 并不是个好的选择。可以使用 ES 作为应用程序的主要事务数据库,但是一般不推荐这样做,因为在 Elasticsearch 中缺少 ACID,如果大量读取数据的时候,它能导致写操作丢失。在许多案例中,ES 服务器更多是一个特定的角色,比如做应用程序中的一个文本搜索功能。这种特定的用途,要求它从主数据库中复制数据到 Elasticsearch 实例中。

    比如,假设我们将用户信息保存在一个 PostgreSQL 表中,但是用 Elasticsearch 去提供我们的用户搜索功能。如果一个用户,比如,“Albert”,决定将他的名字改成 “Al”,我们将需要把这个变化同时反映到我们主要的 PostgreSQL 数据库和辅助的 Elasticsearch 集群中。

    正确地集成它们可能比较棘手,最好的答案将取决于你现有的应用程序栈。这有多种开源方案可选,从 用一个进程去关注 MongoDB 操作日志[31] 并自动同步检测到的变化到 ES,到使用一个 PostgresSQL 插件[32] 去创建一个定制的、基于 PSQL 的索引来与 Elasticsearch 进行自动沟通。

    如果没有有效的预构建选项可用,你可能需要在你的服务器代码中增加一些钩子,这样可以基于数据库的变化来手动更新 Elasticsearch 索引。最后一招,我认为是一个最后的选择,因为,使用定制的业务逻辑去保持 ES 的同步可能很复杂,这将会给应用程序引入很多的 bug。

    让 Elasticsearch 与一个主数据库同步,将使它的架构更加复杂,其复杂性已经超越了 ES 的相关缺点,但是当在你的应用程序中考虑添加一个专用的搜索引擎的利弊得失时,这个问题是值的好好考虑的。

    总结

    在很多现在流行的应用程序中,全文搜索是一个非常重要的功能 —— 而且是很难实现的一个功能。对于在你的应用程序中添加一个快速而又可定制的文本搜索,Elasticsearch 是一个非常好的选择,但是,在这里也有一个替代者。Apache Solr[33] 是一个类似的开源搜索平台,它是基于 Apache Lucene 构建的,与 Elasticsearch 的核心库是相同的。Algolia[34] 是一个搜索即服务的 Web 平台,它已经很快流行了起来,并且它对新手非常友好,很易于上手(但是作为折衷,它的可定制性较小,并且使用成本较高)。

    “搜索” 特性并不是 Elasticsearch 唯一功能。ES 也是日志存储和分析的常用工具,在一个 ELK(Elasticsearch、Logstash、Kibana)架构配置中通常会使用它。灵活的全文搜索功能使得 Elasticsearch 在数据量非常大的科学任务中用处很大 —— 比如,在一个数据集中正确的/标准化的条目拼写,或者为了类似的词组搜索一个文本数据集。

    对于你自己的项目,这里有一些创意。

    ◈ 添加更多你喜欢的书到教程的应用程序中,然后创建你自己的私人图书馆搜索引擎。
    ◈ 利用来自 Google Scholar[35] 的论文索引,创建一个学术抄袭检测引擎。
    ◈ 通过将字典中的每个词索引到 Elasticsearch,创建一个拼写检查应用程序。
    ◈ 通过将 Common Crawl Corpus[36] 加载到 Elasticsearch 中,构建你自己的与谷歌竞争的因特网搜索引擎(注意,它可能会超过 50 亿个页面,这是一个成本极高的数据集)。
    ◈ 在 journalism 上使用 Elasticsearch:在最近的大规模泄露的文档中搜索特定的名字和关键词,比如, Panama Papers[37] 和 Paradise Papers[38]

    本教程中应用程序的源代码是 100% 公开的,你可以在 GitHub 仓库上找到它们 —— https://github.com/triestpa/guttenberg-search

    我希望你喜欢这个教程!你可以在下面的评论区,发表任何你的想法、问题、或者评论。


    作者简介:

    全栈工程师,数据爱好者,学霸,“构建强迫症患者”,探险爱好者。


    via: https://blog.patricktriest.com/text-search-docker-elasticsearch/

    作者:Patrick Triest[40] 译者:qhwdw 校对:wxy

    本文由 LCTT 原创编译,Linux中国 荣誉推出



    今天看啥 - 高品质阅读平台
    本文地址:http://www.jintiankansha.me/t/aawf66cso3
    Python社区是高质量的Python/Django开发社区
    本文地址:http://www.python88.com/topic/11341
    Reply
     
    425 次点击  
    登录后回复