1. Meilisearch 简介
Meilisearch 是一个强大、快速且开源的搜索引擎,专为现代应用程序设计。它提供了即时搜索体验,具有拼写容错和自然语言理解能力,并且非常容易部署和使用。
与传统搜索引擎不同,Meilisearch 专注于最终用户体验,提供毫秒级的搜索响应和高度相关的搜索结果。它不需要复杂的配置就能提供出色的搜索体验,同时也提供了丰富的自定义选项。
Meilisearch 主要特点
- 即时搜索(边输入边搜索)
- 高度相关的搜索结果排序
- 强大的全文搜索能力
- 拼写错误和错别字容忍
- 支持多种语言,包括中文
- 易于部署和集成
- RESTful API 接口
- 可自定义的排名规则
- 分面搜索支持
- 极低的资源占用
Meilisearch 的设计理念是"search-as-you-type"(即时搜索),它能够在用户输入过程中提供实时反馈,显著提升用户体验。相比 Elasticsearch 这样的企业级搜索解决方案,Meilisearch 更加轻量级,设置和维护成本更低,非常适合中小型应用。
2. 安装与配置
Meilisearch 提供了多种安装方式,可以根据你的环境和需求选择最合适的方式。
使用 Docker 安装(推荐)
Docker 是最简单和推荐的安装方式,特别是在开发环境中:
docker run -it --rm \
-p 7700:7700 \
-v $(pwd)/meili_data:/meili_data \
getmeili/meilisearch:latest
上述命令会下载最新版本的 Meilisearch 并在端口 7700 上启动服务,数据将持久化存储在当前目录的 meili_data 文件夹中。
二进制安装
对于 Linux 和 macOS 系统,可以使用 curl 下载并安装 Meilisearch:
# 下载二进制文件
curl -L https://install.meilisearch.com | sh
# 启动 Meilisearch 服务
./meilisearch
Brew 安装 (macOS)
brew update && brew install meilisearch
meilisearch
Windows 安装
Windows 用户可以从 GitHub 发布页面 下载最新的 Windows 二进制文件。
配置选项
启动 Meilisearch 时可以指定多种配置选项:
meilisearch --db-path ./meili_data --http-addr 127.0.0.1:7700 --master-key YOUR_MASTER_KEY
安全提示
在生产环境中,强烈建议设置 master-key 以保护您的 Meilisearch 实例。没有设置主密钥的实例对所有人开放,可能导致数据泄露或丢失。
成功安装后,可以通过访问 http://localhost:7700
来确认 Meilisearch 是否正常运行。如果看到 Meilisearch 的欢迎页面,说明安装成功。
3. 基本使用
一旦安装并启动了 Meilisearch,就可以开始使用它的 API 进行操作。Meilisearch 提供了 RESTful API,你可以使用 HTTP 请求直接与其交互,也可以使用官方提供的客户端库。
3.1 文档管理
在 Meilisearch 中,数据以文档(documents)形式存储。文档是 JSON 对象,每个文档必须有一个唯一标识符。
添加文档
以下是使用 HTTP API 添加文档的示例:
curl \
-X POST 'http://localhost:7700/indexes/movies/documents' \
-H 'Content-Type: application/json' \
--data-binary '[
{
"id": 1,
"title": "卧虎藏龙",
"director": "李安",
"genres": ["动作", "冒险", "奇幻"],
"release_year": 2000
},
{
"id": 2,
"title": "红高粱",
"director": "张艺谋",
"genres": ["剧情", "历史", "爱情"],
"release_year": 1988
}
]'
使用 JavaScript 客户端库添加文档:
import { MeiliSearch } from 'meilisearch'
const client = new MeiliSearch({
host: 'http://localhost:7700'
})
const documents = [
{
id: 1,
title: '卧虎藏龙',
director: '李安',
genres: ['动作', '冒险', '奇幻'],
release_year: 2000
},
{
id: 2,
title: '红高粱',
director: '张艺谋',
genres: ['剧情', '历史', '爱情'],
release_year: 1988
}
]
// 如果索引不存在,会自动创建
client.index('movies').addDocuments(documents)
.then((res) => console.log(res))
.catch((err) => console.error(err))
更新文档
更新文档与添加类似,如果文档 ID 已存在,则会更新该文档:
client.index('movies').updateDocuments([
{
id: 1,
rating: 8.9 // 只更新 rating 字段
}
])
删除文档
通过 ID 删除单个或多个文档:
// 删除单个文档
client.index('movies').deleteDocument(1)
// 删除多个文档
client.index('movies').deleteDocuments([1, 2, 3])
或使用 HTTP API:
curl -X DELETE 'http://localhost:7700/indexes/movies/documents/1'
3.2 索引操作
索引(index)是 Meilisearch 中存储文档的容器。每个索引都有自己的设置和配置。
创建索引
client.createIndex('books', { primaryKey: 'isbn' })
.then((index) => {
console.log(`Index ${index.uid} 创建成功!`)
})
使用 HTTP API:
curl \
-X POST 'http://localhost:7700/indexes' \
-H 'Content-Type: application/json' \
--data-binary '{
"uid": "books",
"primaryKey": "isbn"
}'
提示
primaryKey 参数指定了文档的唯一标识字段。如果不指定,Meilisearch 会尝试使用 "id" 字段。建议在创建索引时明确指定 primaryKey。
获取索引信息
client.getIndex('books')
.then((index) => {
console.log(index)
})
列出所有索引
client.getIndexes().then((indexes) => {
console.log(indexes)
})
删除索引
client.deleteIndex('books')
3.3 搜索功能
搜索是 Meilisearch 的核心功能,支持多种搜索选项和过滤器。
基本搜索
最简单的搜索只需要一个查询字符串:
client.index('movies').search('张艺谋')
.then((res) => {
console.log(res.hits) // 搜索结果
console.log(res.nbHits) // 结果总数
console.log(res.processingTimeMs) // 处理时间
})
使用 HTTP API:
curl -X GET 'http://localhost:7700/indexes/movies/search?q=张艺谋'
高级搜索选项
Meilisearch 支持多种高级搜索选项:
client.index('movies').search('红', {
limit: 20, // 返回结果数量限制
offset: 0, // 分页起始位置
attributesToRetrieve: ['title', 'director', 'release_year'], // 返回的字段
attributesToHighlight: ['title'], // 高亮匹配的字段
filter: 'release_year > 1990', // 过滤条件
sort: ['release_year:desc'] // 排序条件
})
参数名 | 描述 | 示例 |
---|---|---|
q | 搜索查询 | q=红高粱 |
limit | 结果数量限制 | limit=20 |
offset | 分页起始位置 | offset=40 |
filter | 结果过滤条件 | filter=director = '张艺谋' |
sort | 结果排序 | sort=release_year:desc |
4. 高级功能
Meilisearch 提供了多种高级功能,以满足不同的搜索需求和场景。
4.1 相关性调优
相关性调优是提高搜索结果质量的关键。Meilisearch 提供了多种方法来调整搜索结果的相关性。
使用 ranking rules
Meilisearch 允许你定义自己的 ranking rules,以控制搜索结果的排序。
client.index('movies').updateRankingRules([
{
"name": "custom_rule",
"type": "words",
"value": {
"words": ["张艺谋"],
"typo": true
}
}
])
使用 filterable attributes
通过指定 filterable attributes,可以限制搜索结果的范围。
client.index('movies').updateFilterableAttributes(['director'])
使用 searchable attributes
通过指定 searchable attributes,可以控制哪些字段可以被搜索。
client.index('movies').updateSearchableAttributes(['title', 'director'])
4.2 分面搜索
分面搜索是一种强大的搜索功能,可以按不同的维度对搜索结果进行分类和过滤。
使用 facets
通过使用 facets,可以按不同的属性对搜索结果进行过滤和聚合。
client.index('movies').search('张艺谋', {
facets: ['director']
})
使用 facet exclusion
通过使用 facet exclusion,可以排除某些分面,从而缩小搜索结果的范围。
client.index('movies').search('张艺谋', {
facets: ['director'],
facetFilters: ['!director:李安']
})
4.3 拼写容错
拼写容错是提高搜索体验的重要功能,可以处理用户输入中的拼写错误。
使用 typo tolerance
通过使用 typo tolerance,可以处理用户输入中的拼写错误,并返回相关的结果。
client.index('movies').search('张艺谋', {
typoTolerance: {
enabled: true,
minWordSize: {
oneTypo: 3,
twoTypos: 7
},
disableOnAttributes: ['director']
}
})
5. 实战案例
以下是一些实际应用 Meilisearch 的案例,展示了它在不同场景下的强大功能。
案例一:电影推荐系统
在电影推荐系统中,Meilisearch 可以用于快速搜索和推荐相关电影。
client.index('movies').search('张艺谋')
.then((res) => {
console.log(res.hits) // 搜索结果
console.log(res.nbHits) // 结果总数
console.log(res.processingTimeMs) // 处理时间
})
案例二:电商搜索
在电商平台上,Meilisearch 可以用于快速搜索和过滤商品。
client.index('products').search('手机')
.then((res) => {
console.log(res.hits) // 搜索结果
console.log(res.nbHits) // 结果总数
console.log(res.processingTimeMs) // 处理时间
})
案例三:新闻搜索
在新闻平台上,Meilisearch 可以用于快速搜索和过滤新闻文章。
client.index('news').search('人工智能')
.then((res) => {
console.log(res.hits) // 搜索结果
console.log(res.nbHits) // 结果总数
console.log(res.processingTimeMs) // 处理时间
})
6. 性能优化
性能优化是确保搜索引擎高效运行的关键。以下是一些提高 Meilisearch 性能的技巧和建议。
使用索引
索引是 Meilisearch 中存储文档的容器。通过创建和使用索引,可以显著提高搜索性能。
client.createIndex('books', { primaryKey: 'isbn' })
.then((index) => {
console.log(`Index ${index.uid} 创建成功!`)
})
使用过滤器
通过使用过滤器,可以缩小搜索结果的范围,从而提高搜索性能。
client.index('movies').search('张艺谋', {
filter: 'release_year > 1990'
})
使用分页
通过使用分页,可以减少每次搜索返回的结果数量,从而提高搜索性能。
client.index('movies').search('张艺谋', {
limit: 20
})
7. 与其他搜索引擎对比
以下是 Meilisearch 与其他一些流行搜索引擎的对比,以帮助你选择最适合你需求的搜索引擎。
与 Elasticsearch 对比
Elasticsearch 是一个功能强大的企业级搜索引擎,但它的设置和维护成本较高。相比之下,Meilisearch 更加轻量级,设置和维护成本更低,非常适合中小型应用。
与 Solr 对比
Solr 是一个功能强大的开源搜索引擎,但它的设置和维护成本较高。相比之下,Meilisearch 更加轻量级,设置和维护成本更低,非常适合中小型应用。
与 Algolia 对比
Algolia 是一个功能强大的企业级搜索引擎,但它的设置和维护成本较高。相比之下,Meilisearch 更加轻量级,设置和维护成本更低,非常适合中小型应用。
8. 总结与展望
Meilisearch 是一个强大、快速且开源的搜索引擎,专为现代应用程序设计。它提供了即时搜索体验,具有拼写容错和自然语言理解能力,并且非常容易部署和使用。
与传统搜索引擎不同,Meilisearch 专注于最终用户体验,提供毫秒级的搜索响应和高度相关的搜索结果。它不需要复杂的配置就能提供出色的搜索体验,同时也提供了丰富的自定义选项。
Meilisearch 的设计理念是"search-as-you-type"(即时搜索),它能够在用户输入过程中提供实时反馈,显著提升用户体验。相比 Elasticsearch 这样的企业级搜索解决方案,Meilisearch 更加轻量级,设置和维护成本更低,非常适合中小型应用。