5

I have created a index in elastic search with name test. Index mapping is as follow:

{
    "title": {
        "type": "text",
        "fields": {
            "raw": {
                "type": "keyword"
            }
        }
    },
    "url": {
        "type": "text",
        "fields": {
            "raw": {
                "type": "keyword"
            }
        }
    }
}

after creating index I have added following documents into it:

 {
    "title": "demo",
    "url": {
        "name": "tiger",
        "age": 10
    }
}

But I am getting following error:

{"mapper_parsing_exception","reason":"failed to parse field [url] of type [text]"}

can anyone help me into this?

3 Answers 3

2

Hi You need to create mapping like this

PUT test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mapping": {
    "title": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    },
    "url": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        }
      }
    }
  }
} 

And the document is

put test/doc/1
{
    "title": "demo",
    "url": {
        "name": "tiger",
        "age": 10
    }
}


GET test/doc/1

And the result is

{
  "_index" : "test",
  "_type" : "doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "demo",
    "url" : {
      "name" : "tiger",
      "age" : 10
    }
  }
}
1

If your documents look like this:

{
    "title": "demo",
    "url": {
        "name": "tiger",
        "age": 10
    }
}

Then your mapping needs to look like this, i.e. url is an object with the name and age fields:

{
  "title": {
    "type": "text",
    "fields": {
      "raw": {
        "type": "keyword"
      }
    }
  },
  "url": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      },
      "age": {
        "type": "integer"
      }
    }
  }
}
3
  • yes it is working fine but in my case my url field is not a json object every time some times it is like normal text like: {"url":"/abc"} and name and age is not come every time as a fields basically eveything is dynamic so I don't know wheather it is text or object and if it is object that I don't know the key of object so how I handle this case? May 15, 2019 at 4:35
  • That is indeed problematic, you need a processing step before ingestion in order to standardize your documents. An ingest pipeline may help here.
    – Val
    May 15, 2019 at 11:06
  • but here how I change the data type of url field I have already created url field of type text in mapping? and how to solve the error? Jun 10, 2019 at 6:09
0

One reason for this if you're on Elastic Cloud is that the data types are assigned to fields the first time they appear on an index. And it will throw this error if you send it a subsequent log with a different type in that field.

For me, the log field was a string in the first log sent to the index but an object in the second. So the second one got rejected.

Good explanation here: https://discuss.elastic.co/t/getting-illegal-state-exception-error-while-pushing-logs-to-elasticsearch/290029

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.