Elasticsearch Tools
Essential Elasticsearch commands, queries, and code examples for Python and Laravel developers.
🐍Python Elasticsearch
Installation & Setup
Install Elasticsearch Python Client
# Install Elasticsearch client
pip install elasticsearch
# For async support
pip install elasticsearch[async]
# For development with additional features
pip install elasticsearch[dev]
Connection Setup
Basic Connection
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
# Basic connection
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
# With authentication
es = Elasticsearch(
['localhost:9200'],
http_auth=('username', 'password'),
verify_certs=True,
ca_certs='/path/to/ca_certs.pem'
)
# Cloud connection
es = Elasticsearch(
cloud_id="your_cloud_id",
http_auth=('elastic', 'password')
)
# Check connection
if es.ping():
print("Connected to Elasticsearch!")
else:
print("Could not connect to Elasticsearch")
CRUD Operations
Create Document
# Create a single document
doc = {
'title': 'Elasticsearch Guide',
'content': 'This is a comprehensive guide to Elasticsearch',
'tags': ['elasticsearch', 'search', 'database'],
'created_at': '2024-01-15T10:30:00Z'
}
# Index document with auto-generated ID
result = es.index(index='articles', body=doc)
print(f"Document created with ID: {result['_id']}")
# Index document with specific ID
result = es.index(index='articles', id=1, body=doc)
# Bulk indexing
documents = [
{'_index': 'articles', '_source': {'title': 'Doc 1', 'content': 'Content 1'}},
{'_index': 'articles', '_source': {'title': 'Doc 2', 'content': 'Content 2'}},
{'_index': 'articles', '_source': {'title': 'Doc 3', 'content': 'Content 3'}}
]
bulk(es, documents)
Read Document
# Get document by ID
doc = es.get(index='articles', id=1)
print(doc['_source'])
# Search documents
search_body = {
'query': {
'match': {
'title': 'Elasticsearch'
}
}
}
result = es.search(index='articles', body=search_body)
for hit in result['hits']['hits']:
print(f"Score: {hit['_score']}, Source: {hit['_source']}")
# Search with filters
search_body = {
'query': {
'bool': {
'must': [
{'match': {'title': 'guide'}},
{'term': {'tags': 'elasticsearch'}}
],
'filter': [
{'range': {'created_at': {'gte': '2024-01-01'}}}
]
}
}
}
result = es.search(index='articles', body=search_body)
Update Document
# Update entire document
updated_doc = {
'title': 'Updated Elasticsearch Guide',
'content': 'This is an updated comprehensive guide to Elasticsearch',
'tags': ['elasticsearch', 'search', 'database', 'updated'],
'updated_at': '2024-01-16T10:30:00Z'
}
result = es.index(index='articles', id=1, body=updated_doc)
# Partial update using script
update_body = {
'script': {
'source': 'ctx._source.views += params.views',
'params': {'views': 1}
}
}
result = es.update(index='articles', id=1, body=update_body)
# Partial update with doc
update_body = {
'doc': {
'title': 'New Title',
'updated_at': '2024-01-16T10:30:00Z'
}
}
result = es.update(index='articles', id=1, body=update_body)
Delete Document
# Delete document by ID
result = es.delete(index='articles', id=1)
print(f"Document deleted: {result['result']}")
# Delete by query
delete_query = {
'query': {
'term': {'status': 'inactive'}
}
}
result = es.delete_by_query(index='articles', body=delete_query)
print(f"Deleted {result['deleted']} documents")
# Delete entire index
result = es.indices.delete(index='articles')
Advanced Queries
Aggregations
# Terms aggregation
search_body = {
'size': 0,
'aggs': {
'popular_tags': {
'terms': {
'field': 'tags.keyword',
'size': 10
}
}
}
}
result = es.search(index='articles', body=search_body)
for bucket in result['aggregations']['popular_tags']['buckets']:
print(f"Tag: {bucket['key']}, Count: {bucket['doc_count']}")
# Date histogram aggregation
search_body = {
'size': 0,
'aggs': {
'articles_over_time': {
'date_histogram': {
'field': 'created_at',
'calendar_interval': 'month'
}
}
}
}
result = es.search(index='articles', body=search_body)
# Range aggregation
search_body = {
'size': 0,
'aggs': {
'view_ranges': {
'range': {
'field': 'views',
'ranges': [
{'to': 100},
{'from': 100, 'to': 1000},
{'from': 1000}
]
}
}
}
}
result = es.search(index='articles', body=search_body)
Full Text Search
# Multi-match query
search_body = {
'query': {
'multi_match': {
'query': 'elasticsearch guide',
'fields': ['title^2', 'content'],
'type': 'best_fields',
'fuzziness': 'AUTO'
}
}
}
result = es.search(index='articles', body=search_body)
# Bool query with should
search_body = {
'query': {
'bool': {
'should': [
{'match': {'title': 'elasticsearch'}},
{'match': {'content': 'elasticsearch'}},
{'match': {'tags': 'elasticsearch'}}
],
'minimum_should_match': 1
}
}
}
result = es.search(index='articles', body=search_body)
# Wildcard and regex queries
search_body = {
'query': {
'bool': {
'should': [
{'wildcard': {'title': '*guide*'}},
{'regexp': {'content': 'elast.*'}}
]
}
}
}
result = es.search(index='articles', body=search_body)
🌶️Laravel Elasticsearch
Installation & Setup
Install Laravel Scout & Elasticsearch
# Install Laravel Scout
composer require laravel/scout
# Install Elasticsearch driver
composer require tamayo/laravel-scout-elastic
# Publish Scout configuration
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
# Publish Elasticsearch configuration
php artisan vendor:publish --provider="Tamayo\LaravelScoutElastic\ScoutElasticServiceProvider"
Configuration
Environment Configuration
# .env configuration
SCOUT_DRIVER=elastic
ELASTICSEARCH_HOST=localhost:9200
ELASTICSEARCH_INDEX=laravel
# config/scout.php
'elastic' => [
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
'hosts' => [
env('ELASTICSEARCH_HOST', 'localhost:9200'),
],
],
# config/database.php (add to connections)
'elasticsearch' => [
'driver' => 'elasticsearch',
'host' => env('ELASTICSEARCH_HOST', 'localhost:9200'),
'index' => env('ELASTICSEARCH_INDEX', 'laravel'),
],
Model Setup
Article Model with Scout
'array',
'published_at' => 'datetime',
];
/**
* Get the indexable data array for the model.
*/
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
'tags' => $this->tags,
'published_at' => $this->published_at?->toISOString(),
'views' => $this->views,
'created_at' => $this->created_at->toISOString(),
];
}
/**
* Get the index name for the model.
*/
public function searchableAs()
{
return 'articles_index';
}
/**
* Determine if the model should be searchable.
*/
public function shouldBeSearchable()
{
return $this->published_at !== null;
}
}
CRUD Operations
Create & Index Documents
'Laravel Elasticsearch Guide',
'content' => 'This is a comprehensive guide to using Elasticsearch with Laravel',
'tags' => ['laravel', 'elasticsearch', 'search'],
'published_at' => now(),
'views' => 0
]);
// Manually index existing model
$article->searchable();
// Bulk index all models
Article::makeAllSearchable();
// Index specific models
Article::where('published_at', '!=', null)->searchable();
// Remove from search index
$article->unsearchable();
Search Operations
get();
// Search with pagination
$articles = Article::search('laravel guide')
->paginate(15);
// Advanced search with filters
$articles = Article::search('elasticsearch')
->where('tags', 'laravel')
->where('views', '>', 100)
->orderBy('published_at', 'desc')
->get();
// Search with custom query
$articles = Article::search('', function ($elasticsearch, $query) {
return $elasticsearch->search($query, [
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['title' => 'elasticsearch']],
['match' => ['content' => 'guide']]
],
'filter' => [
['term' => ['tags' => 'laravel']]
]
]
]
]
]);
})->get();
// Get search results with highlights
$results = Article::search('elasticsearch')
->with([
'body' => [
'highlight' => [
'fields' => [
'title' => new \stdClass(),
'content' => new \stdClass()
]
]
]
])
->get();
Aggregations
with([
'body' => [
'size' => 0,
'aggs' => [
'popular_tags' => [
'terms' => [
'field' => 'tags.keyword',
'size' => 10
]
]
]
]
])
->get();
$aggregations = $results->getAggregations();
$popularTags = $aggregations['popular_tags']['buckets'];
// Date histogram aggregation
$results = Article::search('')
->with([
'body' => [
'size' => 0,
'aggs' => [
'articles_over_time' => [
'date_histogram' => [
'field' => 'published_at',
'calendar_interval' => 'month'
]
]
]
]
])
->get();
// Range aggregation
$results = Article::search('')
->with([
'body' => [
'size' => 0,
'aggs' => [
'view_ranges' => [
'range' => [
'field' => 'views',
'ranges' => [
['to' => 100],
['from' => 100, 'to' => 1000],
['from' => 1000]
]
]
]
]
]
])
->get();
Artisan Commands
Useful Commands
# Import all models to search index
php artisan scout:import "App\Models\Article"
# Flush all models from search index
php artisan scout:flush "App\Models\Article"
# Create custom search command
php artisan make:command SearchArticles
# Example custom command
argument('query');
$articles = Article::search($query)->get();
$this->info("Found {$articles->count()} articles:");
foreach ($articles as $article) {
$this->line("- {$article->title}");
}
}
}
🔍Common Elasticsearch Queries
Query Examples
Match Query
{
"query": {
"match": {
"title": "elasticsearch guide"
}
}
}
Bool Query
{
"query": {
"bool": {
"must": [
{ "match": { "title": "elasticsearch" } }
],
"should": [
{ "match": { "content": "guide" } },
{ "match": { "tags": "tutorial" } }
],
"filter": [
{ "term": { "status": "published" } },
{ "range": { "created_at": { "gte": "2024-01-01" } } }
],
"must_not": [
{ "term": { "category": "draft" } }
]
}
}
}
Aggregation Query
{
"size": 0,
"aggs": {
"popular_tags": {
"terms": {
"field": "tags.keyword",
"size": 10
}
},
"avg_views": {
"avg": {
"field": "views"
}
},
"articles_by_month": {
"date_histogram": {
"field": "created_at",
"calendar_interval": "month"
}
}
}
}
⚡Performance Tips
Python Performance
- • Use connection pooling
- • Implement bulk operations
- • Use async client for I/O operations
- • Cache frequently used queries
- • Use scroll API for large datasets
- • Optimize mapping and analyzers
Laravel Performance
- • Use queue jobs for indexing
- • Implement search result caching
- • Use eager loading with search
- • Optimize toSearchableArray()
- • Use partial updates when possible
- • Monitor search performance