Skip to content

Opensearch bulk API error: ConnectionError- failed to parse- label empty or too long (Opensearch domain endpoint URL)

0

I believe this error is related to the opensearch domain endpoint URL being too long. Here is the full error including the domain endpoint.

"errorMessage": "ConnectionError(Failed to parse: 'https://search-osdomain275q2h7-1jskmc5lls93-5kdjqpc8b1v2ybmev2hcbe3xiw.us-east-1.es.amazonaws.com', label empty or too long) caused by: LocationParseError(Failed to parse: 'https://search-osdomain275q2h7-1jskmc5lls93-5kdjqpc8b1v2ybmev2hcbe3xiw.us-east-1.es.amazonaws.com', label empty or too long)",

Please advise on how to get around this error when calling the bulk API to update records in Opensearch domain. Here is my current code:


# OpenSearch
osHost = "https://search-osdomain275q2h7-1jskmc5lls93-5kdjqpc8b1v2ybmev2hcbe3xiw.us-east-1.es.amazonaws.com"
osPort = 9200


    client = OpenSearch(
        hosts=[{"host": osHost, "port": osPort}],
        http_auth=("admin", "admin"),
        use_ssl=True,
        verify_certs=False,
        ssl_assert_hostname=False,
        ssl_show_warn=False
    )

    bulk_data = [
        {
            "_index": "index_001",
            "_id": 1,
            "_source": {
                "personName": "evan", # some dummy data
                "personCity": "san francisco"
            }
        }
    ]

    bulk(client, bulk_data)
1 Answer
0

Hello,

I recommend double-checking the hard-coded OpenSearch endpoint. After reviewing the internal documentation, you can refer to the following document when using the Bulk API:

[+] Low-level Python client - Performing bulk operations

https://opensearch.org/docs/latest/clients/python-low-level/

Additionally, here is a sample test code I created using the above document. It worked correctly in my environment, so please refer to it.

import boto3
from requests_aws4auth import AWS4Auth
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth

host = '<domain_endpoint>/_bulk'  
region = 'ap-northeast-2'  
service = 'es
credentials = boto3.Session().get_credentials()
awsauth = AWSV4SignerAuth(credentials, region, service)

client = OpenSearch(
    # hosts=[{"host": host, "port": 9200}],
    hosts=[host+"9200"],
    http_auth=awsauth,
    use_ssl=True,
    verify_certs=True,
    connection_class = RequestsHttpConnection,
    pool_maxsize = 20
)

movies = '{ "index" : { "_index" : "<index>", "_id" : "2" } } \n { "title" : "Interstellar", "director" : "Christopher Nolan", "year" : "2014"} \n { "create" : { "_index" : "<index>", "_id" : "3" } } \n { "title" : "Star Trek Beyond", "director" : "Justin Lin", "year" : "2015"} \n { "update" : {"_id" : "3", "_index" : "<index>" } } \n { "doc" : {"year" : "2016"} }'

client.bulk(movies)
AWS
SUPPORT ENGINEER
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.