We are working on job search portal, where data object is in following format (sample) is ingested in elasticsearch:
{
"Title": "web developer",
"Company": "ABC Corp",
"Pay": 100000,
"keywords": "web search wcf",
"Country": "USA"
}
Here user gives search word and country, We want to display based on following criteria:
- All the jobs which match search word in
Titleorkeywords, and match country inCountryfield. Results should have complete word match on top and partial word match on bottom. All complete match should be sorted byPay. - We want to show 20 results to user, if there are less than 20 results in given country then we want to show results for other countries order by match type (complete/partial) and then by
Pay.
We have tried several way to query these data in one query, but nothing seems to work. We are not trying to get these results in two queries (one query for country filter and one without country filter), and combine results for both.
Can anyone help us in building this query.
Edit
I think I wasn't clear enough. The given Json is example of one record in ES. Now if user types search "wcf" and choose country "USA" then we need to show results in below order:
1. All the results matching with keyword "wcf" in following order
a. All results with country USA sorted by pay.
b. Results that match keyword but from other country and sorted by pay.
below is the query we are using:
GET resume/_search
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"bool":
{
"must": [
{
"multi_match": {
"query": "wcf",
"fields": ["title", "keywords"]
}
}
]
, "should": [
{
"match_phrase_prefix": {
"title": "wcf"
}
}
]
, "should": [
{
"match_phrase_prefix": {
"keywords": "wcf"
}
}
]
}
}
]
}
},
"functions": [
{
"script_score": {
"script": "_score * (doc['Country'].value == 'USA' ? 100000*doc['pay'].value : 1*doc['pay'].value)"
}
}
]
}
}
}
This works for our scenario. Please let me know if you can suggest any better alternative.