Elasticsearch: how to make all properties of an object type not parsed?

I need to create an Elasticsearch mapping with an Object field whose keys are not known in advance. In addition, the values ​​can be integers or strings. But I want the values ​​to be stored as unparsed fields, if they are strings. I tried the following mapping:

PUT /my_index/_mapping/test
{
  "properties": {
    "alert_text": {
      "type": "object",
      "index": "not_analyzed"
    }
  }
}

Now the index is created perfectly. But if I insert the following values:

POST /my_index/test
{
  "alert_text": {
    "1": "hello moto"
  }
}

The value "hello moto" is stored as a parsed field using a standard analyzer. I want it to be stored as a non-parsed field. Is it possible if I do not know in advance that all keys can be present?

+4
source share
1 answer

. .

, , , , .. alert_text not_analyzed:

PUT /my_index
{
    "mappings": {
        "test": {
            "properties": {
                "alert_text": {
                  "type": "object"   
                }
            },
            "dynamic_templates": [
                {
                    "alert_text_strings": {
                        "path_match": "alert_text.*", 
                        "match_mapping_type": "string",
                        "mapping": {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                }
            ]
        }
    }
}

POST /my_index/test
{
  "alert_text": {
    "1": "hello moto"
  }
}

, :

GET /my_index/_mapping

:

{
   "my_index": {
      "mappings": {
         "test": {
            "dynamic_templates": [
               {
                  "alert_text_strings": {
                     "mapping": {
                        "index": "not_analyzed",
                        "type": "string"
                     },
                     "match_mapping_type": "string",
                     "path_match": "alert_text.*"
                  }
               }
            ],
            "properties": {
               "alert_text": {
                  "properties": {
                     "1": {
                        "type": "string",
                        "index": "not_analyzed"
                     }
                  }
               }
            }
         }
      }
   }
}

, alert_text.1 not_analyzed.

+5

Source: https://habr.com/ru/post/1623319/


All Articles