Untitled
unknown
plain_text
a year ago
2.2 kB
8
Indexable
#!/bin/bash
# Prompt for user inputs
read -p "Enter Elasticsearch URL: " es_url
read -p "Enter Index Name: " index_name
read -p "Enter Field Name: " field_name
read -p "Enter CSV Filename: " csv_filename
# Extract cookies from browser (example format)
# Add other cookies if necessary
cookies="cookie_name1=cookie_value1; cookie_name2=cookie_value2"
# Function to fetch all unique paths
fetch_all_unique_paths() {
local after_key=""
local all_buckets=()
local batch_size=1000
while true; do
response=$(curl -s -X POST "$es_url/$index_name/_search" \
-H "Content-Type: application/json" \
-H "Cookie: $cookies" \
-d '{
"size": 0,
"aggs": {
"all_paths": {
"composite": {
"size": '$batch_size',
"sources": [
{
"path": {
"terms": {
"field": "'$field_name'"
}
}
}
]
}
}
}'${after_key:+, '"after":' $after_key}'}')
buckets=$(echo "$response" | jq -c '.aggregations.all_paths.buckets[]')
for bucket in $buckets; do
all_buckets+=("$bucket")
done
after_key=$(echo "$response" | jq -c '.aggregations.all_paths.after_key // empty')
if [[ -z "$after_key" ]]; then
break
fi
done
echo "${all_buckets[@]}"
}
# Write to CSV
write_to_csv() {
local buckets=("$@")
echo "Path,Count" > "$csv_filename"
for bucket in "${buckets[@]}"; do
path=$(echo "$bucket" | jq -r '.key.path')
count=$(echo "$bucket" | jq -r '.doc_count')
echo "$path,$count" >> "$csv_filename"
done
}
# Fetch all unique paths and write to CSV
all_buckets=$(fetch_all_unique_paths)
write_to_csv "${all_buckets[@]}"
echo "Results written to $csv_filename"
Editor is loading...
Leave a Comment