Only speficied fields

Hi forum!

There is a way that make me able to insert into an index only specified fields?

Here is an example:

I create the Anime index with only name field, this is my Anime.mapping file:

{
    "properties": {
        "name": {
            "index": "not_analyzed",
            "type": "string"
        }
    }
}

If i try to put a document like this:

 { 
     "name": "One Piece"
    }

then the result has to be ok.

Instead, if i try to put a document looks like:

     { 
         "name": "One Piece",
         "author": "Eichiro Oda"
        }

Elasticsearch must return error.

Thank you for helping me!

What is the error ?

There isn't an error. I want that i can't insert other fields except for name.

You can change the mapping to use strict dynamic mapping

https://www.elastic.co/guide/en/elasticsearch/reference/5.4/dynamic.html

That not works.

This is my Anime.mapping file:

{
        "mappings": {
        "my_type": {
          "dynamic": false, 
          "properties": {
            "user": { 
    	"dynamic": false,
              "properties": {
                "name": {
                  "type": "string"
                }
        }
       }
      }
     }
    }
   }

My first Put:
curl -XPUT 'http://localhost:9200/anime/user/1' -d '{"name":"OP"}'

My second Put:
curl -XPUT 'http://localhost:9200/anime/user/9' -d '{"name":"OP", "author": "er"}'

Both request are successful.

I said strict.

Sorry, i didn't see it.
But It has the same behavior. :pensive:

It works for me:

DELETE test
PUT test
{
  "mappings": {
    "doc": {
      "dynamic": "strict",
      "properties": {
        "user": {
          "type": "text"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "foo": "bar"
}

Gives:

{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [foo] within [doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [foo] within [doc] is not allowed"
  },
  "status": 400
}
1 Like

What is your elasticsearch.yml configuration? Do you change anything on it?

UPDATE:

it works, but only for field changes.

This is my Anime.mapping file:

{
  "mappings": {
    "doc": {
      "dynamic": "strict",
      "properties": {
        "name": {
          "type": "string"
        }
      }
    }
  }
}

if i try to put this:

curl -XPUT 'http://localhost:9200/anime/doc/1' -d '{"name":"OP"}'

it works.

if i try to put this:

curl -XPUT 'http://localhost:9200/anime/doc/1' -d '{"foo":"OP"}'

it gets the error:

{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [foo] within [doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [foo] within [doc] is not allowed"
  },
  "status": 400
}

Correct! But if i try to put this:

curl -XPUT 'http://localhost:9200/anime/user/9' -d '{"name":"OP", "author": "er"}'

it works too. I want in this case an error like before.

Nope.

PUT test/doc/9
{
  "name": "OP",
  "author": "er"
}

gives:

{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [name] within [doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [name] within [doc] is not allowed"
  },
  "status": 400
}

Did you have changed your mapping in order to use "name"?
If not, can you try with "foo" instead of "name", please? Thank you so much.

DELETE test
PUT test
{
  "mappings": {
    "doc": {
      "dynamic": "strict",
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }
}
PUT test/doc/1
{
  "name": "OP",
  "author": "er"
}

gives

{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [author] within [doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [author] within [doc] is not allowed"
  },
  "status": 400
}

If you can't reproduce it please share a full formatted script as I just did so I can check it.

1 Like

Now it seems to work! I follow your steps and they work fine! Thank you for your patience!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.