Custom Type Field Causes Multiple Types Error

I am new to Elastic search. Following the article https://www.elastic.co/guide/en/elasticsearch/reference/6.x/removal-of-types.html#_custom_type_field I made a index that could have multiple types using the type keyword.

Here is my song index:

% curl -X GET "localhost:9200/_all/_mapping?pretty=true"
{
  "song" : {
    "mappings" : {
      "_doc" : {
        "properties" : {
          "album" : {
            "type" : "text"
          },
          "artist" : {
            "type" : "text"
          },
          "date" : {
            "type" : "date"
          },
          "deleted" : {
            "type" : "boolean"
          },
          "filepath" : {
            "type" : "text"
          },
          "genre" : {
            "type" : "text"
          },
          "liked" : {
            "type" : "boolean"
          },
          "playlist" : {
            "type" : "text"
          },
          "title" : {
            "type" : "text"
          },
          "trackNumber" : {
            "type" : "text"
          },
          "type" : {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

Use the elastic search node.js library, I tried inserting a artist with:

const { client } = require('../app/init-db');

const upsertArtist = (artist) => {
  var doc = {
    index: 'song',
    type: 'artist',
    body: {
      artist: artist
    }
  };

  return client.index(doc);
};

upsertArtist(tags[0].artist).catch(e => console.log(e))

But I get this error:

{ Error: [illegal_argument_exception] Rejecting mapping update to [song] as the final mapping would have more than 1 type: [_doc, artist]
    at respond (/home/one/gitlab/echo-music-player/node_modules/elasticsearch/src/lib/transport.js:308:15)
    at checkRespForFailure (/home/one/gitlab/echo-music-player/node_modules/elasticsearch/src/lib/transport.js:267:7)
    at HttpConnector.<anonymous> (/home/one/gitlab/echo-music-player/node_modules/elasticsearch/src/lib/connectors/http.js:165:7)
    at IncomingMessage.wrapper (/home/one/gitlab/echo-music-player/node_modules/elasticsearch/node_modules/lodash/lodash.js:4949:19)
    at IncomingMessage.emit (events.js:165:20)
    at endReadableNT (_stream_readable.js:1101:12)
    at process._tickCallback (internal/process/next_tick.js:152:19)
  status: 400,
  displayName: 'BadRequest',
  message: '[illegal_argument_exception] Rejecting mapping update to [song] as the final mapping would have more than 1 type: [_doc, artist]',
  path: '/song/artist',
  query: {},
  body: 
   { error: 
      { root_cause: [Array],
        type: 'illegal_argument_exception',
        reason: 'Rejecting mapping update to [song] as the final mapping would have more than 1 type: [_doc, artist]' },
     status: 400 },
  statusCode: 400,
  response: '{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Rejecting mapping update to [song] as the final mapping would have more than 1 type: [_doc, artist]"}],"type":"illegal_argument_exception","reason":"Rejecting mapping update to [song] as the final mapping would have more than 1 type: [_doc, artist]"},"status":400}',
  toString: [Function],
  toJSON: [Function] }

I followed the example on custom types for the new way of doing multiple types. Why is it saying there would be two types? As far as I see, I did it exactly like the example.

You should write this I think:

const upsertArtist = (artist) => {
  var doc = {
    index: 'song',
    type: '_doc',
    body: artist
  };

  return client.index(doc);
};

Thank you, but this would not be using the custom types. From the article linked they do:

PUT twitter/_doc/user-kimchy
{
  "type": "user", 
  "name": "Shay Banon",
  "user_name": "kimchy",
  "email": "shay@kimchy.com"
}

PUT twitter/_doc/tweet-1
{
  "type": "tweet", 
  "user_name": "kimchy",
  "tweeted_at": "2017-10-24T09:00:00Z",
  "content": "Types are going away"
}

I see that they include _doc in the url ... twitter/_doc/user-kimchy. Could that be why? How would I control that with the Elasticsearch node.js api?

Could that be why?

Yes. That's why my proposal fixes that.

but this would not be using the custom types.

I think I see what you mean. try this then.

const upsertArtist = (artist) => {
  var doc = {
    index: 'song',
    type: '_doc',
    body: {
      type: 'artist',
      artist: artist
    }
  };

  return client.index(doc);
};

Thank you, this worked!

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