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

相关推荐
ADHD多动联盟1 分钟前
多动症孩子的运动干预是什么?主要有怎样的方法?
学习·学习方法·玩游戏
炽烈小老头13 分钟前
【每天学习一点算法 2026/03/20】单词搜索
学习·算法
xiaoxiaoxiaolll13 分钟前
最新《Nature Communications》:多元素共生策略为金属材料穿上“抗疲劳铠甲”
学习
weixin_4588726113 分钟前
东华复试OJ二刷复盘14
学习
元契17 分钟前
英语基础语法学习0
学习
arvin_xiaoting25 分钟前
OpenClaw学习总结_I_核心架构系列_Gateway架构详解
学习·架构·llm·gateway·ai-agent·飞书机器人·openclaw
421!29 分钟前
ESP32学习笔记之UART
笔记·学习·嵌入式·esp32·通信
蓝桉~MLGT30 分钟前
Ai-Agent学习历程(插播内容)—— 基于现在最新的Skills、MCP、Rules等进行详细拆解,并列举出使用场景
人工智能·学习
知识分享小能手31 分钟前
Redis入门学习教程,从入门到精通,Redis进阶编程知识点详解(5)
数据库·redis·学习
夏日听雨眠44 分钟前
Linux学习1
linux·服务器·学习