I'm an iOS Swift developer and I'm using ElasticSearch inside my app. I'm trying to wrap my head around how to declare a type in ES , what's the difference between a type and document, and which is most similar to an object/data model.
In Swift I would create an object or data model like this:
class Sneakers {
var condition: String?
var name: String?
}
This is saying I created an object named Sneakers with 2 properties: "condition" and "name" both of Optional (the question mark) of type String.
I know to create and set my ES to an Index I use the following:
curl -XPOST <bonsai_url>/myIndexName //I'm using Heroku & Bonsai for my ES cluster
I can then set a type like so
curl -XPOST <bonsai_url>/myIndexName/sneakerType
Where I'm lost at is how do I set the index to use my Sneakers data model as a reference to search on? Inside my app users can search for footwear based on the the Sneakers name (Nike, adidas, etc) and condition (old, new, used etc).
I know it's something along the lines of
curl -XPOST <bonsai_url>/myIndexName/sneakerType -d '
{
"sneakers": {
"properties": {
"condition": {
"type": string
},
"name": {
"type": string
}
}
}
}
'
My questions would be in ES:
type and a document
fields be the equivalent of properties? index name and type, how do I make the type to
refer to my data model and it's properties so that it can be searched_mapping for and should I use that in my curl command instead?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.json
and 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 [email protected]
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 [email protected]
Then run:
curl -XGET <BONSAI_URL>/firebase/_mappings?pretty
Search it to see what happens
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With