Post JSON data to elastic search using curl command - elasticsearch

I am trying to post Json file to elastic search and facing below errors
curl -XPOST http://localhost/test-index/doc -H "Content-Type: application/json" -d #test.json
{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Rejecting mapping update to [test-index] as the final mapping would have more than 1 type: [_doc, doc]"}],"type":"illegal_argument_exception","reason":"Rejecting mapping update to [test-index] as the final mapping would have more than 1 type: [_doc, doc]"},"status":400}
test.json content
{
"name":"John Smith",
"age":"38"
}
am I missing anything

To post Json data to elasticsearch using curl command, you can try out this command:
curl -XPOST http://localhost:9200/test-index/_doc -H "Content-Type: application/json" -d #test.json
The command with which, you are trying to post Json file, works fine with Postman.

The error returned is
"reason":"Rejecting mapping update to [test-index] as the final
mapping would have more than 1 type: [_doc, doc]"}]
it mean that your index already have a type _doc and you try to create a new type doc. As version ~7.?? of elastic support only one type you can't create your data.
You need to add "_" before doc in your request.
http://localhost/test-index/doc <-- not correct
http://localhost/test-index/_doc <-- correct

Related

How to dynamically recognize points via HTTPS ingestion?

We are sending https requests to ingest data. How can the request be formatted so that Elastic dynamically maps the 'geo_point' field as type:point and not text or number? Thank you!
curl -X POST "https://in-https.URL" -H 'Content-Type: application/json' -d'
{
    "geo_point": -71.34, 41.12
}
'
geo_point dynamic mapping is not supported - https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html.
To index a geo_point field, precede you original request with a request to _mapping to add your geo_point field to the index mapping - https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html

Elasticsearch 7.7 how to read several documents, not fetch all the documents

I am using ElasticSearch 7.7 in CentOS 8 box. I could creat index, type by REST format by command curl. For example, I could use
curl -X PUT "localhost:9200/testindex2"
curl -H "Content-Type: application/json" -XPOST "http://localhost:9200/testindex2/man/1/" -d '{ "name" : "shiny2", "age": 28}'
curl -XGET "localhost:9200/testindex2/man/1/"
curl -XGET "localhost:9200/testindex2/man/_search?pretty"
But if I have inserted many documents, how could I do query by REST command line using command curl to find particular age = 28's documents?
curl -XGET "localhost:9200/testindex2/_search?pretty&q=age:28"
that is the simplest way to query.
more option and documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/7.8/search-search.html
also you can use Match or Term query with JSON body format.
curl -XGET 'localhost:9200/testindex2/_search?pretty' -d '
{
"query": {
"term": {
"age": {
"value": "28"
}
}
}
}'
more documentation:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

Search query to match all and return all data without using curl

I am working with postman and i want to try using getting all data from my index, now when using curl
curl -X GET "localhost:9200/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}
but i want to write a search and return all data, my index name is tourdata and type is tours
127.0.0.1:9200/tourdata/tours/_search
how do i continue
So i found that i need to add size to my query parameter, This worked for me.
127.0.0.1:9200/tourdata/tours/_search?size=7000&pretty=true

I am getting this error in elasticsearch

{
"error":"Content-Type header [application/x-www-form-urlencoded] is not supported",
"status":406
}
Though I used header Content-Type
Here is the the command I used for indexing a customer document for a customer index:
curl -XPUT localhost:9200/customer/doc/1 -H 'Content-Type:application/json' -d '{"name":"abcd"}'

How to update a document using the Elasticsearch Update API?

I have indexed a document in Elasticsearch as follows:
{
_parent: chow-demo
_index: prototype_2013.01.02
_type: chow-clfg
_id: Nx4JcvyxTPujkyy0Jq5BNw
_score: 11.600378
_source: {
chow-clfg: {
#type: chow-clfg
clfg: Cg5iV00z4woYAAAARQ0
#timestamp: 2013-01-02T06:26:00.000Z
count: 1
}
}
}
I tried to update the count field by the following command:
curl -XPOST 'localhost:9200/prototype_2013.01.02/chow-clfg/Nx4JcvyxTPujkyy0Jq5BNw/_update' -d '{"script":"ctx._source.chow-clfg.count+=num","params":{"num":1}}'
However I received the following error instead:
{"error":"RemoteTransportException[[Vesta][inet[/10.15.78.249:9300]][update]]; nested: DocumentMissingException[[prototype_2013.01.02][0] [chow-clfg][Nx4JcvyxTPujkyy0Jq5BNw]: document missing]; ","status":404}
What exactly have I done that is missing? I was following the documents at http://www.elasticsearch.org/guide/reference/api/update.html and yet it doesn't work.
Also, I included the parent field:
curl -XPOST 'localhost:9200/prototype_2013.01.02/chow-clfg/Nx4JcvyxTPujkyy0Jq5BNw/_update' -d '{"parent":"chow-demo","script":"ctx._source.chow-clfg.count+=num","params":{"num":1}}'
And it still didn't work. Anyone can help me with this error?
Basically it was incorrect syntax that caused the problem of not being able to update.
Error:
curl -XPOST 'localhost:9200/prototype_2013.01.02/chow-clfg/Nx4JcvyxTPujkyy0Jq5BNw/_update' \
-d '{"script":"ctx._source.chow-clfg.count+=num","params":{"num":1}}'
Correct syntax:
curl -XPOST 'localhost:9200/prototype_2013.01.02/chow-clfg/Nx4JcvyxTPujkyy0Jq5BNw/_update?parent=chow-demo'
-d '{"script":"ctx._source[\"chow-demo\"].count+=num","params":{"num":1}}'
The parent mapping should be included, together with the type name in its proper syntax:
ctx._source[\"chow-demo\"].count+=num

Resources