Skip to content

Instantly share code, notes, and snippets.

@tyler-smith
Last active April 2, 2016 00:44
Show Gist options
  • Save tyler-smith/1c30566b01aa76bd75e1 to your computer and use it in GitHub Desktop.
Save tyler-smith/1c30566b01aa76bd75e1 to your computer and use it in GitHub Desktop.
ES function_score troubles with filtered query_string query
// Final working version
{
"query":{
"filtered":{
"query":{
"function_score":{
"query":{
"query_string":{
"query":"this is a cool search",
"fields":[
"title",
"text"
]
}
},
"script_score":{
"script":"_score * log(doc['my_field'].value+1)"
}
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"account":1
}
}
],
"must_not":[
{
"terms":{
"state":[
"spam",
"deleted"
]
}
}
]
}
}
}
}
}
// Original question:
// Working function_score query, but not the filtered query_string search I need
{
"query":{
"function_score":{
"query":{
"match_all":{
}
},
"script_score":{
"script":"_score * log(doc['my_field'].value+1)"
}
}
}
}
// Working filtered query_string search but not using the function_score like I need
{
"query":{
"filtered":{
"query":{
"query_string":{
"query":"this is a cool search",
"fields":[
"title",
"text"
]
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"account":1
}
}
],
"must_not":[
{
"terms":{
"state":["spam","deleted"]
}
}
]
}
}
}
}
}
//
// A couple failed attempts
//
// Returns: "[filtered] query does not support [function_score]"
{
"query":{
"filtered":{
"function_score":{
"query":{
"query_string":{
"query":"this is a cool search",
"fields":[
"title",
"text"
]
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"account":1
}
}
],
"must_not":[
{
"terms":{
"state":["spam","deleted"]
}
}
]
}
}
},
"script_score":{
"script":"_score * log(doc['my_field'].value+1)"
}
}
}
}
// Returns: "No function with the name [filtered] is registered"
{
"query":{
"function_score":{
"filtered":{
"query":{
"query_string":{
"query":"this is a cool search",
"fields":[
"title",
"text"
]
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"account":1
}
}
],
"must_not":[
{
"terms":{
"state":["spam","deleted"]
}
}
]
}
}
}
},
"script_score":{
"script":"_score * log(doc['my_field'].value+1)"
}
}
}
// Returns: "[filtered] query does not support [function_score]]"
{
"query":{
"filtered":{
"function_score":{
"query":{
"query_string":{
"query":"this is a cool search",
"fields":[
"title",
"text"
]
}
},
"script_score":{
"script":"_score * log(doc['my_field'].value+1)"
}
},
"filter":{
"bool":{
"must":[
{
"term":{
"account":1
}
}
],
"must_not":[
{
"terms":{
"state":["spam","deleted"]
}
}
]
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment