TIMEX
TIMEX

Reputation: 272094

How can I create a mapping for two types in ElasticSearch?

I would like to index user IDs and tag IDs.

I send a PUT request to https://ip//elasticsearch/myIndex

{ 
  "mappings" : {
    "users" : {
      "properties" : {
        "id" : {"type": "keyword" }
      }
    },
    "tags" : {
      "properties" : {
        "id" : {"type": "keyword" }
      }
    }
  }
}

However, I receive this response:

{
    "error": {
        "root_cause": [
            {
                "type": "illegal_argument_exception",
                "reason": "Rejecting mapping update to [myIndex] as the final mapping would have more than 1 type: [users, tags]"
            }
        ],
        "type": "illegal_argument_exception",
        "reason": "Rejecting mapping update to [myIndex] as the final mapping would have more than 1 type: [users, tags]"
    },
    "status": 400
}

How can I solve this error?

Upvotes: 0

Views: 142

Answers (2)

hudsonb
hudsonb

Reputation: 2294

As stated by @ben5556, you can only have one type per index in elasticsearch 6+. However, you can mimic multiple types per index by including your own "type" field.

{ "mappings" : { "ids" : { "properties" : { "id" : {"type": "keyword" } "type": {"type": "keyword" } } } } }

Then when you index a document, you include the "type":

{ "type": "user", "id": "12345" }

This will allow you to filter by type when querying the index (using a termsQuery). Which is all elasticsearch was really doing behind the scenes for you anyways back when it supported multiple types.

Upvotes: 0

ben5556
ben5556

Reputation: 3018

From Elastic 6.x you cannot have more than 1 mapping type. Use a single mapping type. Instead use custom type field. See this link and also this link.

Upvotes: 3

Related Questions