Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Elasticsearch's search_as_you_type and completion suggester fields in action
PUT jobs
{
"mappings": {
"properties": {
"title": {
"type": "search_as_you_type"
}
}
}
}
POST jobs/_analyze
{
"field": "title",
"text": [
"Senior Software Developer"
]
}
POST jobs/_analyze
{
"field": "title._2gram",
"text": [
"Senior Software Developer"
]
}
POST jobs/_analyze
{
"field": "title._3gram",
"text": [
"Senior Software Developer"
]
}
POST jobs/_analyze
{
"field": "title._index_prefix",
"text": [
"Senior Software Developer"
]
}
PUT jobs/_bulk?refresh
{ "index" : {} }
{ "title" : "Software Developer" }
{ "index" : {} }
{ "title" : "Senior Software Developer" }
{ "index" : {} }
{ "title" : "Principal Sofware Developer" }
{ "index" : {} }
{ "title" : "Developer Advocate" }
{ "index" : {} }
{ "title" : "Developer 🥑" }
GET jobs/_search
{
"query": {
"match_phrase_prefix": { "title": "developer" }
}
}
GET jobs/_search
{
"query": {
"match_phrase_prefix": { "title": "software dev" }
}
}
GET jobs/_search
{
"query": {
"match_phrase_prefix": { "title": "senior dev" }
}
}
GET jobs/_search
{
"query": {
"multi_match": {
"query": "senior dev",
"type": "bool_prefix",
"operator": "and",
"fields": [
"title",
"title._2gram",
"title._3gram"
]
}
}
}
GET jobs/_search
{
"query": {
"multi_match": {
"query": "senior dev",
"type": "bool_prefix",
"operator": "or",
"fields": [
"title",
"title._2gram",
"title._3gram"
]
}
}
}
GET jobs/_search
{
"query": {
"multi_match": {
"query": "🥑",
"type": "bool_prefix",
"operator": "and",
"fields": [
"title",
"title._2gram",
"title._3gram"
]
}
}
}
DELETE jobs
PUT jobs_completion
{
"mappings": {
"properties": {
"title": {
"type": "completion"
}
}
}
}
PUT jobs_completion/_bulk?refresh
{ "index" : {} }
{ "title" : "Software Developer" }
{ "index" : {} }
{ "title" : "Senior Software Developer" }
{ "index" : {} }
{ "title" : "Principal Sofware Developer" }
{ "index" : {} }
{ "title" : "Developer Advocate" }
{ "index" : {} }
{ "title" : "Developer 🥑" }
GET jobs_completion/_search
{
"suggest": {
"job-title": {
"prefix": "software dev",
"completion": {
"field": "title"
}
}
}
}
GET jobs_completion/_search
{
"suggest": {
"job-title": {
"prefix": "senior dev",
"completion": {
"field": "title"
}
}
}
}
DELETE jobs_completion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment