Using Custom Sort with Track Scores set to True is still showing score as null

So I'm setting a default query in my React Native app. Essentially I'm trying to set a sortOrder based on the elementOrder values. My partner used this same piece of code in his web app and it works for him. It doesn't seem to work on my end. The score exists if I remove the custom sort, which is normal due to what I've read in the docs. When I'm using a custom sort, then I should add track_scores: true. My score is still coming up as null.

I am not sure how to debug this situation. Can someone point me in the right direction? Thanks! Here's my code and let me know if you need to see anything. Unfortunately I don't have access to Kibana. I'm just console logging the list item and it's properties.

const defaultQueryConfig = {
  track_scores: true,
  sort: {
    _script: {
      type: 'number',
      script: {
        lang: 'painless',
        source: `
            int sortOrder = 0;
            if (doc['elementOrder'].value == 1) {sortOrder =  3}
            else if (doc['elementOrder'].value == 3) {sortOrder =  2}
            else if (doc['elementOrder'].value == 2) {sortOrder =  1} 
            sortOrder; 
          `,
      },
      order: 'desc',
    },
  },
  query: {
    function_score: {
      query: {
        match_all: {},
      },
      functions: [
        {
          filter: {
            match: {
              categoryType: 'earth',
            },
          },
          weight: 100,
        },
        {
          filter: {
            match: {
              categoryType: 'water',
            },
          },
          weight: 90,
        },
        {
          filter: {
            match: {
              categoryType: 'fire',
            },
          },
          weight: 80,
        },
        {
          filter: {
            match: {
              thingExists: false,
            },
          },
          weight: 2,
        },
      ],
      score_mode: 'multiply',
    },
  },
};

can you share the original HTTP requests including JSON bodies and their responses?

Just a side note, you are doing two pretty expensive operations. A script score and a function score. If you have any influence on changing the sort order infos on index time that might be useful.

Also, out of curiosity, what are you doing with the score if you are not searching for it in your application?

So I want the order of categories to be displayed like so:

  • All 'earth' types should be the first displayed
  • 'water' is the next category on the list
  • 'fire' next'
  • If the type 'thingsExists' is false, then the score is multiplied by 2. The items with that property should take priority

I'm trying to figure out how to get the original HTTP requests currently so I'll show that info when I do. Thanks!

would it be easier to just issue three searches to fulfil the first requirement?

For the last part you could use a boosting query instead.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.