一、基礎認識
1、Elasticsearch是一個分布式的開源搜索和分析引擎,可以快速地存儲、搜索和分析大量數據。
2、Elasticsearch是基於Lucene庫構建的,Lucene是一個高性能、可擴展、全文搜索庫,Elasticsearch在其基礎上進行改善。
3、Elasticsearch使用分布式架構,可以在多個節點上存儲和處理數據,使得可以橫向擴展。
4、Elasticsearch具有強大的查詢能力,支持全文檢索、複合查詢、地理位置查詢等各種查詢方式。
二、使用Elasticsearch
1、安裝和配置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
2、索引操作
// 創建索引
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"
}
}
}
三、數據模型
1、索引是Elasticsearch數據的最小單位,相當於關係型數據庫中的表,但索引中可以包含不同類型的文檔。
2、文檔是Elasticsearch中最基本的單位,相當於關係型數據庫中的行,文檔和索引一一對應。
3、字段是文檔中的屬性,可以是字符串、數字、日期、布爾值等多種類型,可以嵌套。
4、類型已經在Elasticsearch 7.0版本後被棄用,文檔可以直接存放在索引中,不再需要指定類型。
四、查詢操作
1、全文搜索
// 匹配查詢
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"]
}
}
}
2、過濾器
// 範圍查詢
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
}
}
}
}
}
}
五、聚合查詢
1、常用聚合
// 分組統計
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"
}
}
}
}
}
}
2、Pipeline聚合
// 計算移動平均值
GET /my_index/_search
{
"size": 0,
"aggs": {
"moving_avg_balance": {
"moving_avg": {
"buckets_path": "group_by_age>avg_balance",
"window": 2,
"model": "simple"
}
}
}
}
六、高級技巧
1、分片和副本
2、自定義分析
// 自定義分析器
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."
}
3、詞向量分析
// 添加詞向量
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。
原創文章,作者:CFSNR,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/317864.html
微信掃一掃
支付寶掃一掃