Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion between mappings and types in ElasticSearch

Sorry, I'm new to ElasticSearch.

http://www.elasticsearch.org/guide/reference/api/admin-indices-put-mapping.html

This document says you can "creates a mapping called tweet within the twitter index"

$ curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d '
{
    "tweet" : {
        "properties" : {
            "message" : {"type" : "string", "store" : "yes"}
        }
    }
}
'

As someone told me on ES IRC channel, /twitter/tweet twitter=index, tweet=type

But what happens if I do the following?

$ curl -XPUT 'http://localhost:9200/twitter/XXX/_mapping' -d '
{
    "YYY" : {
        "properties" : {
            "message" : {"type" : "string", "store" : "yes"}
        }
    }
}
'

If I already provided the type name in the url, why should i still provide a type name in the content? If I provide the type name in the content, why can't I just call some url like:

$ curl -XPUT 'http://localhost:9200/twitter/_mapping' -d '

While reading the doc, for me it says "creates a mapping called tweet within the twitter index", this means XXX is the mapping name and YYY is the type name.

Thus if there is a mapping name, there can normally be many "mappings" for an index

So, in the end, XXX and YYY are/should be the same?


It's not what i understand from the doc, but what I think is: - One index can have types - Types have a mapping Thus we don't create a mapping like the documentation says, but we create a type, that has a mapping, or we update the type's mapping no?

And on an index where I don't want to use any type (all documents indexed are the same kind of data), but I want to create a mapping for that index, am I supposed to handle that by creating only one type with its mapping, and always use that type (in the CouchDB river for example)?

Thanks

like image 557
Sebastien Lorber Avatar asked Jun 05 '12 16:06

Sebastien Lorber


People also ask

What is mapping type in Elasticsearch?

Elasticsearch supports two types of mappings: “Static Mapping” and “Dynamic Mapping.” We use Static Mapping to define the index and data types.

Can Elasticsearch index have multiple mappings?

So it is recommended to save one mapping type into one index. From Elasticsearch version 6.0 by default index doesn't allow multiple types per index. The better option is to always have one document type per index. The single responsibility of the index is to maintain the single document type/mapping type per index.

Why do we need to use mapping on an Elasticsearch index?

Mapping determines how a document and its fields are indexed and stored by defining the type of each field. It contains a list of the names and types of fields in an index. Depending on its type, each field is indexed and stored differently in Elasticsearch.

Can I change mapping for existing index Elasticsearch?

It is not possible to update the mapping of an existing field. If the mapping is set to the wrong type, re-creating the index with updated mapping and re-indexing is the only option available. In version 7.0, Elasticsearch has deprecated the document type and the default document type is set to _doc.


1 Answers

In the current version of elasticsearch search (0.19.4) "YYY" is ignored and mapping is assigned to the "XXX" type.

Indeed, you can think of creating mappings as creating types. Internally, types are represented in terms of mappings and documentation just reflects this behavior. If all your records have the same type, you should treat the index as an index with only one type. Type is used to "namespace" records of different types and therefore it's mandatory. An id itself doesn't uniquely identify a record. To identify a record in elasticsearch, you need to have index, type and id. This is why type is a required paramater for the get operation, for example. For other operations, if type is not specified, elasticsearch assumes that you want to perform the operation with records of all types.

Note, that types can also be created dynamically at any time by adding a record of a new type. So, an index with only one type is an index that doesn't have the second type yet.

like image 113
imotov Avatar answered Sep 22 '22 12:09

imotov