دليل Elasticsearch: الأوامر الأساسية والنصائح
أوامر Elasticsearch للبحث، والفرز، والتحليلات
Elasticsearch هو محرك بحث وتحليل قوي تم بناؤه على Apache Lucene. تحتوي هذه المجموعة الشاملة من الأوامر على الأوامر الأساسية، وال أفضل الممارسات، والمرجع السريع لاستخدام مجموعات Elasticsearch.

ملاحظة: تستخدم معظم الأمثلة في هذا الدليل cURL لطلبات HTTP. إذا كنت جديدًا في cURL أو تحتاج إلى مرجع سريع للخيارات المتقدمة، فراجع قائمة الأوامر المفيدة لـ cURL لمعرفة تقنيات طلب HTTP من سطر الأوامر.
إدارة المجموعات
فحص صحة المجموعة
تستخدم جميع الأوامر في هذا القسم cURL للتفاعل مع واجهة برمجة التطبيقات (API) REST لـ Elasticsearch. يمكنك تخصيص هذه الطلبات بإضافات إضافية مثل الرؤوس، والاعتماد، وغيرها حسب الحاجة.
# فحص صحة أساسية
curl -X GET "localhost:9200/_cluster/health?pretty"
# فحص صحة المجموعة مع معلومات الشارد
curl -X GET "localhost:9200/_cluster/health?level=shards&pretty"
# فحص معلومات العقدة
curl -X GET "localhost:9200/_cat/nodes?v"
# فحص إعدادات المجموعة
curl -X GET "localhost:9200/_cluster/settings?pretty"
عمليات العقدة
# قائمة بجميع العقد
curl -X GET "localhost:9200/_cat/nodes?v&h=name,node.role,heap.percent,ram.percent,cpu,load_1m"
# إحصائيات العقدة
curl -X GET "localhost:9200/_nodes/stats?pretty"
# خيوط ساخنة (التحقق من الأعطال)
curl -X GET "localhost:9200/_nodes/hot_threads"
إدارة الفهارس
إنشاء وحذف الفهارس
# إنشاء فهرس
curl -X PUT "localhost:9200/my_index?pretty"
# إنشاء فهرس مع إعدادات
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
'
# حذف الفهرس
curl -X DELETE "localhost:9200/my_index?pretty"
# قائمة بجميع الفهارس
curl -X GET "localhost:9200/_cat/indices?v"
# إحصائيات الفهرس
curl -X GET "localhost:9200/my_index/_stats?pretty"
خرائط الفهرس
# تعريف الخريطة
curl -X PUT "localhost:9200/products" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"name": { "type": "text" },
"price": { "type": "float" },
"created_at": { "type": "date" },
"tags": { "type": "keyword" },
"description": {
"type": "text",
"analyzer": "english"
}
}
}
}
'
# الحصول على الخريطة
curl -X GET "localhost:9200/products/_mapping?pretty"
# تحديث الخريطة (إضافة حقل)
curl -X PUT "localhost:9200/products/_mapping" -H 'Content-Type: application/json' -d'
{
"properties": {
"category": { "type": "keyword" }
}
}
'
قوالب الفهارس
# إنشاء قوالب الفهارس
curl -X PUT "localhost:9200/_index_template/logs_template" -H 'Content-Type: application/json' -d'
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"message": { "type": "text" },
"level": { "type": "keyword" }
}
}
}
}
'
# قائمة بالقوالب
curl -X GET "localhost:9200/_index_template?pretty"
عمليات المستندات (CRUD)
إنشاء المستندات
# إدراج مستند مع توليد معرف تلقائي
curl -X POST "localhost:9200/products/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Laptop",
"price": 999.99,
"tags": ["electronics", "computers"]
}
'
# إدراج مستند مع معرف محدد
curl -X PUT "localhost:9200/products/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Laptop",
"price": 999.99
}
'
# إدراج متعدد
curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index": { "_index": "products", "_id": "1" }}
{ "name": "Laptop", "price": 999.99 }
{ "index": { "_index": "products", "_id": "2" }}
{ "name": "Mouse", "price": 29.99 }
'
قراءة المستندات
# الحصول على مستند حسب المعرف
curl -X GET "localhost:9200/products/_doc/1?pretty"
# الحصول على مستندات متعددة
curl -X GET "localhost:9200/_mget?pretty" -H 'Content-Type: application/json' -d'
{
"docs": [
{ "_index": "products", "_id": "1" },
{ "_index": "products", "_id": "2" }
]
}
'
# التحقق من وجود مستند
curl -I "localhost:9200/products/_doc/1"
تحديث المستندات
# تحديث مستند
curl -X POST "localhost:9200/products/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"doc": {
"price": 899.99
}
}
'
# تحديث باستخدام نص
curl -X POST "localhost:9200/products/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"script": {
"source": "ctx._source.price *= params.discount",
"params": {
"discount": 0.9
}
}
}
'
# تحديث أو إدراج
curl -X POST "localhost:9200/products/_update/1?pretty" -H 'Content-Type: application/json' -d'
{
"doc": {
"price": 899.99
},
"doc_as_upsert": true
}
'
حذف المستندات
# حذف حسب المعرف
curl -X DELETE "localhost:9200/products/_doc/1?pretty"
# حذف حسب الاستعلام
curl -X POST "localhost:9200/products/_delete_by_query?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"name": "old"
}
}
}
'
استعلامات البحث
البحث الأساسي
# تطابق جميع
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
'
# استعلام التطابق
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"name": "laptop"
}
}
}
'
# استعلام التطابق المتعدد
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"multi_match": {
"query": "laptop gaming",
"fields": ["name", "description"]
}
}
}
'
الاستعلامات على مستوى المصطلحات
# استعلام المصطلح (تطابق دقيق)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"tags": "electronics"
}
}
}
'
# استعلام المصطلحات (قيم متعددة)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"terms": {
"tags": ["electronics", "computers"]
}
}
}
'
# استعلام النطاق
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"range": {
"price": {
"gte": 100,
"lte": 1000
}
}
}
}
'
# استعلام الوجود
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"exists": {
"field": "description"
}
}
}
'
الاستعلامات المنطقية
# استعلام المنطق (must, should, must_not, filter)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "name": "laptop" }}
],
"filter": [
{ "range": { "price": { "gte": 500 }}}
],
"should": [
{ "term": { "tags": "gaming" }}
],
"must_not": [
{ "term": { "tags": "refurbished" }}
]
}
}
}
'
البحث المتقدم
# استعلام النمط البريدي
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"wildcard": {
"name": "lap*"
}
}
}
'
# استعلام الضبابية (تحمل الأخطاء)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"fuzzy": {
"name": {
"value": "laptpo",
"fuzziness": "AUTO"
}
}
}
}
'
# استعلام المقدمة
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"prefix": {
"name": "lap"
}
}
}
'
التجميعات
التجميعات المترابطة
# المتوسط، المجموع، الحد الأدنى، الحد الأقصى
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"avg_price": { "avg": { "field": "price" }},
"max_price": { "max": { "field": "price" }},
"min_price": { "min": { "field": "price" }},
"total_sales": { "sum": { "field": "price" }}
}
}
'
# تجميع الإحصائيات
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"price_stats": {
"stats": { "field": "price" }
}
}
}
'
التجميعات في المجموعات
# تجميع المصطلحات (المجموعة حسب)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"popular_tags": {
"terms": {
"field": "tags",
"size": 10
}
}
}
}
'
# تجميع النطاق
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"price_ranges": {
"range": {
"field": "price",
"ranges": [
{ "to": 50 },
{ "from": 50, "to": 100 },
{ "from": 100 }
]
}
}
}
}
'
# تجميع مخطط التاريخ
curl -X GET "localhost:9200/logs/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"logs_over_time": {
"date_histogram": {
"field": "timestamp",
"calendar_interval": "day"
}
}
}
}
'
التجميعات المضمنة
# تجميعات مضمنة
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"categories": {
"terms": { "field": "category" },
"aggs": {
"avg_price": {
"avg": { "field": "price" }
}
}
}
}
}
'
الفرز والتصفح
# الفرز حسب الحقل
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"sort": [
{ "price": { "order": "desc" }},
{ "_score": { "order": "desc" }}
]
}
'
# التصفح باستخدام from/size
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"from": 0,
"size": 10,
"query": { "match_all": {} }
}
'
# البحث بعد (للتصفح العميق)
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 10,
"query": { "match_all": {} },
"sort": [{ "price": "asc" }, { "_id": "asc" }],
"search_after": [100, "product_123"]
}
'
اختيار الحقول والتوسيع
# اختيار حقول محددة
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"_source": ["name", "price"]
}
'
# التوسيع
curl -X GET "localhost:9200/products/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": { "description": "gaming laptop" }
},
"highlight": {
"fields": {
"description": {}
}
}
}
'
التسميات البديلة
# إنشاء تسمية بديلة
curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "add": { "index": "products_v1", "alias": "products" }}
]
}
'
# تبديل التسمية البديلة إلى فهرس جديد (بدون وقت توقف)
curl -X POST "localhost:9200/_aliases?pretty" -H 'Content-Type: application/json' -d'
{
"actions": [
{ "remove": { "index": "products_v1", "alias": "products" }},
{ "add": { "index": "products_v2", "alias": "products" }}
]
}
'
# قائمة التسميات البديلة
curl -X GET "localhost:9200/_cat/aliases?v"
إعادة الفهرس
# إعادة الفهرس من فهرس إلى آخر
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "old_products"
},
"dest": {
"index": "new_products"
}
}
'
# إعادة الفهرس مع استعلام
curl -X POST "localhost:9200/_reindex?pretty" -H 'Content-Type: application/json' -d'
{
"source": {
"index": "products",
"query": {
"range": {
"price": { "gte": 100 }
}
}
},
"dest": {
"index": "expensive_products"
}
}
'
النسخ الاحتياطي وال快照
# تسجيل مستودع快照
curl -X PUT "localhost:9200/_snapshot/my_backup?pretty" -H 'Content-Type: application/json' -d'
{
"type": "fs",
"settings": {
"location": "/mount/backups/my_backup"
}
}
'
# إنشاء快照
curl -X PUT "localhost:9200/_snapshot/my_backup/snapshot_1?wait_for_completion=true&pretty"
# استعادة快照
curl -X POST "localhost:9200/_snapshot/my_backup/snapshot_1/_restore?pretty"
# قائمة快shots
curl -X GET "localhost:9200/_snapshot/my_backup/_all?pretty"
# حذف快照
curl -X DELETE "localhost:9200/_snapshot/my_backup/snapshot_1?pretty"
تحسين الأداء
إعدادات الفهرس
# تعطيل التحديث أثناء إعادة الفهرس
curl -X PUT "localhost:9200/products/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"index": {
"refresh_interval": "-1"
}
}
'
# إعادة تفعيله بعد إعادة الفهرس
curl -X PUT "localhost:9200/products/_settings?pretty" -H 'Content-Type: application/json' -d'
{
"index": {
"refresh_interval": "1s"
}
}
'
# دمج قسري (تحسين الأداء)
curl -X POST "localhost:9200/products/_forcemerge?max_num_segments=1&pretty"
مسح التخزين المؤقت
# مسح جميع التخزين المؤقت
curl -X POST "localhost:9200/_cache/clear?pretty"
# مسح تخزين مؤقت محدد
curl -X POST "localhost:9200/products/_cache/clear?query=true&pretty"
المراقبة والتحقق من الأعطال
# المهام المعلقة
curl -X GET "localhost:9200/_cat/pending_tasks?v"
# إحصائيات حوض الخيوط
curl -X GET "localhost:9200/_cat/thread_pool?v"
# معلومات الشريحة
curl -X GET "localhost:9200/_cat/segments?v"
# معلومات الاستعادة
curl -X GET "localhost:9200/_cat/recovery?v&h=index,stage,time"
# واجهة برمجة التطبيقات للمهام
curl -X GET "localhost:9200/_tasks?detailed=true&pretty"
أمثلة على العميل في Python
from elasticsearch import Elasticsearch
# الاتصال بـ Elasticsearch
es = Elasticsearch(['http://localhost:9200'])
# إدراج مستند
doc = {
'name': 'Laptop',
'price': 999.99,
'tags': ['electronics']
}
es.index(index='products', id=1, document=doc)
# البحث
resp = es.search(index='products', query={'match': {'name': 'laptop'}})
for hit in resp['hits']['hits']:
print(hit['_source'])
# إدراج متعدد
from elasticsearch.helpers import bulk
actions = [
{
'_index': 'products',
'_id': i,
'_source': {'name': f'Product {i}', 'price': i * 10}
}
for i in range(1000)
]
bulk(es, actions)
أمثلة على العميل في JavaScript/Node.js
يوفر عميل JavaScript لـ Elasticsearch طريقة آمنة من حيث النوع للتفاعل مع مجموعتك. لتطبيقات الإنتاج، يُنصح باستخدام TypeScript للحصول على أمان أفضل من حيث النوع والاكتمال التلقائي. راجع قائمة الأوامر المفيدة لـ TypeScript لمعرفة أفضل الممارسات حول تعريف الأنواع وواجهات TypeScript.
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
// إدراج مستند
async function indexDoc() {
await client.index({
index: 'products',
id: 1,
document: {
name: 'Laptop',
price: 999.99
}
});
}
// البحث
async function search() {
const result = await client.search({
index: 'products',
query: {
match: { name: 'laptop' }
}
});
console.log(result.hits.hits);
}
// إدراج متعدد
async function bulkIndex() {
const operations = [];
for (let i = 0; i < 1000; i++) {
operations.push({ index: { _index: 'products', _id: i } });
operations.push({ name: `Product ${i}`, price: i * 10 });
}
await client.bulk({ operations });
}
مثال TypeScript مع توصيفات قوية
import { Client } from '@elastic/elasticsearch';
interface Product {
name: string;
price: number;
tags?: string[];
created_at?: Date;
}
const client = new Client({ node: 'http://localhost:9200' });
async function indexProduct(product: Product, id: number): Promise<void> {
await client.index<Product>({
index: 'products',
id: id.toString(),
document: product
});
}
async function searchProducts(query: string): Promise<Product[]> {
const result = await client.search<Product>({
index: 'products',
query: {
match: { name: query }
}
});
return result.hits.hits.map(hit => hit._source as Product);
}
أفضل الممارسات
تصميم الفهارس
- حافظ على حجم الشارد بين 20-50 جيجابايت للحصول على أداء مثالي
- استخدم إدارة دورة حياة الفهرس (ILM) للبيانات الزمنية
- اصمم الخرائط بعناية قبل إدراج البيانات
- استخدم أنواع الحقول المناسبة (الكلمة المفتاحية مقابل النص، تنسيقات التاريخ)
- تعطيل
_sourceللوثائق الكبيرة إذا لم يكن مطلوبًا
تحسين الاستعلامات
- استخدم الترشيحات بدلًا من الاستعلامات عندما لا تحتاج إلى التقييم
- تفضيل الاستعلامات على مستوى المصطلحات للبيانات المهيكلة
- استخدم استعلام
boolلدمج عدة شروط بكفاءة - تنفيذ التصفح باستخدام
search_afterللتصفح العميق - تخزين الترشيحات المتكررة
أداء الإدراج
- استخدم واجهة برمجة التطبيقات (API) للإدراج بالجملة (1000-5000 وثيقة لكل طلب)
- تعطيل التحديث أثناء عمليات الإدراج بالجملة
- زيادة
index.refresh_intervalأثناء الإدراج المكثف - استخدام عدة خيوط/عمليات للإدراج المتوازي
- تفكير في استخدام التوجيه لتحسين توزيع الشارد
إدارة المجموعة
- مراقبة صحة المجموعة بانتظام
- إعداد تكوين المكرر المناسب
- استخدام عقد مخصصة للإدارة في المجموعات الكبيرة
- تنفيذ استراتيجية نسخ احتياطي مناسبة مع快shots
- مراقبة استخدام الذاكرة (الكمبيوتر) (احتفظ بـ أقل من 75%)
الأمان
- تفعيل المصادقة والتحكم في الوصول (X-Pack Security)
- استخدام HTTPS في الإعدادات الإنتاجية (configure cURL مع خيارات
--cacert,--cert, و--keyلـ SSL/TLS) - تنفيذ سياسة التحكم في الوصول بناءً على الأدوار
- تحديثات أمنية دورية وتحديثات
- تشفير البيانات أثناء الراحة والنقل
الاستخدامات الشائعة
البحث النصي الكامل
يتميز Elasticsearch بالبحث النصي الكامل مع ميزات مثل:
- تقييم الأهمية
- التطابق الضبابي
- التطابق بالجملة
- التعامل مع المترادفات
- الدعم متعدد اللغات
تحليل السجلات (مجمع ELK)
- جمع السجلات باستخدام Logstash/Filebeat
- فهرسة وبحث السجلات في Elasticsearch
- تصور البيانات باستخدام Kibana
- إعداد تنبيهات للanomalies
البحث في التجارة الإلكترونية
- بحث في فهرس المنتجات
- التنقل المبني على الفئات مع التجميعات
- اقتراحات وتنبؤات تلقائية
- نتائج البحث المخصصة
مراقبة أداء التطبيق
- فهرسة مؤشرات أداء التطبيق
- لوحات تحكم مراقبة الوقت الفعلي
- اكتشاف الأعطال
- تحليل الاتجاهات في الأداء
روابط مفيدة
موارد Elasticsearch الرسمية
- التوثيق الرسمي لـ Elasticsearch
- مرجع واجهة برمجة التطبيقات لـ Elasticsearch
- دليل الاستعلامات DSL
- عميل Elasticsearch لـ Python
- عميل Elasticsearch لـ JavaScript
- نظرة عامة على مجمع Elastic
- تحسين أداء Elasticsearch
- إدارة دورة حياة الفهرس
كشوفات ودليل مفيد
- قائمة الأوامر المفيدة لـ cURL - ضروري لاستيعاب طلبات HTTP إلى واجهة برمجة التطبيقات (API) REST لـ Elasticsearch
- كشوفات TypeScript: المفاهيم الأساسية وال أفضل الممارسات - تطوير تطبيقات عميل Elasticsearch آمنة من حيث النوع