Simplifying search using elastic search and understanding search relevancy

elastic search — searching simplified

Introduction

Use case

Customer database
select * from customers where firstName like '%inputName' or lastName like '%inputName' or middleName like '%inputName'
Customer database with fullName column for easier querying .
select * from customers where fullName like '%inputName%';

Happy path

UnHappy path

PUT localhost:9200/customer{
"mappings": {
"customer": {
"properties": {
"firstName": {
"type": "text"
},
"middleName": {
"type": "text"
},
"lastName": {
"type": "text"
},
"fullName": {
"type": "text"
}
}
}
}
}
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "customer"
}

Data type — text

PUT localhost:9200/customer/customer/1{
"firstName" : "Steve",
"middleName" : "Rodger",
"lastName": "Waugh" ,
"fullName" : "Steve Rodger Waugh"
}
{
"_index": "customer",
"_type": "customer",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
GET localhost:9200/customer/customer/_search{
"took": 67,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 1,
"hits": [
{
"_index": "customer",
"_type": "customer",
"_id": "2",
"_score": 1,
"_source": {
"firstName": "Mark",
"middleName": "Rodger",
"lastName": "Waugh",
"fullName": "Mark Rodger Waugh"
}
},
{
"_index": "customer",
"_type": "customer",
"_id": "1",
"_score": 1,
"_source": {
"firstName": "Steve",
"middleName": "Rodger",
"lastName": "Waugh",
"fullName": "Steve Rodger Waugh"
}
},
{
"_index": "customer",
"_type": "customer",
"_id": "3",
"_score": 1,
"_source": {
"firstName": "Austin",
"middleName": "Steve",
"lastName": "Waugh",
"fullName": "Austin steve Waugh"
}
}
]
}
}

Search

Body :
{
"query" : {
"match" : {
"fullName" : "Waugh Steve"
}
}
}
{
"took": 26,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.5063205,
"hits": [
{
"_index": "customer",
"_type": "customer",
"_id": "1",
"_score": 0.5063205,
"_source": {
"firstName": "Steve",
"middleName": "Rodger",
"lastName": "Waugh",
"fullName": "Steve Rodger Waugh"
}
},
{
"_index": "customer",
"_type": "customer",
"_id": "3",
"_score": 0.5063205,
"_source": {
"firstName": "Austin",
"middleName": "Steve",
"lastName": "Waugh",
"fullName": "Austin steve Waugh"
}
},
{
"_index": "customer",
"_type": "customer",
"_id": "2",
"_score": 0.25316024,
"_source": {
"firstName": "Mark",
"middleName": "Rodger",
"lastName": "Waugh",
"fullName": "Mark Rodger Waugh"
}
}
]
}
}

Spelling mistakes and fuzzy search

PUT localhost:9200/customer/customer/5body : 
{
"firstName": "Suraj",
"lastName": "Sharma",
"fullName": "Suraj Sharma"
}
GET localhost:9200/customer/customer/_search
body :
{
"query" : {
"match" : {
"firstName" :
{
"query" : "Sooraj"
}
}
}
}
result:
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
GET localhost:9200/customer/customer/_search{
"query" : {
"match" : {
"firstName" :
{
"query" : "Sooraj",
"fuzziness" : "2"
}
}
}
result:
{
"took": 15,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.17260925,
"hits": [
{
"_index": "customer",
"_type": "customer",
"_id": "5",
"_score": 0.17260925,
"_source": {
"firstName": "Suraj",
"lastName": "Sharma",
"fullName": "Suraj Sharma"
}
}
]
}
}
GET localhost:9200/customer/customer/_search{
"query" : {
"match" : {
"firstName" : "Sooraj~"
}
}
}

Edit distance

2 edits can transform Sooraj into Suraj

Phonetic searches :

PUT : localhost:9200/employee/employee{
"settings": {
"index": {
"analysis": {
"analyzer": {
"phonetic_analyzer": {
"tokenizer": "standard",
"filter": [ "lowercase", "phonetic_filter" ]
}
},
"filter": {
"phonetic_filter": {
"type": "phonetic",
"replace": false,
"encoder" : "double_metaphone"
}
}
}
}
},
"mappings": {
"employee": {
"properties": {
"firstName": {
"type": "text",
"analyzer": "phonetic_analyzer"
}
}
}
}
}
PUT localhost:9200/employee/employee/1body : 
{
"firstName" : "stephen"
}
GET localhost:9200/employee/employee/_search body : 
{
"query": {
"match": {
"firstName": "steven"
}
}
}
result : 
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.36165747,
"hits": [
{
"_index": "employee",
"_type": "employee",
"_id": "1",
"_score": 0.36165747,
"_source": {
"firstName": "stephen"
}
}
]
}
}

Summary

Next

Interests : software design ,architecture , search, open banking , machine learning ,mobility

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store