一、基础认识
- Elasticsearch是一个分布式的开源搜索和分析引擎,可以快速地存储、搜索和分析大量数据。
- Elasticsearch是基于Lucene库构建的,Lucene是一个高性能、可扩展、全文搜索库,Elasticsearch在其基础上进行改善。
- Elasticsearch使用分布式架构,可以在多个节点上存储和处理数据,使得可以横向扩展。
- Elasticsearch具有强大的查询能力,支持全文检索、复合查询、地理位置查询等各种查询方式。
二、使用Elasticsearch
- 安装和配置Elasticsearch
// 下载Elasticsearch并解压
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.14.0-linux-x86_64.tar.gz
tar -zxvf elasticsearch-7.14.0-linux-x86_64.tar.gz
// 修改配置文件config/elasticsearch.yml
cluster.name: my_elasticsearch_cluster
node.name: my_elasticsearch_node
network.host: 0.0.0.0
- 索引操作
// 创建索引
PUT /my_index
// 在指定索引中创建文档
PUT /my_index/_doc/1
{
"title": "Elasticsearch",
"content": "Elasticsearch是一个分布式的开源搜索和分析引擎"
}
// 查询指定文档
GET /my_index/_doc/1
// 搜索索引中的文档
GET /my_index/_search
{
"query": {
"match": {
"title": "Elasticsearch"
}
}
}
三、数据模型
- 索引是Elasticsearch数据的最小单位,相当于关系型数据库中的表,但索引中可以包含不同类型的文档。
- 文档是Elasticsearch中最基本的单位,相当于关系型数据库中的行,文档和索引一一对应。
- 字段是文档中的属性,可以是字符串、数字、日期、布尔值等多种类型,可以嵌套。
- 类型已经在Elasticsearch 7.0版本后被弃用,文档可以直接存放在索引中,不再需要指定类型。
四、查询操作
- 全文搜索
// 匹配查询
GET /my_index/_search
{
"query": {
"match": {
"title": "search"
}
}
}
// 短语匹配查询
GET /my_index/_search
{
"query": {
"match_phrase": {
"title": "Elasticsearch search"
}
}
}
// 多字段匹配查询
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "search",
"fields": ["title", "content"]
}
}
}
- 过滤器
// 范围查询
GET /my_index/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
}
}
}
// 地理位置查询
GET /my_index/_search
{
"query": {
"bool": {
"filter": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 40,
"lon": -70
}
}
}
}
}
}
五、聚合查询
- 常用聚合
// 分组统计
GET /my_index/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age"
}
}
}
}
// 嵌套聚合
GET /my_index/_search
{
"size": 0,
"aggs": {
"group_by_age": {
"terms": {
"field": "age"
},
"aggs": {
"avg_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
- Pipeline聚合
// 计算移动平均值
GET /my_index/_search
{
"size": 0,
"aggs": {
"moving_avg_balance": {
"moving_avg": {
"buckets_path": "group_by_age>avg_balance",
"window": 2,
"model": "simple"
}
}
}
}
六、高级技巧
- 分片和副本
- 自定义分析
// 自定义分析器
PUT /my_analyzer_example
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "my_stemmer"]
}
},
"filter": {
"my_stemmer": {
"type": "stemmer",
"language": "light_english"
}
}
}
}
}
// 测试分析器
GET /my_analyzer_example/_analyze
{
"analyzer": "my_analyzer",
"text": "Stemmers remove morphological affixes from words, leaving only the word stem."
}
- 词向量分析
// 添加词向量
PUT /my_index
{
"settings": {
"analysis": {
"tokenizer": {
"my_tokenizer": {
"type": "whitespace",
"filter": ["my_w2v_filter"]
}
},
"filter": {
"my_w2v_filter": {
"type": "word2vec",
"model_name": "my_model",
"vector_size": 300,
"window_size": 5,
"min_count": 5,
"top_terms": 10,
"term_weighting": "tf-idf"
}
},
"word2vec": {
"models": {
"my_model": {
"source": "https://s3.amazonaws.com/models.huggingface.co/bert/bert-base-uncased.tar.gz",
"preprocessing": {
"tokenizer": "bert-base-uncased"
}
}
}
}
}
},
"mappings": {
"properties": {
"text": {
"type": "text",
"analyzer": "my_analyzer",
"term_vector": "with_positions_offsets",
"store": true
}
}
}
}
七、总结
本篇文章从基础认识、使用Elasticsearch、数据模型、查询操作、聚合查询和高级技巧的角度,对Elasticsearch进行了全面的阐述。希望可以帮助读者更好地理解和使用Elasticsearch。