Reputation: 11
I have set of designations stored in Elasticsearch DB, like Senior Software Engineer Assistant Software Engineer Staff Software Engineer Group Software Engineer Software Engineer
Problem Statement: If query is made for "Software Engg", order of results are not prioritized considering Software to be first word, instead results are showing Assistant Software Engineer on top
How can we configure search to raise the ranking for entries where search phrase is part of beginning ? So that order should be Software Engineer Assistant Software Engineer Group Software Engineer Senior Software Engineer
Mapping used currently:
{
"properties": {
"name":{"type": "search_as_you_type"}
}
}
Query used for autocomplete search:
s = s.query(Q("match_phrase_prefix",name={"query":input_string})).sort('_score')
res = s.execute()
Upvotes: 1
Views: 191
Reputation: 3271
One possibility is to use the completion suggester. It provides the "weight" that allows you to rank your suggestions. Below I made an example that allows you to manipulate the order of suggestions. I hope it helps.
PUT idx_test
{
"mappings": {
"properties": {
"role": {
"type": "text"
},
"suggest": {
"type": "completion"
}
}
}
}
POST idx_test/_doc
{
"role":"Senior Software Engineer Assistant",
"suggest": [
{
"input": "Senior Software Engineer Assistant",
"weight": 50
},
{
"input": "Software Engineer Assistant",
"weight": 50
}
]
}
POST idx_test/_doc
{
"role":"Software Engineer Staff",
"suggest": [
{
"input": "Software Engineer Staff",
"weight": 40
}
]
}
POST idx_test/_doc
{
"role":"Software Engineer Group",
"suggest": [
{
"input": "Software Engineer Group",
"weight": 20
}
]
}
POST idx_test/_doc
{
"role":"Software Engineer",
"suggest": [
{
"input": "Software Engineer",
"weight": 10
}
]
}
GET idx_test/_search
{
"suggest": {
"role-suggest": {
"prefix": "software engg",
"completion": {
"field": "suggest",
"fuzzy": {
"fuzziness": 2
}
}
}
}
}
Results:
{
"suggest": {
"role-suggest": [
{
"options": [
{
"_source": {
"role": "Senior Software Engineer Assistant",
"suggest": [
{
"input": "Senior Software Engineer Assistant",
"weight": 50
},
{
"input": "Software Engineer Assistant",
"weight": 50
}
]
}
},
{
"_source": {
"role": "Software Engineer Staff",
"suggest": [
{
"input": "Software Engineer Staff",
"weight": 40
}
]
}
},
{
"_source": {
"role": "Software Engineer Group",
"suggest": [
{
"input": "Software Engineer Group",
"weight": 20
}
]
}
},
{
"_source": {
"role": "Software Engineer",
"suggest": [
{
"input": "Software Engineer",
"weight": 10
}
]
}
}
]
}
]
}
}
Upvotes: 1