Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch: create index with mappings

when i first create index and then add type with mapping it all work. but when i try to create an index with mappings in one call, i get error:

"type": "mapper_parsing_exception",
"reason": "Failed to parse mapping [my_type]: Expected map for property [fields] on field [type] but got a class java.lang.String",

how to fix it? my code below:

create:

PUT /my_index
{
    "settings": {
        "number_of_shards": 1,  
        "analysis": {
            "filter": {
                "my_shingle_filter": {
                    "type":             "shingle",
                    "min_shingle_size": 2, 
                    "max_shingle_size": 3, 
                    "output_unigrams":  false,   
                    "filler_token": ""
                },
                "kill_fillers": {
                    "type": "pattern_replace",
                    "pattern": ".*_.*",
                    "replace": ""
                }
            },
            "analyzer": {
                "my_shingle_analyzer": {
                    "type":             "custom",
                    "tokenizer":        "standard",
                    "filter": [
                        "standard"
                        ,"lowercase"
                        ,"stop"
                        ,"porter_stem"
                        ,"my_shingle_filter" 
                        ,"trim"
                    ]
                }}}}}

add type:

PUT /my_index/_mapping/my_type
{
    "my_type": {
        "properties": {
            "title": {
                "type": "string",
                "fields": {
                    "shingles": {
                        "type":     "string",
                        "analyzer": "my_shingle_analyzer"
                    }}}}}}

create index together with mapping:

PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "analysis": {
      "filter": {
        "my_shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 2,
          "output_unigrams": false
        }
      },
      "analyzer": {
        "my_shingle_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_shingle_filter"
          ]
        }}}
  },
  "mappings": {
    "my_type": {
      "properties": {
        "type": "string",
        "fields": {
          "shingles": {
            "type": "string",
            "analyzer": "my_shingle_analyzer"
          }}}}}}
like image 659
piotrek Avatar asked Oct 27 '25 15:10

piotrek


1 Answers

You're simply missing the title field in your second query:

PUT /my_index
{
  "settings": {
    "number_of_shards": 1,
    "analysis": {
      "filter": {
        "my_shingle_filter": {
          "type": "shingle",
          "min_shingle_size": 2,
          "max_shingle_size": 2,
          "output_unigrams": false
        }
      },
      "analyzer": {
        "my_shingle_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_shingle_filter"
          ]
        }}}
  },
  "mappings": {
    "my_type": {
      "properties": {
       "title": {                    <--- this line is missing
        "type": "string",
        "fields": {
          "shingles": {
            "type": "string",
            "analyzer": "my_shingle_analyzer"
          }}}}}}}                    <--- + one closing brace
like image 189
Val Avatar answered Oct 29 '25 07:10

Val