Skip to main content

Opensearch Rest API

Shenzhen, China

After setting up my local single node Opensearch "cluster" and create an index mapping and adding my data I now want to interact with it using the terminal.

Using cURL

curl -XPOST "http://localhost:9200/_search" -d'
{
"query": {
"match_all": {}
}
}'

curl: (52) Empty reply from server

curl -XGET "https://localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'

curl: (60) SSL certificate problem: unable to get local issuer certificate

curl -XGET "https://localhost:9200/_search" --insecure -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'

Unauthorized

curl -XGET "https://localhost:9200/_search" --insecure -H 'Content-Type: application/json' -u admin -d'
{
"query": {
"match_all": {}
}
}'

Enter host password for user 'admin': admin => JSON response

Search Queries

Opensearch Rest API

curl -XGET "https://localhost:9200/_search" --insecure -H 'Content-Type: application/json' -u admin -d'
{
"query": {
"query_string": {
"query": "Continuous Integration"
}


}
}'

The JSON response I get for each matching article follows my index mapping:

{
"took": 41,
"timed_out": false,
"_shards": {
"total": 6,
"successful": 6,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 3.0692163,
"hits": [{
"_index": "dev_2022_08_20",
"_id": "docs_ci-cd",
"_score": 3.0692163,
"_source": {
"_index": "dev_2022_08_20",
"_id": "docs_dev-intro",
"_score": 0.7032547,
"_source": {
"title": "Article Title",
"type": "Article",
"description": "Article content",
"link": "article/url",
"chapter": "Introduction",
"tags": ["Tags"],
"imagesquare": "/logo.png",
"short": "Short description",
"abstract": "Abstract"
}
}
}, {
...
}
}]
}
}

Filter the Query Response

curl -XGET "https://localhost:9200/_search?q=Continuous%20Integration&filter_path=took,hits.hits._id,hits.hits._score,hits.hits._source&_source=title&pretty" --insecure -H 'Content-Type: application/json' -u admin
{
"took" : 16,
"hits" : {
"hits" : [
{
"_id" : "id1",
"_score" : 3.0692163,
"_source" : {
"title" : "Article Title 1"
}
},
{
"_id" : "id2",
"_score" : 0.7032547,
"_source" : {
"title" : "Article Title 2"
}
}
]
}
}

Using Python

Python - Working with the Elasticsearch REST API