Elasticsearch文檔全面解析

一、基礎認識

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-tw/n/317864.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
CFSNR的頭像CFSNR
上一篇 2025-01-11 16:28
下一篇 2025-01-11 16:28

相關推薦

  • Python應用程序的全面指南

    Python是一種功能強大而簡單易學的編程語言,適用於多種應用場景。本篇文章將從多個方面介紹Python如何應用於開發應用程序。 一、Web應用程序 目前,基於Python的Web…

    編程 2025-04-29
  • 使用Spire.PDF進行PDF文檔處理

    Spire.PDF是一款C#的PDF庫,它可以幫助開發者快速、簡便地處理PDF文檔。本篇文章將會介紹Spire.PDF庫的一些基本用法和常見功能。 一、PDF文檔創建 創建PDF文…

    編程 2025-04-29
  • Python zscore函數全面解析

    本文將介紹什麼是zscore函數,它在數據分析中的作用以及如何使用Python實現zscore函數,為讀者提供全面的指導。 一、zscore函數的概念 zscore函數是一種用於標…

    編程 2025-04-29
  • 全面解讀數據屬性r/w

    數據屬性r/w是指數據屬性的可讀/可寫性,它在程序設計中扮演著非常重要的角色。下面我們從多個方面對數據屬性r/w進行詳細的闡述。 一、r/w的概念 數據屬性r/w即指數據屬性的可讀…

    編程 2025-04-29
  • Python計算機程序代碼全面介紹

    本文將從多個方面對Python計算機程序代碼進行詳細介紹,包括基礎語法、數據類型、控制語句、函數、模塊及面向對象編程等。 一、基礎語法 Python是一種解釋型、面向對象、動態數據…

    編程 2025-04-29
  • Python爬蟲文檔報告

    本文將從多個方面介紹Python爬蟲文檔的相關內容,包括:爬蟲基礎知識、爬蟲框架及常用庫、爬蟲實戰等。 一、爬蟲基礎知識 1、爬蟲的定義: 爬蟲是一種自動化程序,通過模擬人的行為在…

    編程 2025-04-28
  • Matlab二值圖像全面解析

    本文將全面介紹Matlab二值圖像的相關知識,包括二值圖像的基本原理、如何對二值圖像進行處理、如何從二值圖像中提取信息等等。通過本文的學習,你將能夠掌握Matlab二值圖像的基本操…

    編程 2025-04-28
  • 瘋狂Python講義的全面掌握與實踐

    本文將從多個方面對瘋狂Python講義進行詳細的闡述,幫助讀者全面了解Python編程,掌握瘋狂Python講義的實現方法。 一、Python基礎語法 Python基礎語法是學習P…

    編程 2025-04-28
  • 全面解析Python中的Variable

    Variable是Python中常見的一個概念,是我們在編程中經常用到的一個變數類型。Python是一門強類型語言,即每個變數都有一個對應的類型,不能無限制地進行類型間轉換。在本篇…

    編程 2025-04-28
  • Zookeeper ACL 用戶 anyone 全面解析

    本文將從以下幾個方面對Zookeeper ACL中的用戶anyone進行全面的解析,並為讀者提供相關的示例代碼。 一、anyone 的作用是什麼? 在Zookeeper中,anyo…

    編程 2025-04-28

發表回復

登錄後才能評論