How to add two _type under same index. But both are having different Json data

I'm working on Elastic search to store and retrieve schema less json's. I have created index called "index002", I tried to create document with two different _type. Both having different json and some keys are still exists with different datatype.

Doc1:
{
"foo": "bar",
"date": "23-3-90",
"Type": "Open"
}

Doc 2:

{
"Name": "John",
"date": "23-3-90",
"Type": 2
}

It throws an exception

ERROR:root:b'{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse [Type]"}],"type":"mapper_parsing_exception","reason":"failed to parse [Type]","caused_by":{"type":"number_format_exception","reason":"For input string: \"Open\""}},"status":400}'

Your mapping probably has Type field as a number.
That's why you can't index a String content in it.

1 Like

Can i support any datatype for the field "Type"? Because the data could be in any datatype for my case. How could i index a document with different types?

You can only if you just want to store the field but not index it.
In general, you could also just use text like:

"foo": "bar"

or

"foo": "4"

Can i conclude that we are not able to map two data types for single field inside same index right?

Yes. I believe we can say that.
That's basically what we can say for most databases and most languages I guess.

But, you can use multifields may be to index the same value (ie a number) as both a number and a text.

But I'm curious about the use case. Why are you asking this?

Thanks for your answer. I'm in the need of storing different JSON data in the same _type. Because Im using schema less mongoDB as my database system earlier. Now I need to store some data's from mongo to elastic search via REST API.

In Mongo, we can store different JSON data in same collection as documents. Im trying to achieve the same here in ES.

Storing and indexing are different things. That's why I asked in the first place.

Look at https://www.elastic.co/guide/en/elasticsearch/reference/current/enabled.html

Yeah. Now we are in same way. Can i search or filter the stored data ?

No. If you don't index, you can't search.

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