elk_stack/work_with_elastic.py
2025-09-14 20:02:12 +03:30

53 lines
2.8 KiB
Python

from elasticsearch import Elasticsearch, helpers
client = Elasticsearch(
"https://127.0.0.1:3113",
api_key="cnpEaUxaa0JPYlRRZTVkQmRva2I6VW5wWFpLYjRIMW9KT3FKM25hZW45UQ==",
ca_certs="./ca.crt"
)
index_name = "search-87wk"
mappings = {
"properties": {
"text": {
"type": "text"
}
}
}
mapping_response = client.indices.put_mapping(index=index_name, body=mappings)
print(mapping_response)
index_name = "search-87wk"
docs = [
{
"text": "Yellowstone National Park is one of the largest national parks in the United States. It ranges from the Wyoming to Montana and Idaho, and contains an area of 2,219,791 acress across three different states. Its most famous for hosting the geyser Old Faithful and is centered on the Yellowstone Caldera, the largest super volcano on the American continent. Yellowstone is host to hundreds of species of animal, many of which are endangered or threatened. Most notably, it contains free-ranging herds of bison and elk, alongside bears, cougars and wolves. The national park receives over 4.5 million visitors annually and is a UNESCO World Heritage Site."
},
{
"text": "Yosemite National Park is a United States National Park, covering over 750,000 acres of land in California. A UNESCO World Heritage Site, the park is best known for its granite cliffs, waterfalls and giant sequoia trees. Yosemite hosts over four million visitors in most years, with a peak of five million visitors in 2016. The park is home to a diverse range of wildlife, including mule deer, black bears, and the endangered Sierra Nevada bighorn sheep. The park has 1,200 square miles of wilderness, and is a popular destination for rock climbers, with over 3,000 feet of vertical granite to climb. Its most famous and cliff is the El Capitan, a 3,000 feet monolith along its tallest face."
},
{
"text": "Rocky Mountain National Park is one of the most popular national parks in the United States. It receives over 4.5 million visitors annually, and is known for its mountainous terrain, including Longs Peak, which is the highest peak in the park. The park is home to a variety of wildlife, including elk, mule deer, moose, and bighorn sheep. The park is also home to a variety of ecosystems, including montane, subalpine, and alpine tundra. The park is a popular destination for hiking, camping, and wildlife viewing, and is a UNESCO World Heritage Site."
}
]
# Timeout to allow machine learning model loading and semantic ingestion to complete
ingestion_timeout=300
from elasticsearch.helpers import bulk, BulkIndexError
try:
bulk_response = helpers.bulk(
client.options(request_timeout=ingestion_timeout),
docs,
index=index_name
)
except BulkIndexError as e:
print("Documents failed to index:")
for error in e.errors:
print(error)
print(bulk_response)