Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support query and filter in function_score #8638

Closed
sandstrom opened this issue Nov 24, 2014 · 4 comments
Closed

Support query and filter in function_score #8638

sandstrom opened this issue Nov 24, 2014 · 4 comments
Assignees
Labels
>enhancement :Search/Search Search-related issues that do not fall into other categories v1.5.0 v2.0.0-beta1

Comments

@sandstrom
Copy link
Contributor

Function score is an excellent addition to ES. However, it doesn't support query + filter, which works in most other parts of ES.

Currently, function_score takes either a filter OR a query, but not both. Allowing support for both query and filter would be good because:

  1. It would align better with the rest of ES (it took me a while to figure out why the filter wasn't applied). Overall the API is fairly consistent and well-designed, which I like. Smoothing out this rough edge would make it even better!
  2. It would be very useful.

Below is a simple use-case.

# This query is run against two indices, 'users' and 'speakers'.
# But I think the situation would be the same if it was a single index, 
# with two different types in it.
# 
# I get the following error:
# `QueryParsingException[[users] Unknown field [mentioned_at]];`
#
# All speakers has the `mentioned_at` field, and no user has it.

query_body = {
  :query => {
    :bool => {
      :should => [
        {
          :function_score => {
            :query => {
              :multi_match => {
                :query => "MY QUERY HERE",
                :type => :phrase_prefix,
                :fields => ['name']
              }
            },
            :filter => { :type => { :value => 'speaker' } },
            :functions => [
              {
                :gauss => {
                  'mentioned_at' => {
                    :scale => '180d',
                    :offset => '5d',
                    :decay => 0.5,
                  }
                }
              }
            ]
          }
        },
        {
          :filtered => {
            :query => {
              :multi_match => {
                :query => "MY QUERY HERE",
                :type => :phrase_prefix,
                :fields => ['name']
              }
            },
            :filter => { :type => { :value => 'user' } },
          }
        }
      ]
    }
  }
}

Reference

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html

@clintongormley
Copy link

Hi @sandstrom

You can use a filtered query instead, which will do what you need. However, I agree that silently choosing just one of filter or query is incorrect. I see no need why we can't combine both.

@brwe ?

@sandstrom
Copy link
Contributor Author

@clintongormley Awesome, thanks for looking into this! ⛵

@brwe
Copy link
Contributor

brwe commented Nov 26, 2014

easiest would be to just convert given query and filter to a filtered_query. I'll make a pr shortly.

brwe added a commit to brwe/elasticsearch that referenced this issue Nov 26, 2014
Before, if filter and query was defined for function_score, then the
filter was silently ignored. Now, if both is defined then function score
query wraps this in a filtered_query.

closes elastic#8638
@sandstrom
Copy link
Contributor Author

@brwe Awesome!

brwe added a commit that referenced this issue Jan 19, 2015
Before, if filter and query was defined for function_score, then the
filter was silently ignored. Now, if both is defined then function score
query wraps this in a filtered_query.

closes #8638
closes #8675
@brwe brwe closed this as completed in 7230e60 Jan 19, 2015
@clintongormley clintongormley added :Search/Search Search-related issues that do not fall into other categories and removed :Query DSL labels Feb 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>enhancement :Search/Search Search-related issues that do not fall into other categories v1.5.0 v2.0.0-beta1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants