Elasticsearch Suche Installation
Elasticsearch: Volltextsuche
Elasticsearch ist eine verteilte Such- und Analyse-Engine. Sie ermöglicht blitzschnelle Volltextsuche, Logging-Analyse und komplexe Datenabfragen.
Installation
# GPG Key
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg
# Repository
echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list
# Installieren
sudo apt update
sudo apt install elasticsearch -y
# Starten
sudo systemctl start elasticsearch
sudo systemctl enable elasticsearch
Docker-Installation
docker run -d \
--name elasticsearch \
-p 9200:9200 \
-e "discovery.type=single-node" \
-e "xpack.security.enabled=false" \
-v esdata:/usr/share/elasticsearch/data \
elasticsearch:8.11.0
Verbindung testen
curl -X GET "localhost:9200/"
# Cluster-Health
curl -X GET "localhost:9200/_cluster/health?pretty"
Index erstellen
# Index mit Mapping
curl -X PUT "localhost:9200/produkte" -H 'Content-Type: application/json' -d'
{
"mappings": {
"properties": {
"name": { "type": "text" },
"beschreibung": { "type": "text" },
"preis": { "type": "float" },
"kategorie": { "type": "keyword" },
"erstellt": { "type": "date" }
}
}
}'
Dokumente indexieren
# Einzelnes Dokument
curl -X POST "localhost:9200/produkte/_doc/1" -H 'Content-Type: application/json' -d'
{
"name": "Laptop Pro",
"beschreibung": "Leistungsstarker Laptop für Entwickler",
"preis": 1299.99,
"kategorie": "elektronik",
"erstellt": "2026-01-11"
}'
# Bulk-Import
curl -X POST "localhost:9200/produkte/_bulk" -H 'Content-Type: application/json' -d'
{"index":{"_id":"2"}}
{"name":"Smartphone","preis":599.99,"kategorie":"elektronik"}
{"index":{"_id":"3"}}
{"name":"Kopfhörer","preis":149.99,"kategorie":"audio"}
'
Suchen
# Alle Dokumente
curl -X GET "localhost:9200/produkte/_search?pretty"
# Match Query (Volltextsuche)
curl -X GET "localhost:9200/produkte/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match": {
"beschreibung": "Laptop Entwickler"
}
}
}'
# Exakter Filter
curl -X GET "localhost:9200/produkte/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"kategorie": "elektronik"
}
}
}'
# Kombinierte Query
curl -X GET "localhost:9200/produkte/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "name": "Laptop" } }
],
"filter": [
{ "range": { "preis": { "lte": 1500 } } }
]
}
}
}'
PHP-Integration
// composer require elasticsearch/elasticsearch
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()
->setHosts(['localhost:9200'])
->build();
// Indexieren
$client->index([
'index' => 'produkte',
'id' => '4',
'body' => [
'name' => 'Tablet',
'preis' => 449.99
]
]);
// Suchen
$results = $client->search([
'index' => 'produkte',
'body' => [
'query' => [
'match' => ['name' => 'Laptop']
]
]
]);
foreach ($results['hits']['hits'] as $hit) {
echo $hit['_source']['name'];
}
Aggregationen
curl -X GET "localhost:9200/produkte/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"kategorien": {
"terms": { "field": "kategorie" }
},
"durchschnittspreis": {
"avg": { "field": "preis" }
}
}
}'
Nützliche Befehle
# Alle Indizes
curl -X GET "localhost:9200/_cat/indices?v"
# Index löschen
curl -X DELETE "localhost:9200/produkte"
# Dokument löschen
curl -X DELETE "localhost:9200/produkte/_doc/1"
Weitere Hilfe
- 📖 Elasticsearch Dokumentation
- 📧 E-Mail: support@enjyn.de