How to create an ElasticSearch Type and make it searchable inside the Index How to create an ElasticSearch Type and make it searchable inside the Index elasticsearch elasticsearch

How to create an ElasticSearch Type and make it searchable inside the Index


In ES a type is the equivalent of a class/data model/object.

In ES the fields are the equivalent of a class/data model/object's properties.

A document is the result of what is actually getting searched inside the index. If inside the index there are 2 pairs of Sneaker types then the index would have 2 documents inside of it.

Mappings is 1- how you make your type searchable inside the index and 2- how you set the index so that the type and its fields would equate to an object/data model and its properties. This type and its fields are what you’re going to run your searches on.

I first created a type by creating a file named sneakerfile.jsonand added this code inside of it

{   "sneakers": { // this is a type      "properties": {        "condition": { // it has a field named ‘condition’          "type": "string"        },        "name": {  // it has a field named ‘name’          "type": "string"        }      }   }}// sneakers is being treated like my Sneakers Swift class/dataModel from the original question// the fields “condition” & “name” are of type String just like my Swift's Sneakers’ class' properties (since Optionals are a Swift thing that’s not included here)

Then inside terminal I created my ES index which is named firebase by running:

curl -XPOST <BONSAI_URL>/firebase

1- Now that I have an index named firebase, 2- an ES type named sneakers, 3- I now need to make the type searchable in the index by 4- populating the _mappings key with it:

curl -XPOST <BONSAI_URL>/firebase/_mappings/sneakers -d@sneakerfile.json

Now that I want to see what's inside my mappings key when I run:

curl -XGET <BONSAI_URL>/firebase/_mappings?pretty

I'll get

{  "firebase" : {    "mappings" : {      "sneakers" : {        "properties" : {          "condition" : {            "type" : "string"          },          "name" : {            "type" : "string"          }        }      }    }  }}// Final Result -the index named firebase now can return documents of type ‘sneakers’. You can search for those documents by searching on the ‘condition’ and ‘name’ fields

As a side note the above answer definitely 100% works so you can use it. I haven’t been back to this answer in over 2 yrs but maybe try creating and setting another type to a sneakersTestFile.json like this. TEST TEST TEST to see what happens. I haven’t tried this below myself but I know it’s easier to read.

{   "sneakersTest": {        "condition": "string"        "name": "string"   }}

Assuming you already created the index named firebase run:

curl -XPOST <BONSAI_URL>/firebase/_mappings/sneakersTest -d@sneakersTestFile.json

Then run:

curl -XGET <BONSAI_URL>/firebase/_mappings?pretty

Search it to see what happens