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

相关推荐
云栈开源日记2 小时前
Python 开发技术栈梳理:从数据库、爬虫到 Django 与机器学习
数据库·爬虫·python·学习·机器学习·django
青衫码上行2 小时前
【Java Web学习 | 第15篇】jQuery(万字长文警告)
java·开发语言·前端·学习·jquery
必胜的思想钢印7 小时前
修改主频&睡眠模式&停机模式&待机模式
笔记·stm32·单片机·嵌入式硬件·学习
brave and determined9 小时前
可编程逻辑器件学习(day30):数字电路设计中的流水线技术:原理、实现与优化
学习·fpga开发·verilog·fpga·数字电路·硬件设计·嵌入式设计
Radan小哥10 小时前
Docker学习笔记—day007
笔记·学习·docker
PyAIGCMaster10 小时前
如何编译一个apk,我是新手
深度学习·学习
Mr_sun.11 小时前
Day08——ElasticSearch-基础
大数据·elasticsearch·jenkins
Elastic 中国社区官方博客11 小时前
在 Elasticsearch 中实现带可观测性的 agentic 搜索以自动调优相关性
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
黄黄黄黄黄莹11 小时前
ElasticSearch安装相关插件
elasticsearch
立志成为大牛的小牛11 小时前
数据结构——四十四、平衡二叉树的删除操作(王道408)
数据结构·学习·程序人生·考研·算法