How to ignore 404 errors when removing an ElasticSearch alias? - elasticsearch

I'm trying to remove the alias my_alias on ElasticSearch 2.3 as:
curl -XPOST 'http://localhost:9200/_aliases' -d '
{
"actions" : [
{ "remove" : { "index" : "*", "alias" : "my_alias" } }
]
}'
However, this alias can not exist. In such case, I get the error:
{
"error": {
"reason": "aliases [my_alias] missing",
"resource.id": "my_alias",
"resource.type": "aliases",
"root_cause": [
{
"reason": "aliases [my_alias] missing",
"resource.id": "my_alias",
"resource.type": "aliases",
"type": "aliases_not_found_exception"
}
],
"type": "aliases_not_found_exception"
},
"status": 404
}
I tried adding ignore_unavailable to the action as { "remove" : { "index" : "*", "alias" : "my_alias", "ignore_unavailable": true } }, or simply ignore: 404, but to no avail. I looked at ElasticSearch's test suite for update_aliases in https://github.com/elastic/elasticsearch/tree/7560101ec73331acb39a042bde6130e59e4bb630/rest-api-spec/src/main/resources/rest-api-spec/test/indices.update_aliases, but couldn't find a test that did this.
I'm starting to think that there's no way to do that. Am I wrong?

I don't think this is possible. There is no delete "if exists" and that is somewhat expected from any HTTP API.
However, you can definitely ignore the response for this particular case in your code instead of treating it as an error.

Related

OpenSearch/ElasticSearch - Authorisation error removing alias using only a wildcard

I'm trying to do something fairly simple - I have an alias that may be associated with one or more indices and I want to remove all associations and add a single one. Here is an example of what I'm doing (as suggested in here):
POST _aliases
{
"actions": [
{
"remove": {
"index": "*", // or "index": "_all"
"alias": "my_index"
}
},
{
"add": {
"index": "my_index_2023_01_05_17_18_29",
"alias": "my_index"
}
}
]
}
I'm using an admin user and for some reason I'm getting the following error message:
{
"error" : {
"root_cause" : [
{
"type" : "security_exception",
"reason" : "no permissions for [] and User [name=admin, backend_roles=[], requestedTenant=]"
}
],
"type" : "security_exception",
"reason" : "no permissions for [] and User [name=admin, backend_roles=[], requestedTenant=]"
},
"status" : 403
}
Kind of weird message - it doesn't specify what's the action that is failing (no permissions for []) so I cannot figure out which permissions I'm missing...
Either way, if instead of using the wildcard like that ("index": "*"), I do something like "index": "my_index*" it works - It is not exactly what I want but it executes.
Any idea why this might be happening and how can I solve this?
Thanks!
(Using OpenSearch 2.3)
When you specify index_pattern as "*", system indexes are also included. Even with the admin user, you cannot change the settings of the system indexes by default. You can delete the alias by using parameters like "index_name"* or "2022" instead of "*".
For example:
POST _aliases
{
"actions": [
{
"remove": {
"index": "index_name*",
"alias": "my_index"
}
}
]
}
Edit:
You can use the following API to remove all aliases without system indices.
DELETE *,.-*/_alias/my-alias
You can check if the API is correct before removing them
GET *,-.*/_alias/my-alias
Ref: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-delete-alias.html
Note: Unfortunately, it's not possible to use an expression like *,.-* in the POST _aliases body.

Complex document - provide mapping for a few fields only but keep the rest as is

i have some pretty complex documents i want to store in elastic to retrieve them and make them searchable. I dont know the whole strucutre of the data, so i would like elastic to "swallow" everything as i put it in but define some indexed fields to make searching possible.
As soon as i provide a mapping for elastic i will get errors, if i dont define mappings i fear that the index will grow too big because elastic will index too much.
I create the index using php, but it boils down to this:
PUT localhost:9200/name-of-index
{
"mappings": {
"_doc": {
"properties": {
"title": {
"type": "text"
}
}
}
}
}
Then - when i add one object to test everything i will get the following error:
status 400
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Rejecting mapping update to [name-of-index] as the final mapping would have more than 1 type: [_doc, 2187]"
}
],
"type": "illegal_argument_exception",
"reason": "Rejecting mapping update to [name-of-index] as the final mapping would have more than 1 type: [_doc, 2187]"
},
"status": 400
}
The command i use to post the document is about:
POST localhost:9200/name-of-index/2187
{
"title": "some title",
"otherField": "other value",
"obj": {
"nestedProp": "nestedValue",
"deepObj": {
"someStorage": [
...
{
"someVeryDeepProp": 1
}
...
]
}
},
"obj2": [
"str1",
"str2"
]
}
The node names are not real of course and the structure is much more complex than that. But i doubt that is the cause of my problem.
So how could i define a partial index and keep everything else as it is?
update: i forgot some elastic information..
{
"name" : "KNJ_3Eg",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "4M9p8XiaQHKPz7N2AAuVlw",
"version" : {
"number" : "6.6.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "a9861f4",
"build_date" : "2019-01-24T11:27:09.439740Z",
"build_snapshot" : false,
"lucene_version" : "7.6.0",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
Okay, that was just a stupid mistake, i forgot to add the type.
So the correct request to add a document should be:
POST localhost:9200/name-of-index/_doc/2187
{
"title": "some title",
"otherField": "other value",
"obj": {
"nestedProp": "nestedValue",
"deepObj": {
"someStorage": [
...
{
"someVeryDeepProp": 1
}
...
]
}
},
"obj2": [
"str1",
"str2"
]
}
I guess from version 7 onwards it will be without "_doc" because types are generally deprecated.

"_doc" mapping type name not accepted in ElasticSearch 5.6

I am looking at examples of single-type indices on ElasticSearch 5.6 to prepare for the removal of mapping types. Specifically, I am running the first example from the ElasticSearch page about the removal of types, on a fresh cluster running locally in Docker using the docker.elastic.co/elasticsearch/elasticsearch:5.6.5 image
Running the first example from section I linked to:
PUT localhost:9200/users
{
"settings": {
"index.mapping.single_type": true
},
"mappings": {
"_doc": {
"properties": {
"name": {
"type": "text"
},
"user_name": {
"type": "keyword"
},
"email": {
"type": "keyword"
}
}
}
}
}
I get the following error:
{
"error": {
"root_cause": [
{
"type": "invalid_type_name_exception",
"reason": "mapping type name [_doc] can't start with '_'"
}
],
"type": "invalid_type_name_exception",
"reason": "mapping type name [_doc] can't start with '_'"
},
"status": 400
}
I understand that fields with a leading underscore in the name are generally considered as reserved for ES internals; but I was assuming that _doc would be considered a special case starting with version 5.6, since the linked guide mentions:
Indices created in 6.x only allow a single-type per index. Any name can be used for the type, but there can be only one. The preferred type name is _doc so that index APIs have the same path as they will have in 7.0
Am I missing something, such as a cluster setting?
The document I linked to is the master version. In the 6.1 or 5.6 versions of that same document, there is no mention of _doc being the preferred name; which likely means that the ability to use _doc as a mapping type name will come with future 6.x versions.
I got the same issue while trying examples in the readme file from master branch https://github.com/elastic/elasticsearch/tree/master.
$ curl -XPUT 'elastic:#localhost:9200/twitter/_doc/1?pretty' -H 'Content-Type: application/json' -d '
{
"user": "kimchy",
"post_date": "2009-11-15T13:12:00",
"message": "Trying out Elasticsearch, so far so good?"
}'
{
"error" : {
"root_cause" : [
{
"type" : "invalid_type_name_exception",
"reason" : "Document mapping type name can't start with '_', found: [_doc]"
}
],
"type" : "invalid_type_name_exception",
"reason" : "Document mapping type name can't start with '_', found: [_doc]"
},
"status" : 400
}
Just checkout to the branch for version 5.6 https://github.com/elastic/elasticsearch/tree/5.6 and it looks everything is fine.
$ curl -XPUT 'http://localhost:9200/twitter/user/kimchy?pretty' -H 'Content-Type: application/json' -d '{ "name" : "Shay Banon" }'
{
"_index" : "twitter",
"_type" : "user",
"_id" : "kimchy",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}

Enable _size for exist index

I need to enable "_size" for an exist index. This question talks that it's possible. But it provides no example how to do it.
According to "Put Mapping API" I executed query
curl -XPUT "localhost:9200/my_index/_mapping/my_type?pretty" -d '{
"properties": {
"_size": {
"enabled": true,
"store" : true
}
}
}'
and got error:
{
"error" : {
"root_cause" : [
{
"type" : "mapper_parsing_exception",
"reason" : "No type specified for field [_size]"
}
],
"type" : "mapper_parsing_exception",
"reason" : "No type specified for field [_size]"
},
"status" : 400
}
What my mistake is? Please, show the correct version of this query.
You first need to install the mapper-size plugin:
bin/elasticsearch-plugin install mapper-size
Then you'll be able to enable it like this:
PUT my_index
{
"mappings": {
"my_type": {
"_size": {
"enabled": true
}
}
}
}
or
PUT my_index/_mapping/my_type
{
"_size": {
"enabled": true
}
}

Update by query in elasticsearch

I am trying to run update by query on my elasticsearch index using the method provided in this answer. This is the query that I've been trying to run:
curl -XPOST 'localhost:9200/my_index/_update_by_query' -d '
{
"query":{
"match":{
"latest_uuid":"d56ffe2095f511e6bcdd0acbdf0298e3"
}
},
"script" : "ctx._source.is_in_stock = \"false\";"
}'
But I keep getting the following error:
{
"error": {
"root_cause": [
{
"type": "class_cast_exception",
"reason": "java.lang.String cannot be cast to java.util.Map"
}
],
"type": "class_cast_exception",
"reason": "java.lang.String cannot be cast to java.util.Map"
},
"status": 500
}
What am I doing wrong here?
Found the solution.
Turns out that I had to use the following as script:
"script":{"inline":"ctx._source.is_in_stock = false"}
I think problem can be the \"false\" (String value) who no want to be cast.
curl -XPOST 'localhost:9200/my_index/_update_by_query' -d '
{
"query":{
"match":{
"latest_uuid":"d56ffe2095f511e6bcdd0acbdf0298e3"
}
},
"script" : "ctx._source.is_in_stock = false;"
}'
You can first try it. Waiting your feedback ! :)

Resources