Demo code and sample employees data to implement the "Search as you type" feature on elasticsearch.
Written the middleware API in python
using flask. Used JQuery for javascript operations.
Assuming you have successfully installed Elasticsearch and Kibana on your machine and it is working perfectly. Kindly refer respective installation document.
OR
You can run Elasticsearch on the cloud with a few clicks.
- Refer Document to install
python3
&pip3
on your system. - Install
flask
pip3 install flask
- Install
elasticsearch
package
pip3 install elasticsearch
git clone https://github.com/ashishtiwari1993/search_as_you_type.git
cd search_as_you_type
Make sure Elasticsearch and kibana are up and running fine on your machine.
PUT /sayt?pretty
{
"mappings": {
"properties": {
"first_name": {
"type": "search_as_you_type"
},
"last_name": {
"type": "search_as_you_type"
},
"street_address": {
"type": "search_as_you_type"
},
"company": {
"type": "search_as_you_type"
},
"email": {
"type": "search_as_you_type"
}
}
}
}
Sample data.json file is given which need to load with the help of bulk API.
curl -s -H "Content-Type: application/x-ndjson" --cacert /path/to/http_ca.crt -u "username:password" -XPOST "https://localhost:9200/_bulk" --data-binary "@data.json"
Do not forget to change the elasticsearch's endpoint.
Open api.py
and change elasticsearch endpoint accordingly.
es = Elasticsearch(
"https://localhost:9200",
ca_certs = "path/to/ca_certificate",
basic_auth = ("username", "password")
)
python3 api.py
This will start the API service on port 5001
.
Open index.html
on your browser.
myproject/
api.py
templates/
index.html