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.).

相关推荐
upper20206 小时前
【炒股学习】集合竞价
学习·区块链
以孝治家行动6 小时前
线上共读传孝道 身体力行润家风——以孝治家家教中心开展线上学习
学习
小智RE0-走在路上8 小时前
Python学习笔记(8) --函数的多返回值,不同传参,匿名函数
笔记·python·学习
charlie1145141919 小时前
现代C++嵌入式教程:C++98基础特性:从C到C++的演进(1)
c语言·开发语言·c++·笔记·学习·教程
Elastic 中国社区官方博客9 小时前
让我们把这个 expense 工具从 n8n 迁移到 Elastic One Workflow
大数据·运维·elasticsearch·搜索引擎·ai·信息可视化·全文检索
喜欢吃豆10 小时前
我把 LLM 技术栈做成了一张“可复用的认知地图”:notes-on-llms 开源仓库介绍
学习·语言模型·架构·开源·大模型·多模态
学烹饪的小胡桃10 小时前
【运维学习】实时性能监控工具 WGCLOUD v3.6.2 更新介绍
linux·运维·服务器·学习·工单系统
nnsix10 小时前
QFramework学习笔记
笔记·学习
我想我不够好。10 小时前
电工实操 电路的接线和理解以及练习 12.21
学习
ys~~10 小时前
git学习
git·vscode·python·深度学习·学习·nlp·github