Only speficied fields


(Giuseppe Merlo) #1

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!


(David Pilato) #2

What is the error ?


(Giuseppe Merlo) #3

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


(David Pilato) #4

You can change the mapping to use strict dynamic mapping

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


(Giuseppe Merlo) #5

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.


(David Pilato) #6

I said strict.


(Giuseppe Merlo) #7

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


(David Pilato) #8

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
}

(Giuseppe Merlo) #9

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


(Giuseppe Merlo) #10

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.


(David Pilato) #11

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
}

(Giuseppe Merlo) #12

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.


(David Pilato) #13
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.


(Giuseppe Merlo) #14

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


(system) #15

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