1

I have some json (below) that I need to index in elasticsearch:

{
  "array": [
      "item1",
      {
        "name": "item2"
      }
    ]
}

When I try and index this type of json structure I get an error:

{
   "error": "MapperParsingException[failed to parse [array]]; nested: ElasticsearchIllegalArgumentException[unknown property [name]]; ",
   "status": 400
}

Now I understand that elasticsearch is getting confused because array contains stings type for item 1 and then an object for item2.

My question is, how would I define a mapping to handle this type of data?

1
  • I don't think you can. You need to think well about the data you want to put in and adjust the mapping according to what ES can do. Commented Nov 18, 2014 at 11:21

1 Answer 1

1

The only way that this can work is to define your index mapping so that the array field is not enabled and therefore make it not searchable (Elasticsearch avoids parsing that field).

curl -XPUT localhost:9200/your-index -d '{
  "mappings": {
    "your-type" : {
      "properties" : {
        "array" : {
          "type" : "object",
          "enabled" : false
        }
      }
    }
  }
}'

You must do this because Elasticsearch will dynamically map the type based on what it sees (if you don't tell it the mapping). The first element is going to be a string in your example, so it will map array to a string. When it then hits the object, it does not know what to do, so it has to stop.

This is not as useless as it sounds because you can still retrieve the value(s) of the array from the document's _source (assuming it's stored, which it is by default). However, it does mean that the information in array is not searchable within Elasticsearch. You can get the _source by doing a search or a get request.

As Andrei commented, you may benefit from having a cleaner object, but sometimes this is just the way that it is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.