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

相关推荐
乔代码嘚1 分钟前
Agentic-KGR:多智能体强化学习驱动的知识图谱本体渐进式扩展技术
人工智能·学习·大模型·知识图谱·ai大模型·大模型学习·大模型教程
zhangrelay1 小时前
三分钟云课实践速通--模拟电子技术-模电--SimulIDE
linux·笔记·学习·ubuntu·lubuntu
木木_王1 小时前
嵌入式Linux学习 | 数据结构 (Day05) 栈与队列详解(原理 + C 语言实现 + 实战实验 + 易错点剖析)
linux·c语言·开发语言·数据结构·笔记·学习
OSwich1 小时前
【 Godot 4 学习笔记】数组(Array)
笔记·学习·godot
测试那点事儿1 小时前
第2章零基础接口自动化到 Jenkins 持续集成【本地环境准备与首次跑通】
ci/cd·自动化·jenkins
程序员-小李2 小时前
uv 学习总结:从零到一掌握现代化 Python 工具链
python·学习·uv
nashane2 小时前
HarmonyOS 6学习:页面跳转弹窗状态保持全解析
学习·华为·harmonyos·harmonyos 5
山楂树の2 小时前
图像标注大坑:img图片 + Canvas 叠加标注,同步放大后标注位置偏移、对不齐?详解修复方案及亚像素处理原理
前端·css·学习·canva可画
小郑加油3 小时前
python学习Day10天:列表进阶 + 内置函数 + 代码简化
开发语言·python·学习
Elastic 中国社区官方博客3 小时前
在 Elastic 中使用 MCP 自动化用户旅程以进行合成监控
大数据·运维·人工智能·elasticsearch·搜索引擎·自动化·可用性测试