Last active
April 2, 2016 00:44
-
-
Save tyler-smith/1c30566b01aa76bd75e1 to your computer and use it in GitHub Desktop.
ES function_score troubles with filtered query_string query
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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