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

Boosting by popularity, or script_score in elasticsearch query #487

Closed
dioscuroi opened this issue Aug 19, 2018 · 4 comments
Closed

Boosting by popularity, or script_score in elasticsearch query #487

dioscuroi opened this issue Aug 19, 2018 · 4 comments
Assignees
Labels
enhancement native 📱 Issues related to React Native version of library web 🕸️ Issues related to Web version of library
Milestone

Comments

@dioscuroi
Copy link

dioscuroi commented Aug 19, 2018

Issue Type:
enhancement

Platform:
Web

Description:
Is there a way to use the "script_score" feature in the elasticsearch query to boost search results by popularity? The reference can be found here. Thanks!

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-script-score

Reactivesearch version: 2.7.0

@dioscuroi
Copy link
Author

dioscuroi commented Aug 25, 2018

I solved the problem by using the "customQuery" field. Below is my code.

<DataSearch
  componentId="searchbar"
  autosuggest={true}
  showClear={true}
  autoFocus
  customQuery={
    function(value, props) {
      return {
        function_score: {
          query: {
            match: { name: value }
          },
          script_score : {
            script : {
              source: "Math.log(2 + doc['popularity'].value)"
            }
          }
        }
      }
    }
  }
/>

@siddharthlatest
Copy link
Member

siddharthlatest commented Aug 28, 2018

We can also support this at the library level. DataSearch and CategorySearch components can have a prop called popularityField (which has to have a Numeric mapping). When present and valid, the query is transformed to a function_score type query.

Instead of a script_score function, we will support the field_value_factor function.

{
    "query": {
        "function_score": {
            "query": { ... original query ...},   // this is the query the component was generating so far.
            "field_value_factor": {
                "field": "likes",   // this is the user specified popularity field
                "modifier": "log1p",// this smoothens the score (read more: https://www.elastic.co/guide/en/elasticsearch/guide/current/boosting-by-popularity.html#_modifier)
                "missing": 1        // when field has missing value, treat this as default value.
            }
        }
    }
}

As @dioscuroi shared in the above example, a user can always override this behavior by specifying a customQuery, but this gives a very quick way to add custom popularity (and allows to support the popularity use-case via a GUI in Dejavu, appbase.io dashboard, etc.)

@siddharthlatest siddharthlatest added enhancement native 📱 Issues related to React Native version of library web 🕸️ Issues related to Web version of library labels Aug 28, 2018
@siddharthlatest siddharthlatest added this to the 2.9.0 milestone Aug 28, 2018
@dioscuroi
Copy link
Author

Yes, it will be awesome if popularity is supported at the library level. Thank you for adding it to the milestone. Please keep me updated when it is done! 😁

@stonesthatwhisper
Copy link

We can also support this at the library level. DataSearch and CategorySearch components can have a prop called popularityField (which has to have a Numeric mapping). When present and valid, the query is transformed to a function_score type query.

Is this actually implemented?
So I was trying to use customQuery to do something similar, but I need to obtain the "original query" generated, but could not find a way to get it. Is this at all possible?

Instead of a script_score function, we will support the field_value_factor function.

{
    "query": {
        "function_score": {
            "query": { ... original query ...},   // this is the query the component was generating so far.
            "field_value_factor": {
                "field": "likes",   // this is the user specified popularity field
                "modifier": "log1p",// this smoothens the score (read more: https://www.elastic.co/guide/en/elasticsearch/guide/current/boosting-by-popularity.html#_modifier)
                "missing": 1        // when field has missing value, treat this as default value.
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement native 📱 Issues related to React Native version of library web 🕸️ Issues related to Web version of library
Projects
None yet
Development

No branches or pull requests

5 participants