Mary
Mary

Reputation: 187

Elasticsearch does not work when try to use with python

I am following the tutorial, completely new to any data bases, including Elasticsearch.

My code

client = Elasticsearch()
indexName = "art"
client.indices.create(index=indexName)

shows the following error (according to the book, it shouldn't)

ValueError Traceback (most recent call last) /var/folders/s5/dhdrwvbj5bs4m9nld9bp43400000gn/T/ipykernel_49947/868473428.py in ----> 1 client = Elasticsearch() 2 indexName = "art" 3 client.indices.create(index=indexName)

~/opt/anaconda3/lib/python3.9/site-packages/elasticsearch/_sync/client/init.py in init(self, hosts, cloud_id, api_key, basic_auth, bearer_auth, opaque_id, headers, connections_per_node, http_compress, verify_certs, ca_certs, client_cert, client_key, ssl_assert_hostname, ssl_assert_fingerprint, ssl_version, ssl_context, ssl_show_warn, transport_class, request_timeout, node_class, node_pool_class, randomize_nodes_in_pool, node_selector_class, dead_node_backoff_factor, max_dead_node_backoff, serializer, serializers, default_mimetype, max_retries, retry_on_status, retry_on_timeout, sniff_on_start, sniff_before_requests, sniff_on_node_failure, sniff_timeout, min_delay_between_sniffing, sniffed_node_callback, meta_header, timeout, randomize_hosts, host_info_callback, sniffer_timeout, sniff_on_connection_fail, http_auth, maxsize, _transport) 189 ) -> None: 190 if hosts is None and cloud_id is None and _transport is None: --> 191 raise ValueError("Either 'hosts' or 'cloud_id' must be specified") 192 193 if timeout is not DEFAULT:

ValueError: Either 'hosts' or 'cloud_id' must be specified

When I start Elasticsearch through Terminal, I can open localhost:9200. I also have changed #network.host: 0.0.0.0 in elasticsearch.yml.

Could you please help me? And could you please explain, what exactly this code does?

Thank you!

Upvotes: 0

Views: 1064

Answers (1)

warkolm
warkolm

Reputation: 2064

take a look at the docs, basically you need to use the below if you want to connect to Elasticsearch locally;

client = Elasticsearch("http://localhost:9200")
indexName = "art"
client.indices.create(index=indexName)
  1. client tells where this instance needs to connect to to talk to Elasticsearch
  2. indexName is the name of the index you want to use
  3. the last line is the action, what you want to do, ie create the index you defined

Upvotes: 1

Related Questions