elastic search 学习

reference:https://www.elastic.co/docs

ElasticSearch Overview

ElasticSearch is a distributed, RESTful search and analytics engine built on Apache Lucene. It stores, searches, and analyzes large volumes of data quickly and in near real-time.


Principle

  • Indexing: Data is stored in indices, which are collections of documents. Each document is a JSON object.
  • Sharding & Replication: Indices are split into shards for scalability and replicated for fault tolerance.
  • Search: Uses inverted indices for fast full-text search.
  • RESTful API: All operations (CRUD, search, aggregation) are exposed via HTTP endpoints.

Usage

  1. Install ElasticSearch

    • Download from elastic.co
    • Start with bin/elasticsearch
  2. Basic Concepts

    • Index: Like a database.
    • Type: Deprecated, previously like a table.
    • Document: A JSON object.
    • Field: Key-value pairs in a document.

API Integration

ElasticSearch exposes a RESTful API. You can use curl, Postman, or any HTTP client.

1. Index a Document
bash 复制代码
curl -X POST "localhost:9200/my_index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "title": "ElasticSearch Guide",
  "content": "ElasticSearch is a search engine."
}
'
2. Get a Document
bash 复制代码
curl -X GET "localhost:9200/my_index/_doc/1"
bash 复制代码
curl -X GET "localhost:9200/my_index/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match": {
      "content": "search engine"
    }
  }
}
'
4. Update a Document
bash 复制代码
curl -X POST "localhost:9200/my_index/_update/1" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "title": "Updated Title"
  }
}
'
5. Delete a Document
bash 复制代码
curl -X DELETE "localhost:9200/my_index/_doc/1"

Java API Example

Add dependency in pom.xml:

xml 复制代码
<dependency>
  <groupId>org.elasticsearch.client</groupId>
  <artifactId>elasticsearch-rest-high-level-client</artifactId>
  <version>7.17.0</version>
</dependency>

Sample code:

java 复制代码
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.xcontent.XContentType;

RestHighLevelClient client = new RestHighLevelClient(
    RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

IndexRequest request = new IndexRequest("my_index").id("1")
    .source("{\"title\":\"ElasticSearch Guide\"}", XContentType.JSON);

IndexResponse response = client.index(request, RequestOptions.DEFAULT);
client.close();

Summary:

ElasticSearch is a scalable, distributed search engine. Use its RESTful API for CRUD and search operations. Integrate via HTTP or official clients (Java, Python, etc.).

相关推荐
好奇龙猫18 小时前
【AI学习-comfyUI学习-第三十节-第三十一节-FLUX-SD放大工作流+FLUX图生图工作流-各个部分学习】
人工智能·学习
saoys19 小时前
Opencv 学习笔记:图像掩膜操作(精准提取指定区域像素)
笔记·opencv·学习
电子小白12320 小时前
第13期PCB layout工程师初级培训-1-EDA软件的通用设置
笔记·嵌入式硬件·学习·pcb·layout
唯情于酒20 小时前
Docker学习
学习·docker·容器
charlie11451419121 小时前
嵌入式现代C++教程: 构造函数优化:初始化列表 vs 成员赋值
开发语言·c++·笔记·学习·嵌入式·现代c++
IT=>小脑虎1 天前
C++零基础衔接进阶知识点【详解版】
开发语言·c++·学习
#眼镜&1 天前
嵌入式学习之路2
学习
码农小韩1 天前
基于Linux的C++学习——指针
linux·开发语言·c++·学习·算法
微露清风1 天前
系统性学习C++-第十九讲-unordered_map 和 unordered_set 的使用
开发语言·c++·学习
wdfk_prog1 天前
[Linux]学习笔记系列 -- [fs]seq_file
linux·笔记·学习