Elasticsearch is the #1 ranked search engine on DB Engines Ranking, and the #7 system overall.
We’re able to load data into the Elastic NoSQL store, and perform a number of searches over that data. We create an index to store the data, and add data as JSON documents. We can designate a schema, but we’re free to add fields on the fly.
There’s a 14 day free trial available for the cloud version. There’s also a Basic version that we can download and run locally.
Downloadable Version:
If you want to download and run Elasticsearch locally, there’s versions available for Windows, Linux, Mac, as well as a Docker file. The package is available here. You’ll also want to download the Kibana tool as a front end.
I’m going to opt for the cloud version free trial, which will be a quicker setup.
Cloud Setup:
Signup on the Elastic site for the trial. No credit card is needed, but you’ll use an email address, and be asked for your name and company.
We’ll create the deployment. Google Cloud is the default provider, but we can opt for Azure or AWS.
You’ll also be asked questions on what you want to use Elastic for. I opted for search. The setup took be to the search section to create an index, but I opted out of the setup. Instead, I’ll use the Elastic API and Dev Tools.
Index Creation:
We’ll create an index to hold our data, named ‘search-roster’.
From the home page, we can select the menu and the top left. At Management, Dev Tools, we can access the Console.
From the console, we can create an index to hold our data with this command.
PUT /search-roster
{
"mappings": {
"properties": {
"JerseyNo": { "type": "integer" },
"Player": { "type": "keyword" },
"Position": { "type": "keyword" },
"Experience": { "type": "integer" },
"College": { "type": "keyword" },
"Acquired": { "type": "text" }
}
}
}
Running the console commands seemed to work best when I include a return after the last light, then selected the entire command. Clicking the green arrow executes the command.
There are several data types we can use for our schema. For our test, we’ve used integer for numerical values. There’s a keyword type that we’ll use for strings where we’re looking for an exact match, like the 1 or 2 character position code. For longer strings where we may look for a partial search, we’ll use text. Our “Acquired” field is made up of concatenated values on how and when a player joined the team.
We can add one document:
POST /search-roster/_doc
{
"JerseyNo": 0,
"Player": "Lorenzo Carter",
"Position": "OLB",
"Experience": 2018,
"College": "Georgia",
"Acquired": "FA - 22 - NYG"
}
We can also bulk load multiple documents:
POST /search-roster/_bulk
{ "index": {} }
{ "JerseyNo": 1, "Player": "Jeff Okudah", "Position": "CB", "Experience": 2020, "College": "Ohio State", "Acquired": "TR w DET - 23"}
{ "index": {} }
{ "JerseyNo": 3,"Player": "Jessie Bates III","Position": "S","Experience": 2018, "College": "Wake Forest", "Acquired": "FA - 23 - CIN"}
I’ve created a file of commands that will create 53 records for a football team roster.
Search:
We can search with no parameters with this command:
GET /search-roster/_search
The default seems to be to return 10 records, although the results pane will give us the total count under hits.total.value.
We can also search using criteria. We’ll look for players on the team from the University of Georgia:
GET /search-roster/_search
{
"query": {
"match": {
"College": "Georgia"
}
}
}
Which returns two matches. It doesn’t return the player from Georgia Southern, since this is a keyword field.
We can also search the text field for partial matches:
GET /search-roster/_search
{
"query": {
"match": {
"Acquired": "Draft"
}
}
}
This returns the 19 players that were drafted.
Cleanup:
A simple DELETE command to drop the index:
DELETE /search-roster
Next Steps:
There’s definitely a lot more to look into with Elasticsearch. There’s many more options for search, like geo-searches to find locations within a certain distance, aggregations, etc.
For this test we used the console, but a real life scenario would involve accessing data from an application, which can be done through the same API the Console uses.
It’s also possible to set up integrations through the Elastic connectors to get data from a database, or from other data sources. These can be a one-time load, or set up on a schedule.
Links:
Maël Fabien – Getting Started with Dev Tools in Elasticsearch