Skip to content

Instantly share code, notes, and snippets.

@kosho
Last active December 5, 2018 01:36
Show Gist options
  • Save kosho/49cb0b17cbfbbf65e61d8292b175a4d3 to your computer and use it in GitHub Desktop.
Save kosho/49cb0b17cbfbbf65e61d8292b175a4d3 to your computer and use it in GitHub Desktop.
Usage example of Function Score Query to control search relevancy.
##################################################
# Function Score Query Example
##################################################
# Note: `now` should be replaced by `2018-12-06` for expected results
# Prepare for items index
DELETE items
PUT items
{
"mappings": {
"doc": {
"properties": {
"origin": {
"properties": {
"location": {
"type": "geo_point"
},
"prefecture": {
"type": "keyword"
}
}
},
"price": {
"type": "long"
},
"name": {
"type": "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"promotion": {
"type": "long"
},
"arrival_date": {
"type": "date"
}
}
}
},
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "0"
}
}
}
POST items/doc/_bulk
{"index":{}}
{"arrival_date":"2018-12-02","name":"Tsugaru Apple","origin":{"prefecture":"Aomori","location":"40.82,140.73"},"price":310,"promotion":2}
{"index":{}}
{"arrival_date":"2018-11-29","name":"Shinano Apple","origin":{"prefecture":"Nagano","location":"36.65,138.17"},"price":280,"promotion":10}
{"index":{}}
{"arrival_date":"2018-12-04","name":"Fuji Apple","origin":{"prefecture":"Akita","location":"39.69,139.78"},"price":150,"promotion":1}
{"index":{}}
{"arrival_date":"2018-12-04","name":"Mikkabi Mandarine Orange","origin":{"prefecture":"Shizuoka","location":"34.97,138.38"},"price":80,"promotion":1}
# Show Apples Only
GET items/_search
{
"profile": "true",
"query": {
"match": {
"name": "apple"
}
}
}
# Use function_score query instead
GET items/_search
{
"query": {
"function_score": {
"query": {
"match": {
"name": "apple"
}
}
}
}
}
# Bias the score by arrival_date (datetime)
GET items/_search
{
"query": {
"function_score": {
"query": {
"match": {
"name": "apple"
}
},
"linear": {
"arrival_date": {
"origin": "now",
"scale": "7d",
"offset": "0d"
}
}
}
}
}
# Bias the score by promotion (number)
GET items/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match": {
"name": "apple"
}
},
"linear": {
"promotion": {
"origin": "10",
"scale": "10",
"offset": "0"
}
}
}
}
}
# Bias the score by location (geo distance)
GET items/_search
{
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match": {
"name": "apple"
}
},
"linear": {
"origin.location": {
"origin": "35.68,139.69",
"offset": "0",
"scale": "300km"
}
}
}
}
}
# Bias the score by arrival_date + promotion
GET items/_search
{
"profile": true,
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match": {
"name": "apple"
}
},
"functions": [
{
"linear": {
"arrival_date": {
"origin": "now",
"scale": "7d",
"offset": "0d"
}
}
},
{
"linear": {
"promotion": {
"origin": "10",
"scale": "10",
"offset": "0"
}
}
}
]
}
}
}
# Using script_score instead **make sure which query consts more**
GET items/_search/
{
"profile": true,
"query": {
"function_score": {
"score_mode": "sum",
"query": {
"match": {
"name": "apple"
}
},
"script_score": {
"script": "doc['promotion'].value - (new Date().getTime() - doc['arrival_date'].value.toInstant().toEpochMilli()) / 1000000 / 60"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment